object_attorney 2.6.1 → 2.6.9

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
- NGI5MTNkZDE0MmUyOWMyZTFmMjgwZWVmMDgyMGY3YjIyZGRjMzFiOA==
4
+ N2JiOWE3MWRlMDhmY2UwYzA2OTgwYmI4MTI0NDhkMzc4ZTkyNjBjYw==
5
5
  data.tar.gz: !binary |-
6
- MzU5Y2EwOWY5MzA5NzE5ZWEyZDA5NjdlMTc3Y2U3N2FlNGU3MmU2MQ==
6
+ MDM5ZGQyYzg4Yzg0NTkyY2I3NzY3OWU0YTk0ZGM0MzIyZWU2MDU3MQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ODMwNmIzMjJiZWY0NGYxZjlhMjE1NzJlZmYzOTNhODE4M2VlMmFkNzU3YTA2
10
- Mjg5MzczMjMwYmI5YmE0NmQ5M2FiMTJlODQ4MWM2OWQ4YjlmYmFiN2Q3Njgz
11
- NWFkZTU3OWE5Njk5ODk4NjUzMDNhYmM0NDE4MzA3YWNhYmM4YTc=
9
+ NzFiNGU0Yjg0ODBkYjdjZWUzMzUzOGM5ODAxNDM0NzE5YTY5ODkzMWU1OGEz
10
+ ZDM1OWNiNjI5NmQ4MjZjYzE5MTM0ZGYxMDc1ZTM1ZDQ5OGU3NzU2NmRmMWZk
11
+ NTM3ZTQ0ZGUyODM1YjAwNzA4ODE2NzIwNzljNDMxZmFmMjlhYjE=
12
12
  data.tar.gz: !binary |-
13
- OThjNGRhYThjZmU2NWQ4OGU2ZGY5YzU5MzVkODNiMDViYjM5OGY4N2I5OGQ3
14
- OTc3YjRhNWMyMTEwNTFmMWUyYjhhN2ZjNTMyNDEyMzM1MThkMjc5YjExMTE1
15
- OWNkZmUzMDFhZDU0OTdiMTk4ZDk1NzM4ZGFjMTZhZTExOTg1OTU=
13
+ YTZkYWY4NmUzMDAxZjBiZmE5MWVjNDdiMGM3YjAzNzFhMzAyZmI1NmRiZDM0
14
+ ODE1NjcyOWJlZmVlZjYzYTdiYmM5MjljOTNiMjRkM2ViZTk0YjAwNzkxNDk3
15
+ MjIxNDE2MWY3MDIxZDVhYjcyMzIzYjQ0MGE5NzlhMjAwMTk5ODk=
@@ -22,6 +22,8 @@ module ObjectAttorney
22
22
 
23
23
  attributes = {} if attributes.blank?
24
24
 
25
+ attributes.symbolize_keys!
26
+
25
27
  [attributes, object]
26
28
  end
27
29
 
@@ -153,6 +153,8 @@ module ObjectAttorney
153
153
 
154
154
  def build_nested_object(nested_object_name, attributes = {})
155
155
  reflection = self.class.reflect_on_association(nested_object_name)
156
+
157
+ return nil if reflection.options[:no_new_records]
156
158
 
157
159
  if can_represented_object_build_nested?(reflection, nested_object_name)
158
160
  new_nested_object = build_from_represented_object(reflection, nested_object_name)
@@ -197,9 +199,11 @@ module ObjectAttorney
197
199
  def existing_nested_objects(nested_object_name)
198
200
  nested_relection = self.class.reflect_on_association(nested_object_name)
199
201
 
200
- existing = represented_object.blank? ? nested_relection.klass.all : (represented_object.send(nested_object_name) || (nested_relection.has_many? ? [] : nil))
202
+ return [] if nested_relection.options[:no_existing_records]
203
+
204
+ existing = represented_object.nil? ? (nested_relection.klass.try(:all) || []) : (represented_object.send(nested_object_name) || (nested_relection.has_many? ? [] : nil))
201
205
 
