Module: Airmodel::Associable

Included in:
Model
Defined in:
lib/airmodel/associable.rb

Instance Method Summary collapse

Instance Method Details

#default_has_many_contraintsObject



46
47
48
# File 'lib/airmodel/associable.rb', line 46

def default_has_many_contraints
  true
end

#has_many(association_name, args = {}) ⇒ Object

defines a clone of the child class on this model required args: association_name



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/airmodel/associable.rb', line 6

def has_many(association_name, args={})
  args[:class_name] ||= association_name.to_s.singularize.capitalize
  define_method association_name do
    config = if args[:base_key]
               # the airtable base_id is dynamically configured
               # as a column on the parent model,
               # and the table_name is either passed as
               # an argument or inferrred from the child model name
               {
                 base_id: self.send(args[:base_key]),
                 table_name: args[:table_name] || association_name.to_s.tableize
               }
               # maybe the base is defined in the config file
             elsif c = Airmodel.bases[args[:class_name].tableize.to_sym]
               c
            # maybe the base is just a table in the same base as the parent
             else
               {
                 base_id: self.class.base_config[:base_id],
                 table_name: args[:table_name] || association_name.to_s.tableize
               }
             end
    finder_name = "@#{association_name}_finder"
    if f = instance_variable_get(finder_name)
      f
    else
      finder = Class.new(Object.const_get args[:class_name]) do
        @base_id = config[:base_id]
        @table_name = config[:table_name]
      end
      constraints = if args[:constraints].respond_to?(:call)
                      args[:constraints].call(self)
                    else
                      {}
                    end
      instance_variable_set(finder_name, finder.where(constraints))
    end
  end
end