Module: PassiveRecord::Associations

Included in:
ClassMethods
Defined in:
lib/passive_record/associations.rb,
lib/passive_record/associations/has_one.rb,
lib/passive_record/associations/has_many.rb,
lib/passive_record/associations/belongs_to.rb,
lib/passive_record/associations/has_many_through.rb

Defined Under Namespace

Classes: BelongsToAssociation, BelongsToRelation, HasManyAssociation, HasManyRelation, HasManyThroughAssociation, HasManyThroughRelation, HasOneAssociation, HasOneRelation, Relation

Instance Method Summary collapse

Instance Method Details

#associate!(assn) ⇒ Object



8
9
10
11
12
# File 'lib/passive_record/associations.rb', line 8

def associate!(assn)
  @associations ||= []
  @associations += [assn] unless @associations.include?(assn)
  self
end

#associations_id_symsObject



14
15
16
17
18
19
20
21
22
# File 'lib/passive_record/associations.rb', line 14

def associations_id_syms
  @associations && @associations.map do |assn|
    if assn.is_a?(HasOneAssociation) || assn.is_a?(BelongsToAssociation)
      (assn.target_name_symbol.to_s + "_id").to_sym
    else
      (assn.target_name_symbol.to_s.singularize + "_ids").to_sym
    end
  end || []
end

#belongs_to(parent_name_sym, opts = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/passive_record/associations.rb', line 24

def belongs_to(parent_name_sym, opts={})
  target_class_name = opts.delete(:class_name) { (parent_name_sym.to_s).split('_').map(&:capitalize).join }
  association = BelongsToAssociation.new(self, target_class_name, parent_name_sym)
  associate!(association)

  define_method(:"#{parent_name_sym}_id") do
    prnt = send(parent_name_sym)
    prnt && prnt.id
  end

  define_method(parent_name_sym) do
    relation = detect_relation(association)
    association.parent_class.find(relation.parent_model_id)
  end

  define_method(:"#{parent_name_sym}=") do |new_parent|
    send(:"#{parent_name_sym}_id=", new_parent.id)
  end

  define_method(:"#{parent_name_sym}_id=") do |new_parent_id|
    relation = detect_relation(association)
    relation.parent_model_id = new_parent_id
  end
end

#has_and_belongs_to_many(collection_name_sym) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/passive_record/associations.rb', line 173

def has_and_belongs_to_many(collection_name_sym)
  habtm_join_class_name =
    self.name.split('::').last.singularize +
    collection_name_sym.to_s.camelize.singularize +
    "JoinModel"
  inverse_habtm_join_class_name =
    collection_name_sym.to_s.camelize.singularize +
    self.name.split('::').last.singularize +
    "JoinModel"

  module_name = self.name.deconstantize
  module_name = "Object" if module_name.empty?
  intended_module = module_name.constantize

  if (intended_module.const_get(inverse_habtm_join_class_name) rescue false)
    has_many inverse_habtm_join_class_name.underscore.to_sym
    has_many collection_name_sym, :through => inverse_habtm_join_class_name.underscore.to_sym, habtm: true
  else
    auto_collection_sym = self.name.split('::').last.underscore.pluralize.to_sym
    eval <<-ruby
    class #{module_name}::#{habtm_join_class_name}          # class System::UserRoleJoinModel
      include PassiveRecord                                 #   include PassiveRecord
      belongs_to :#{collection_name_sym.to_s.singularize}   #   belongs_to :role
      belongs_to :#{auto_collection_sym.to_s.singularize}   #   belongs_to :user
    end                                                     # end
    ruby
    has_many habtm_join_class_name.underscore.to_sym
    has_many(collection_name_sym, :through => habtm_join_class_name.underscore.to_sym, habtm: true)
  end
end

#has_many(collection_name_sym, opts = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/passive_record/associations.rb', line 84

