object_attorney 2.1.1 → 2.1.2

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
- ZGE1NWUyMGQ1NzE4YjIyMmM0ZmJhN2YxYWJiZjczYmM1MTk5YzA1Mw==
4
+ MDY1NGY3N2NlMjUyNDg4N2E2NGY2MTExMjhiNDg4YmU1NGRkM2E1ZA==
5
5
  data.tar.gz: !binary |-
6
- OTU5Y2FkZThiNDQ2Y2ZjZjhkYmIyNzhlN2ZlNmJhOWQ3OGZmOWU4ZA==
6
+ OWVjYjNkZGJkMDVlNmRhZmZlY2E0NDQ4Y2MwYjYyZTA4ZTYzNGJmNA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTliNTEyNjU2ZjRhM2QwNjEyOTMyNmFiYzM4ZDJmYTViMmYwMzM0NDE4Y2Fj
10
- ZGIzMDhhMjk1YjdjZjFjNWExYzU0NTRiZTdmOGM0NDJmZDk5OWExN2M2MWJk
11
- MTI2MTdhNTIyMmExYTQ1MjVlYjY5NmVlZmI4ZGFmZTEwMzE3MDI=
9
+ NmZhNTQ3NTg4Y2FjYWExM2M1ZDE1OGU2ODc2YTMxYmY5ZjhlYTgzMDQ3YjJm
10
+ NWE2NmYyN2FjMWFmOTIzNjA5ZDk3MjdhODhlZTU0YzVmOWRkNTEyYzViMmI0
11
+ YjU3OWJkNTA4ZmZlMmNjNTEyYzM2ZGYzN2E1ZjNmM2ZiMDk1M2Y=
12
12
  data.tar.gz: !binary |-
13
- MDdiMmRjMjgxMDdkYmNiODU1MTMxYTA0ODU0M2JjZDVmMDYyYjNhNGFlMzFm
14
- NDZmODY0NWUxNGYxN2Q2ZTA0NTY3YjBjODMxMTc4Y2IxM2UyNzViZWMyMjA1
15
- ZGVjNzMyOWEzZGMyNjIzYjA1MzY0NzkzMWJmMGEzODRiMzE0OTU=
13
+ ODAwYjliMmU0M2Y4YjEwOTg0ZGMxMzU5NDkxYjg5ODgzZDk2M2JmNGE2Y2M1
14
+ ZTE4OTJiZTdiODIzNmZmYTllN2VmYWViZmNlYzMzMDk0ZTkyODYyOGQ5NjE4
15
+ Zjc4NmVhNWYyMTdlYTczMDRiNzY1ODFlNDMzOThkZjMwZDRmMmM=
@@ -141,13 +141,26 @@ module ObjectAttorney
141
141
  def build_nested_object(nested_object_name, attributes = {})
142
142
  reflection = self.class.reflect_on_association(nested_object_name)
143
143
 
144
- new_nested_object = reflection.klass.new(attributes)
145
-
144
+ new_nested_object = build_from_represented_object(reflection, nested_object_name, attributes) || reflection.klass.new(attributes)
145
+
146
146
  populate_foreign_key(self, new_nested_object, reflection, :has_many)
147
147
 
148
148
  new_nested_object
149
149
  end
150
150
 
151
+ def build_from_represented_object(reflection, nested_object_name, attributes)
152
+ return nil if represented_object.blank?
153
+ return nil if reflection.klass != self.class.represented_object_class.reflect_on_association(nested_object_name).klass
154
+
155
+ build_method = "build_#{nested_object_name}"
156
+
157
+ if represented_object.respond_to?(build_method)
158
+ represented_object.send(build_method, attributes)
159
+ else
160
+ represented_object.send(nested_object_name).build(attributes)
161
+ end
162
+ end
163
+
151
164
  def existing_nested_objects(nested_object_name)
152
165
  nested_association_klass = self.class.reflect_on_association(nested_object_name).klass
153
166
 
@@ -1,3 +1,3 @@
1
1
  module ObjectAttorney
2
- VERSION = "2.1.1"
2
+ VERSION = "2.1.2"
3
3
  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.1.1
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Gonçalves