202
- if represented_object.present? && nested_relection.klass != self.class.represented_object_class.reflect_on_association(nested_object_name).try(:klass)
206
+ if represented_object.present? && self.class.represented_object_class.respond_to?(:reflect_on_association) && nested_relection.klass != self.class.represented_object_class.reflect_on_association(nested_object_name).try(:klass)
203
207
  if existing.respond_to?(:map)
204
208
  existing = existing.map { |existing_nested_object| nested_relection.klass.new({}, existing_nested_object) }
205
209
  else
@@ -225,6 +229,7 @@ module ObjectAttorney
225
229
  end
226
230
 
227
231
  def accepts_nested_objects(nested_object_name, options = {})
232
+ options.symbolize_keys!
228
233
  reflection = AssociationReflection.new(nested_object_name, represented_object_reflection, options)
229
234
 
230
235
  self.instance_variable_set("@#{nested_object_name}_reflection", reflection)
@@ -103,7 +103,11 @@ module ObjectAttorney
103
103
  module ClassMethods
104
104
 
105
105
  def all(*args)
106
- represented_object_class.all(*args).map { |represented_object| self.new({}, represented_object) }
106
+ if represented_object_class.respond_to?(:all)
107
+ represented_object_class.all(*args).map { |represented_object| self.new({}, represented_object) }
108
+ else
109
+ []
110
+ end
107
111
  end
108
112
 
109
113
  end
@@ -61,12 +61,11 @@ module ObjectAttorney
61
61
 
62
62
  delegate_properties(*options[:properties], to: represented_object_name) if options.include?(:properties)
63
63
 
64
- delegate(*options[:readers], to: represented_object_name) if options.include?(:readers)
64
+ initiate_getters(options[:getter], represented_object_name)
65
+ initiate_getters(options[:getters], represented_object_name)
65
66
 
66
- if options.include?(:writers)
67
- writers = options[:writers].map { |writer| "#{writer}=" }
68
- delegate(*writers, to: represented_object_name)
69
- end
67
+ initiate_setters(options[:setter], represented_object_name)
68
+ initiate_setters(options[:setters], represented_object_name)
70
69
  end
71
70
 
72
71
  def represented_object_reflection
@@ -83,6 +82,21 @@ module ObjectAttorney
83
82
  represented_object_class.reflect_on_association(association)
84
83
  end
85
84
 
85
+
86
+ private ############### PRIVATE #################
87
+
88
+ def initiate_getters(getters, represented_object_name)
89
+ delegate(*getters, to: represented_object_name) unless getters.nil?
90
+ end
91
+
92
+ def initiate_setters(setters, represented_object_name)
93
+ return nil if setters.nil?
94
+
95
+ setters = [*setters].map { |setter| "#{setter}=" }
96
+
97
+ delegate(*setters, to: represented_object_name)
98
+ end
99
+
86
100
  end
87
101
 
88
102
  end
@@ -22,9 +22,9 @@ module ObjectAttorney
22
22
 
23
23
  def populate_imposed_errors
24
24
  if respond_to?(:represented_object)
25
- represented_object.errors.each { |key, value| @imposed_errors[key] = value } if Helpers.has_errors_method?(represented_object)
25
+ represented_object.errors.each { |key, value| imposed_errors[key] = value } if Helpers.has_errors_method?(represented_object)
26
26
  else
27
- errors.each { |key, value| @imposed_errors[key] = value } if Helpers.has_errors_method?(self)
27
+ errors.each { |key, value| imposed_errors[key] = value } if Helpers.has_errors_method?(self)
28
28
  end
29
29
  end
30
30
 
@@ -35,7 +35,11 @@ module ObjectAttorney
35
35
  private #################### PRIVATE METHODS DOWN BELOW ######################
36
36
 
37
37
  def load_errors_from(errors)