def has_many(collection_name_sym, opts={})
  target_class_name = opts.delete(:class_name) { (collection_name_sym.to_s).split('_').map(&:capitalize).join.singularize }
  habtm = opts.delete(:habtm) { false }

  association = nil
  if opts.key?(:through)
    through_class_collection_name = opts.delete(:through)

    through_class_name = (through_class_collection_name.to_s).split('_').map(&:capitalize).join.singularize
    base_association = associations.detect { |assn| assn.child_class_name == through_class_name rescue false }

    association = HasManyThroughAssociation.new(
      self, target_class_name, collection_name_sym, through_class_collection_name, base_association, habtm)

    associate!(association)

    define_method(:"#{collection_name_sym}=") do |new_collection|
      send(:"#{collection_name_sym.to_s.singularize}_ids=", new_collection.map(&:id))
    end

    define_method(:"#{collection_name_sym.to_s.singularize}_ids=") do |new_collection_ids|
      relation = detect_relation(association) # relata.detect { |rel| rel.association == association }

      intermediary = relation.intermediary_relation

      # drop all intermediary relations
      intermediary.where( relation.parent_model_id_field => relation.id ).each do |intermediate|
        intermediate.destroy
      end

      # add in new ones...
      singular_target = collection_name_sym.to_s.singularize
      if !(relation.nested_association.is_a?(BelongsToAssociation))
        intermediary.create(
          singular_target + "_ids" => new_collection_ids,
          relation.parent_model_id_field => relation.id
        )
      else
        new_collection_ids.each do |child_id|
          intermediary.create(
            singular_target + "_id" => child_id,
            relation.parent_model_id_field => relation.id
          )
        end
      end
    end
  else
    association = HasManyAssociation.new(self, target_class_name, collection_name_sym)
    associate!(association)

    define_method(:"#{collection_name_sym}=") do |new_collection|
      relation = detect_relation(association)

      # detach existing children...
      relation.all.each do |child|
        child.send(:"#{relation.parent_model_id_field}=", nil)
      end

      # reattach new children
      new_collection.each do |child|
        child.send(:"#{relation.parent_model_id_field}=", relation.id)
      end
    end

    define_method(:"#{collection_name_sym.to_s.singularize}_ids=") do |new_collection_ids|
      relation = detect_relation(association) #@ relata.detect { |rel| rel.association == association }
      send(:"#{collection_name_sym}=", relation.child_class.find(new_collection_ids))
    end
  end

  define_method(collection_name_sym) do
    detect_relation(association)
    # relata.detect { |rel| rel.association == association }
  end

  define_method(:"#{collection_name_sym.to_s.singularize}_ids") do
    begin
      send(collection_name_sym).map(&:id)
    rescue
      binding.pry
    end
  end

  define_method(:"create_#{collection_name_sym.to_s.singularize}") do |attrs={}|
    relation = detect_relation(association) # relata.detect { |rel| rel.association == association }
    relation.create(attrs)
  end
end

#has_one(child_name_sym) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/passive_record/associations.rb', line 49

def has_one(child_name_sym)
  child_class_name = (child_name_sym.to_s).split('_').map(&:capitalize).join
  association = HasOneAssociation.new(self, child_class_name, child_name_sym)
  associate!(association)

  define_method(:"#{child_name_sym}_id") do
    chld = send(child_name_sym)
    chld && chld.id
  end

  define_method(child_name_sym) do
    relation = detect_relation(association)
    relation.lookup
  end

  define_method(:"create_#{child_name_sym}") do |attrs={}|
    relation = detect_relation(association)
    relation.create(attrs)
  end

  define_method(:"#{child_name_sym}=") do |new_child|
    send(:"#{child_name_sym}_id=", new_child.id)
  end

  define_method(:"#{child_name_sym}_id=") do |new_child_id|
    relation = detect_relation(association) #relata.detect { |rel| rel.association == association }
    rel = relation.lookup
    rel && rel.send(:"#{relation.parent_model_id_field}=", nil)

    relation.child_class.
      find(new_child_id).
      update(relation.parent_model_id_field => relation.id)
  end
end