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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDNjNTU1NmM2MDhkZmIwODRlMjE1ZjhmMmRiYzI5ODFlMzFlZDIxMg==
4
+ YWYxODZiZjI4ZDA3ZmI3N2RlMzJjZDliMmQ1ZTRlYzg5NDkxZDZlNw==
5
5
  data.tar.gz: !binary |-
6
- MjQ0NGYyYjg1NmRhMzY1NGVjZGFiZGYzODk1NGQyMTJjMWJkMzFhNg==
6
+ YWIzZDY5MGUwN2NkYzliNjc0YmMxYTkyY2QyOTg2NzIwNzAzMjEyNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTMyNDA4MDY3N2UyZDJmMDgxNzU5OWQ1N2JhNWZlOWRiN2Q2YzE1MDg2MTQ4
10
- OGMzNmE0ZTgwZjEyNzc2YjU4ZjFjOWI0ODEzYzdiMDNmODI4N2VlYjI3NDk3
11
- ZWNmYTc0MmRkODdmYzFhNTEzOTM1MzdkNTM0OGQ0NWViODg1ZGU=
9
+ NGQ3ZjVkMjc3MTMxNmEwNWFhMzI2MWQwNTc2NDEwYTM5MDZiZDljNWM5MDcx
10
+ MDhiZDM1MjU5OThiMjM0YjAwOWExN2YwMWE0NjU3NDlmMzM2ZGUzMmI5Yjk2
11
+ YmVhMzMyMDAxYzZmYzY5YjM5ZDEzNzAxNjNkZmM1YWUwMjUxMDA=
12
12
  data.tar.gz: !binary |-
13
- ZmZhZmRmZjNlODA1YzkyMzQ5MzcyNGQzODRkNWM3ZTZlNjJkN2JkZTdkOWFl
14
- ZDdhZDc2ZjcyYzdiMDc4ODkxZjdiZWI0MjNhODljNWYyOTRlOTMwMzdkMDk2
15
- NzlkOTc3NGI0NDkxNTBjYjVhYzdhNjJiZGVlYWY0N2EyMTg3YmY=
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 build_from_represented_object(reflection, nested_object_name, attributes)
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
- return nil if reflection.klass != self.class.represented_object_class.reflect_on_association(nested_object_name).try(:klass)
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(attributes)
207
+ represented_object.send(nested_object_name).try(:build, attributes)
188
208
  end
189
209
  end
190
210
 
@@ -1,3 +1,3 @@
1
1
  module ObjectAttorney
2
- VERSION = "2.2.11"
2
+ VERSION = "2.2.12"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_attorney
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.11
4
+ version: 2.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Gonçalves