Module: Hippo::Concerns::ExportAssociations::ClassMethods

Defined in:
lib/hippo/concerns/export_associations.rb

Instance Method Summary collapse

Instance Method Details

#export_associations(*associations) ⇒ Object

Mark associations as exported, meaning they can be quered against and optionally written to

Parameters:

  • associations (list of Symbols)
  • options (Hash)

    a customizable set of options



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hippo/concerns/export_associations.rb', line 17

def export_associations( *associations )
    self.exported_associations ||= {}
    associations.flatten!
    options = associations.extract_options!
    associations.each do | assoc_name |
        self.exported_associations[ assoc_name.to_sym ] = options
        if options[:writable]
            accepts_nested_attributes_for( assoc_name, options.except(:writable, :optional) )
        end
        if false == options[:optional]
            self.export_methods( *assoc_name, { :optional=>false })
        end
    end
end

#has_exported_association?(association, user) ⇒ Boolean

If the association is exported it may be queried against and it’s data included along with the parent record. It may not be written to unless the :writable flag was set, in which case it’ll be allowed by has_exported_association?

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/hippo/concerns/export_associations.rb', line 35

def has_exported_association?( association, user )
    self.exported_associations &&
        ( options = self.exported_associations[ association.to_sym ] ) &&
        evaluate_export_limit( user, :association, association, options[:limit] )
end

#has_exported_nested_attribute?(association, user) ⇒ Boolean

Does the association allow writes? True if the association has been exported and it accepts nested attributes

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/hippo/concerns/export_associations.rb', line 43

def has_exported_nested_attribute?( association, user )
    self.nested_attributes_options[ association.to_sym ] &&
        self.has_exported_association?( association, user )
end