object_attorney 2.1.3 → 2.2.1

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
- MTNkY2ZkYWY2YWQ2Zjk0MWM2MTBmYTNjN2M4NDFkNDkwMTVmMWE2Yw==
4
+ NDBmNWRmNjg0YjdkZjBmYjI1NjUyOTU4ZjIwOGVhMWI4ZWY3MzcyMQ==
5
5
  data.tar.gz: !binary |-
6
- NTA2YjBiNjgwZDI5OGJiYmI5Y2ZlYTc3ZTFmN2Y2MWNjMWY1MWFlNg==
6
+ YjcyNTlkZmEyY2UzNWUyYjM4OGQzMDg2MTgzMmY3Mzg5YTJkZmFjYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTNhOTMzYjJmMDcxMzQ1YzFjMDRiYzlmNzE0NTYyMWVkNGE1YmU0NTVhYTRl
10
- MTc1MjM5NDc4OGNlZDQ5Y2EzYjgwZWU2N2JhZDQxMmRhN2M3OTU3Y2ZmNTNh
11
- ZDM1YzY2ODgzOWQ4MDk4OWZkNDdlNTMyYWM5MTk5YjcxZGE4YzU=
9
+ NDY0NTBlODhjNWI1OTc0ZDc2MmNmNjI2MmU2YWJmMjc4MTg2YmYxOWI1NDhm
10
+ MTUwYzdmMTVkMDQ4ZmU4NmJlYTdmZGRmNzBkMTQ2OWM1Mjc0ZjEyMWQ2NWYx
11
+ NTdkN2IwNGY3MGVlZTVkMGJiMzZmNjlmZDZiNDViZWJhNDg2OTE=
12
12
  data.tar.gz: !binary |-
13
- OTk3MTgzOWE2YjI5NDA2OTBkYzdjYzgyNjYzZjY4MzQ5OGFiYWQyZTUyYmNh
14
- NWE5ZGI3ZmNkNjQ2M2FhNTJlNjU5MjNmNGExMDExYWJhYzk2N2RiOWViMjc1
15
- MmEwZDM1MDY2NTI3YWIzNjgyZTUxYzI1NGRlNTRjZDI2MDhiZDI=
13
+ NjE0NDg4NDBhZGI3N2U2YWFlODFjYjg5ODU4OWY3NzE3ZWNlMzExMTYwYzUx
14
+ ZDFkMWNlNzNiM2VjNjE1MmM5NzI3OGNmMzk0YTZmOWEyYTJhMTZiZTRiMDQ5
15
+ MGI4NThlMTcwNDcwZGMxODQ5ZWE5MmQ3NTM0NzI3ODkxMDI2NzI=
@@ -2,6 +2,7 @@ class CreateAddresses < ActiveRecord::Migration
2
2
  def up
3
3
  create_table :addresses do |t|
4
4
  t.string :street
5
+ t.string :city
5
6
  t.integer :post_id
6
7
  t.timestamps
7
8
  end
@@ -49,12 +49,13 @@ module ObjectAttorney
49
49
  end
50
50
 
51
51
  def validate_nested_objects
52
- return true if nested_objects.map do |reflection, nested_object|
52
+ valid = nested_objects.map do |reflection, nested_object|
53
53
  nested_object.marked_for_destruction? ? true : nested_object.valid?
54
54
  end.all?
55
55
 
56
- import_nested_objects_errors
57
- false
56
+ import_nested_objects_errors unless valid
57
+
58
+ valid
58
59
  end
59
60
 
60
61
  def import_nested_objects_errors
@@ -86,7 +87,7 @@ module ObjectAttorney
86
87
  end
87
88
 
88
89
  def attributes_without_destroy(attributes)
89
- return nil unless attributes.kind_of?(Hash)
90
+ return nil unless attributes.is_a?(Hash)
90
91
 
91
92
  _attributes = attributes.dup
92
93
  _attributes.delete("_destroy")
@@ -99,13 +100,33 @@ module ObjectAttorney
99
100
  nested_instance_variable = self.instance_variable_get("@#{nested_object_name}")
100
101
 
