object_attorney 2.2.11 → 2.2.12
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 +8 -8
- data/lib/object_attorney/nested_objects.rb +24 -4
- data/lib/object_attorney/version.rb +1 -1
- data/lib/object_attorney.rb +6 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
---
|
|
2
2
|
!binary "U0hBMQ==":
|
|
3
3
|
metadata.gz: !binary |-
|
|
4
|
-
|
|
4
|
+
YWYxODZiZjI4ZDA3ZmI3N2RlMzJjZDliMmQ1ZTRlYzg5NDkxZDZlNw==
|
|
5
5
|
data.tar.gz: !binary |-
|
|
6
|
-
|
|
6
|
+
YWIzZDY5MGUwN2NkYzliNjc0YmMxYTkyY2QyOTg2NzIwNzAzMjEyNQ==
|
|
7
7
|
SHA512:
|
|
8
8
|
metadata.gz: !binary |-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
NGQ3ZjVkMjc3MTMxNmEwNWFhMzI2MWQwNTc2NDEwYTM5MDZiZDljNWM5MDcx
|
|
10
|
+
MDhiZDM1MjU5OThiMjM0YjAwOWExN2YwMWE0NjU3NDlmMzM2ZGUzMmI5Yjk2
|
|
11
|
+
YmVhMzMyMDAxYzZmYzY5YjM5ZDEzNzAxNjNkZmM1YWUwMjUxMDA=
|
|
12
12
|
data.tar.gz: !binary |-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
NjhkMWYzYTFlNTg2NGQ4YzRmYjgzMWNmYjBiOTg0MGJlNjUzZGNkZjEwMjBj
|
|
14
|
+
MGJiZTViMmYwMzViN2JkMzBlZDVkODA1MmVlZTBiNmI3YWVjMDJmZmNhMTA5
|
|
15
|
+
ZmVlOWNjODc1YjcwMjk4OTYwN2JhOWZkMmE5NDFjZGRiYTljYjA=
|
|
@@ -168,23 +168,43 @@ module ObjectAttorney
|
|
|
168
168
|
def build_nested_object(nested_object_name, attributes = {})
|
|
169
169
|
reflection = self.class.reflect_on_association(nested_object_name)
|
|
170
170
|
|
|
171
|
-
new_nested_object = build_from_represented_object(reflection, nested_object_name, attributes) || reflection.klass.new(attributes)
|
|
172
171
|
|
|
172
|
+
if can_represented_object_build_nested?(reflection, nested_object_name)
|
|
173
|
+
new_nested_object = build_from_represented_object(reflection, nested_object_name, attributes)
|
|
174
|
+
|
|
175
|
+
new_nested_object = build_nested_custom_class_if_necessary(reflection, attributes, new_nested_object)
|
|
176
|
+
else
|
|
177
|
+
new_nested_object = reflection.klass.new(attributes)
|
|
178
|
+
end
|
|
179
|
+
|
|
173
180
|
populate_foreign_key(self, new_nested_object, reflection, :has_many)
|
|
174
181
|
|
|
175
182
|
new_nested_object
|
|
176
183
|
end
|
|
177
184
|
|
|
178
|
-
def
|
|
185
|
+
def build_nested_custom_class_if_necessary(reflection, attributes, new_nested_object)
|
|
186
|
+
real_reflection_class = self.class.represented_object_reflect_on_association(reflection.name).try(:klass)
|
|
187
|
+
|
|
188
|
+
if reflection.klass == real_reflection_class
|
|
189
|
+
new_nested_object
|
|
190
|
+
else
|
|
191
|
+
reflection.klass.respond_to?(:represents) ? reflection.klass.new(attributes, new_nested_object) : reflection.klass.new(attributes)
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def can_represented_object_build_nested?(reflection, nested_object_name)
|
|
179
196
|
return nil if represented_object.blank?
|
|
180
|
-
|
|
197
|
+
|
|
198
|
+
represented_object.respond_to?("build_#{nested_object_name}") || represented_object.send(nested_object_name).respond_to?(:build)
|
|
199
|
+
end
|
|
181
200
|
|
|
201
|
+
def build_from_represented_object(reflection, nested_object_name, attributes)
|
|
182
202
|
build_method = "build_#{nested_object_name}"
|
|
183
203
|
|
|
184
204
|
if represented_object.respond_to?(build_method)
|
|
185
205
|
represented_object.send(build_method, attributes)
|
|
186
206
|
else
|
|
187
|
-
represented_object.send(nested_object_name).build
|
|
207
|
+
represented_object.send(nested_object_name).try(:build, attributes)
|
|
188
208
|
end
|
|
189
209
|
end
|
|
190
210
|
|
data/lib/object_attorney.rb
CHANGED
|
@@ -117,6 +117,12 @@ module ObjectAttorney
|
|
|
117
117
|
represented_object_reflection.try(:klass)
|
|
118
118
|
end
|
|
119
119
|
|
|
120
|
+
def represented_object_reflect_on_association(association)
|
|
121
|
+
return nil if represented_object_class.nil?
|
|
122
|
+
|
|
123
|
+
represented_object_class.reflect_on_association(association)
|
|
124
|
+
end
|
|
125
|
+
|
|
120
126
|
def zuper_method(method_name, *args)
|
|
121
127
|
self.superclass.send(method_name, *args) if self.superclass.respond_to?(method_name)
|
|
122
128
|
end
|