Module: SuperModel::Association::ClassMethods

Defined in:
lib/supermodel/association.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(to_model, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/supermodel/association.rb', line 6

def belongs_to(to_model, options = {})
  to_model    = to_model.to_s
  class_name  = options[:class_name]  || to_model.classify
  foreign_key = options[:foreign_key] || "#{to_model}_id"
  primary_key = options[:primary_key] || "id"
  
  attributes foreign_key
  
  class_eval(<<-EOS, __FILE__, __LINE__ + 1)
    def #{to_model}                                             # def user
      #{foreign_key} && #{class_name}.find(#{foreign_key})      #   user_id && User.find(user_id)
    end                                                         # end
                                                                # 
    def #{to_model}?                                            # def user?
      #{foreign_key} && #{class_name}.exists?(#{foreign_key})   #   user_id && User.exists?(user_id)
    end                                                         # end
                                                                # 
    def #{to_model}=(object)                                    # def user=(model)
      self.#{foreign_key} = (object && object.#{primary_key})   #   self.user_id = (model && model.id)
    end                                                         # end
  EOS
end

#has_many(to_model, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/supermodel/association.rb', line 29

def has_many(to_model, options = {})
  to_model    = to_model.to_s
  class_name  = options[:class_name]  || to_model.classify
  foreign_key = options[:foreign_key] || "#{model_name.singular}_id"
  primary_key = options[:primary_key] || "id"
  class_eval(<<-EOS, __FILE__, __LINE__ + 1)
    def #{to_model}                                             # def user
      #{class_name}.find_all_by_attribute(                      #   User.find_all_by_attribute(
        :#{foreign_key},                                        #    :task_id,
        #{primary_key}                                          #    id
      )                                                         #   )
    end                                                         # end
  EOS
end