101
102
  if nested_instance_variable.nil?
102
- nested_instance_variable = get_existing_and_new_nested_objects(nested_object_name)
103
+ reflection = self.class.reflect_on_association(nested_object_name)
104
+
105
+ nested_instance_variable = reflection.has_many? ? get_existing_and_new_nested_objects(nested_object_name) : get_existing_or_new_nested_object(nested_object_name)
106
+
103
107
  self.instance_variable_set("@#{nested_object_name}", nested_instance_variable)
104
108
  end
105
109
 
106
110
  nested_instance_variable
107
111
  end
108
112
 
113
+ def get_existing_or_new_nested_object(nested_object_name)
114
+ nested_object = send("existing_#{nested_object_name}")
115
+ attributes = send("#{nested_object_name}_attributes")
116
+
117
+ if nested_object.present?
118
+ return nested_object if (attributes["id"] || attributes[:id]).to_s != nested_object.id.to_s
119
+
120
+ nested_object.assign_attributes(attributes_without_destroy(attributes))
121
+ mark_for_destruction_if_necessary(nested_object, attributes)
122
+ else
123
+ nested_object = send("build_#{nested_object_name.to_s.singularize}", attributes_without_destroy(attributes))
124
+ mark_for_destruction_if_necessary(nested_object, attributes)
125
+ end
126
+
127
+ nested_object
128
+ end
129
+
109
130
  def get_existing_and_new_nested_objects(nested_object_name)
110
131
  existing_and_new_nested_objects = []
111
132
 
@@ -19,7 +19,7 @@ module ObjectAttorney
19
19
 
20
20
  def save!(save_method = :save!)
21
21
  before_save
22
- save_result = valid? ? save_after_validations(save_method) : false
22
+ save_result = valid? ? submit(save_method) : false
23
23
  after_save if valid? && save_result
24
24
  save_result
25
25
  end
@@ -43,10 +43,6 @@ module ObjectAttorney
43
43
  def before_save; end
44
44
  def after_save; end
45
45
 
46
- def save_after_validations(save_method)
47
- submit(save_method)
48
- end
49
-
50
46
  def submit(save_method)
51
47
  save_result = save_or_destroy_nested_objects(save_method, :belongs_to)
52
48
  save_result = save_or_destroy_represented_object(save_method) if save_result
@@ -9,7 +9,7 @@ module ObjectAttorney
9
9
 
10
10
  def save!(options = {}, save_method = :save!)
11
11
  before_save
12
- save_result = valid? ? save_after_validations(save_method, options) : false
12
+ save_result = valid? ? submit(save_method, options) : false
13
13
  after_save if valid? && save_result
14
14
  save_result
15
15
  end
@@ -30,10 +30,6 @@ module ObjectAttorney
30
30
 
31
31
  protected #################### PROTECTED METHODS DOWN BELOW ######################
32
32
 
33
- def save_after_validations(save_method, options = {})
34
- submit(save_method, options)
35
- end
36
-
37
33
  def submit(save_method, options = {})
38
34
  save_result = save_or_destroy_nested_objects(save_method, :belongs_to, options)
39
35
  save_result = save_or_destroy_represented_object(save_method, options) if save_result
@@ -1,3 +1,3 @@
1
1
  module ObjectAttorney
2
- VERSION = "2.1.3"
2
+ VERSION = "2.2.1"
3
3
  end
@@ -12,7 +12,8 @@ describe PostWithCommentsAndAddressForm do
12
12
  "1" => { body: "" }
13
13
  },
14
14
  address_attributes: {
15
- '0' => { street: 'street' }
15
+ street: 'street',
16
+ city: 'city'
16
17
  }
17
18
  }
18
19
  }
@@ -40,6 +41,7 @@ describe PostWithCommentsAndAddressForm do
40
41
  address = Address.first
41
42
  address.post_id.should == post.id
42
43
  address.street.should == 'street'
44
+ address.city.should == 'city'
43
45
  end
44
46
 
45
47
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_attorney
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.3
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Gonçalves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-02 00:00:00.000000000 Z
11
+ date: 2014-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler