passive_record 0.3.14 → 0.3.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b01350b2c51dae72a7383e4752d6bfaf450ba98
4
- data.tar.gz: 3b0bcbc94736f53de51156504791562116687bdf
3
+ metadata.gz: 08dd9a55dcc21162ef347d7ca570df92cb35cecf
4
+ data.tar.gz: 0e8c8a07e5edbb224b9737efbad16f9ae127d7ff
5
5
  SHA512:
6
- metadata.gz: 0a3f2c488bf01cea83122cfb4cb5a8b8d4f0e41f175827f0ccc0ab404a71b8b6c0104b170fe0cb15e68dd29da32424c53df0a714c603f29c0a32c9b7470d9684
7
- data.tar.gz: bd24c57315e0b57bd6df836e19e55471dd90c699bbae4476e9eb4c3c0beea2dbb242555ee396e7a9404595b7c2094f32b378c864dfbb5153083f8856a763683e
6
+ metadata.gz: b27ddf9aafe4b6e6b40feea2f7fe71c20d0f3a37d371a96a1fb485fc22a9b3015026b7529b341bb1d19846b709463304c04ae799aadfe25db7f9d813f81029b5
7
+ data.tar.gz: afe63b9e603d25506c67a2fa54d55e459cfe4ae6a4562d8ae1279129a2eeaae5923f8609d8fb34417d20303a14b065fed632e7ffad54d6323e7c456215e24bd2
data/README.md CHANGED
@@ -176,7 +176,10 @@ PassiveRecord may be right for you!
176
176
 
177
177
  - `before_create :call_a_method`
178
178
  - `after_create :call_another_method, :and_then_call_another_one`
179
+ - `before_update do manually_invoke(a_method) end`
179
180
  - `after_update { or_use_a_block }`
181
+ - `before_destroy :something`
182
+ - `after_destroy { something_else }`
180
183
 
181
184
  ## Copyright
182
185
 
@@ -109,16 +109,25 @@ module PassiveRecord
109
109
  end
110
110
 
111
111
  # add in new ones...
112
- target_name = relation.association.target_name_symbol.to_s
113
- new_collection_ids.each do |child_id|
114
- intermediary.create(target_name.singularize + "_id" => child_id, relation.parent_model_id_field => relation.id )
112
+ singular_target = collection_name_sym.to_s.singularize
113
+ if !(relation.nested_association.is_a?(BelongsToAssociation))# &&
114
+ intermediary.create(
115
+ singular_target + "_ids" => new_collection_ids,
116
+ relation.parent_model_id_field => relation.id
117
+ )
118
+ else
119
+ new_collection_ids.each do |child_id|
120
+ intermediary.create(
121
+ singular_target + "_id" => child_id,
122
+ relation.parent_model_id_field => relation.id
123
+ )
124
+ end
115
125
  end
116
126
  end
117
127
  else
118
128
  association = HasManyAssociation.new(self, target_class_name, collection_name_sym)
119
129
  associate!(association)
120
130
 
121
-
122
131
  define_method(:"#{collection_name_sym}=") do |new_collection|
123
132
  relation = instance_eval{relata}.detect { |rel| rel.association == association }
124
133
 
@@ -145,12 +154,12 @@ module PassiveRecord
145
154
  end
146
155
 
147
156
  define_method(:"#{collection_name_sym.to_s.singularize}_ids") do
148
- send(collection_name_sym).map(&:id)
157
+ send(collection_name_sym).map(&:id) rescue binding.pry
149
158
  end
150
159
 
151
160
  define_method(:"create_#{collection_name_sym.to_s.singularize}") do |attrs={}|
152
161
  relation = instance_eval{relata}.detect { |rel| rel.association == association }
153
- relation.create(attrs)
162
+ relation.create(attrs)
154
163
  end
155
164
  end
156
165
 
@@ -167,7 +176,7 @@ module PassiveRecord
167
176
  module_name = self.name.deconstantize
168
177
  module_name = "Object" if module_name.empty?
169
178
  intended_module = module_name.constantize
170
-
179
+
171
180
  if (intended_module.const_get(inverse_habtm_join_class_name) rescue false)
172
181
  has_many inverse_habtm_join_class_name.underscore.pluralize.to_sym
173
182
  has_many collection_name_sym, :through => inverse_habtm_join_class_name.underscore.pluralize.to_sym
@@ -9,7 +9,19 @@ module PassiveRecord
9
9
  class HasManyThroughRelation < HasManyRelation
10
10
  def <<(child)
11
11
  if nested_association.is_a?(HasManyAssociation)
12
- intermediary_relation.create(association.target_name_symbol.to_s.singularize + "_ids" => [child.id])
12
+ intermediary_id =
13
+ child.send(association.base_association.target_name_symbol.to_s.singularize + "_id")
14
+
15
+ if intermediary_id
16
+ intermediary_relation.child_class.find(intermediary_id).
17
+ send(:"#{parent_model_id_field}=", parent_model.id)
18
+ else
19
+ nested_ids_field = nested_association.children_name_sym.to_s.singularize + "_ids"
20
+ intermediary_relation.create(
21
+ parent_model_id_field => parent_model.id,
22
+ nested_ids_field => [ child.id ]
23
+ )
24
+ end
13
25
  else
14
26
  intermediary_relation.
15
27
  where(
@@ -49,7 +61,7 @@ module PassiveRecord
49
61
  if intermediate_results && !intermediate_results.empty?
50
62
  final_results = intermediate_results.flat_map(&nested_association.target_name_symbol)
51
63
  if final_results.first.is_a?(Associations::Relation) && !final_results.first.singular?
52
- final_results.first.send(:all)
64
+ final_results.flat_map(&:all)
53
65
  else
54
66
  Array(final_results)
55
67
  end
@@ -70,6 +70,9 @@ module PassiveRecord
70
70
  end
71
71
 
72
72
  instance
73
+ rescue => ex
74
+ binding.pry
75
+ raise ex
73
76
  end
74
77
 
75
78
  def destroy(id)
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.3.14"
3
+ VERSION = "0.3.15"
4
4
  end
@@ -374,7 +374,13 @@ describe "passive record models" do
374
374
  end
375
375
 
376
376
  it 'should construct intermediary relations with many-through-many' do
377
- expect{parent.create_dog}.to change{Family::Dog.count}.by(1)
377
+ expect{parent.dogs << Family::Dog.create}.to change{parent.dogs.count}.by(1)
378
+
379
+ expect{parent.create_dog}.to change{parent.dogs.count}.by(1)
380
+
381
+ expect{parent.toys << Family::Toy.create}.to change{parent.toys.count}.by(1)
382
+ expect{parent.create_dog(child: child)}.to change{child.dogs.count}.by(1)
383
+ expect{parent.create_dog(child_id: child.id)}.to change{child.dogs.count}.by(1)
378
384
  end
379
385
 
380
386
  it 'should accept class name' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.14
4
+ version: 0.3.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman