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("    def \#{to_model}                                             # def user\n      \#{foreign_key} && \#{class_name}.find(\#{foreign_key})      #   user_id && User.find(user_id)\n    end                                                         # end\n                                                                # \n    def \#{to_model}?                                            # def user?\n      \#{foreign_key} && \#{class_name}.exists?(\#{foreign_key})   #   user_id && User.exists?(user_id)\n    end                                                         # end\n                                                                # \n    def \#{to_model}=(object)                                    # def user=(model)\n      self.\#{foreign_key} = (object && object.\#{primary_key})   #   self.user_id = (model && model.id)\n    end                                                         # end\n  EOS\nend\n", __FILE__, __LINE__ + 1)

#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("    def \#{to_model}                                             # def user\n      \#{class_name}.find_all_by_attribute(                      #   User.find_all_by_attribute(\n        :\#{foreign_key},                                        #    :task_id,\n        \#{primary_key}                                          #    id\n      )                                                         #   )\n    end                                                         # end\n  EOS\nend\n", __FILE__, __LINE__ + 1)