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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 08dd9a55dcc21162ef347d7ca570df92cb35cecf
|
4
|
+
data.tar.gz: 0e8c8a07e5edbb224b9737efbad16f9ae127d7ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
113
|
-
|
114
|
-
intermediary.create(
|
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
|
-
|
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
|
-
|
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.
|
64
|
+
final_results.flat_map(&:all)
|
53
65
|
else
|
54
66
|
Array(final_results)
|
55
67
|
end
|
data/spec/passive_record_spec.rb
CHANGED
@@ -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.
|
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
|