38
- errors.each { |key, value| self.errors.add(key, value) }
38
+ errors.each do |key, value|
39
+ [*value].each do |_value|
40
+ self.errors.add(key, _value) unless self.errors.added?(key, _value)
41
+ end
42
+ end
39
43
  end
40
44
 
41
45
  end
@@ -1,3 +1,3 @@
1
1
  module ObjectAttorney
2
- VERSION = "2.6.1"
2
+ VERSION = "2.6.9"
3
3
  end
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe PostValidationsForm do
4
4
 
5
- it "1. 'PostValidationsForm' becomes invalid if 'Post' has errors after the #submit method and incorporates its errors.", current: true do
5
+ it "1. 'PostValidationsForm' becomes invalid if 'Post' has errors after the #submit method and incorporates its errors." do
6
6
  params = {
7
7
  post: {
8
8
  title: 'First post',
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe PostWithCommentValidationsForm do
4
4
 
5
- it "1. 'PostWithCommentValidationsForm' becomes invalid if 'Post' or nested 'Comment's has errors after the #submit method and incorporates its errors.", current: true do
5
+ it "1. 'PostWithCommentValidationsForm' becomes invalid if 'Post' or nested 'Comment's has errors after the #submit method and incorporates its errors." do
6
6
  params = {
7
7
  post: {
8
8
  title: 'First post',
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ describe PostWithOnlyExistingCommentsForm do
4
+
5
+ it "1. Editing nested 'Comments' inside a PostForm that only accepts existing Comments changes and ignores new Comments." do
6
+ params = {
7
+ id: 1,
8
+ post: {
9
+ title: "altered post",
10
+ comments_attributes: {
11
+ "0" => { body: "new comment" },
12
+ "1" => { id: 1, _destroy: true },
13
+ "2" => { id: 2, body: 'altered comment' }
14
+ }
15
+ }
16
+ }
17
+
18
+ Post.create(title: "My title1")
19
+ Post.first.title.should == 'My title1'
20
+ Comment.create(post_id: 1, body: "body1")
21
+ Comment.create(post_id: 1, body: "body2")
22
+ Comment.all.count.should == 2
23
+
24
+ post_form = PostWithOnlyExistingCommentsForm.new(params[:post], Post.find(params[:id]))
25
+ post_form.save
26
+
27
+ comment = Comment.find_by_id(1)
28
+ comment.should == nil
29
+
30
+ comment = Comment.find_by_id(2)
31
+ comment.body.should == 'altered comment'
32
+
33
+ Comment.all.count.should == 1
34
+ Post.first.title.should == 'altered post'
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ describe PostWithOnlyNewCommentsForm do
4
+
5
+ it "1. Creating nested 'Comments' inside a PostForm that only accepts new Comments and ignores existing Comments changes." do
6
+ params = {
7
+ id: 1,
8
+ post: {
9
+ title: "altered post",
10
+ comments_attributes: {
11
+ "0" => { body: "new comment" },
12
+ "1" => { id: 1, _destroy: true },
13
+ "2" => { id: 2, body: 'altered comment' }
14
+ }
15
+ }
16
+ }
17
+
18
+ Post.create(title: "My title1")
19
+ Post.first.title.should == 'My title1'
20
+ Comment.create(post_id: 1, body: "body1")
21
+ Comment.create(post_id: 1, body: "body2")
22
+ Comment.all.count.should == 2
23
+
24
+ post_form = PostWithOnlyNewCommentsForm.new(params[:post], Post.find(params[:id]))
25
+ post_form.save
26
+
27
+ comment = Comment.find_by_id(1)
28
+ comment.body.should == 'body1'
29
+
30
+ comment = Comment.find_by_id(2)
31
+ comment.body.should == 'body2'
32
+
33
+ comment = Comment.find_by_id(3)
34
+ comment.body.should == 'new comment'
35
+ comment.post_id.should == 1
36
+
37
+ Comment.all.count.should == 3
38
+ Post.first.title.should == 'altered post'
39
+
40
+ end
41
+
42
+ end
data/spec/spec_helper.rb CHANGED
@@ -23,6 +23,8 @@ require 'support/form_objects/comment_form'
23
23
  require 'support/form_objects/post_with_comment_form'
24
24
  require 'support/form_objects/post_with_comment_validations_form'
25
25
  require 'support/form_objects/post_with_comments_and_address_form'
26
+ require 'support/form_objects/post_with_only_new_comments_form'
27
+ require 'support/form_objects/post_with_only_existing_comments_form'
26
28
  require 'support/form_objects/bulk_posts_form'
27
29
  require 'support/form_objects/bulk_posts_allow_only_existing_form'
28
30
  require 'support/form_objects/bulk_posts_allow_only_new_form'
@@ -0,0 +1,9 @@
1
+ class PostWithOnlyExistingCommentsForm
2
+
3
+ include ObjectAttorney
4
+
5
+ represents :post, properties: [:title, :body]
6
+
7
+ has_many :comments, no_new_records: true
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ class PostWithOnlyNewCommentsForm
2
+
3
+ include ObjectAttorney
4
+
5
+ represents :post, properties: [:title, :body]
6
+
7
+ has_many :comments, no_existing_records: true
8
+
9
+ 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.6.1
4
+ version: 2.6.9
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-31 00:00:00.000000000 Z
11
+ date: 2014-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,6 +97,8 @@ files:
97
97
  - spec/object_attorney/post_with_comment_form_spec.rb
98
98
  - spec/object_attorney/post_with_comment_validations_form_spec.rb
99
99
  - spec/object_attorney/post_with_comments_and_address_form_spec.rb
100
+ - spec/object_attorney/post_with_only_existing_comments_form_spec.rb
101
+ - spec/object_attorney/post_with_only_new_comments_form_spec.rb
100
102
  - spec/object_attorney/user_form_spec.rb
101
103
  - spec/object_attorney/user_form_with_ruby_errors_spec.rb
102
104
  - spec/spec_helper.rb
@@ -112,6 +114,8 @@ files:
112
114
  - spec/support/form_objects/post_with_comment_form.rb
113
115
  - spec/support/form_objects/post_with_comment_validations_form.rb
114
116
  - spec/support/form_objects/post_with_comments_and_address_form.rb
117
+ - spec/support/form_objects/post_with_only_existing_comments_form.rb
118
+ - spec/support/form_objects/post_with_only_new_comments_form.rb
115
119
  - spec/support/form_objects/user_form.rb
116
120
  - spec/support/form_objects/user_form_with_ruby_errors.rb
117
121
  - spec/support/models/address.rb
@@ -154,6 +158,8 @@ test_files:
154
158
  - spec/object_attorney/post_with_comment_form_spec.rb
155
159
  - spec/object_attorney/post_with_comment_validations_form_spec.rb
156
160
  - spec/object_attorney/post_with_comments_and_address_form_spec.rb
161
+ - spec/object_attorney/post_with_only_existing_comments_form_spec.rb
162
+ - spec/object_attorney/post_with_only_new_comments_form_spec.rb
157
163
  - spec/object_attorney/user_form_spec.rb
158
164
  - spec/object_attorney/user_form_with_ruby_errors_spec.rb
159
165
  - spec/spec_helper.rb
@@ -169,6 +175,8 @@ test_files:
169
175
  - spec/support/form_objects/post_with_comment_form.rb
170
176
  - spec/support/form_objects/post_with_comment_validations_form.rb
171
177
  - spec/support/form_objects/post_with_comments_and_address_form.rb
178
+ - spec/support/form_objects/post_with_only_existing_comments_form.rb
179
+ - spec/support/form_objects/post_with_only_new_comments_form.rb
172
180
  - spec/support/form_objects/user_form.rb
173
181
  - spec/support/form_objects/user_form_with_ruby_errors.rb
174
182
  - spec/support/models/address.rb