object_attorney 2.4.0 → 2.5.0
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/imported_errors.rb +33 -0
- data/lib/object_attorney/nested_objects.rb +7 -1
- data/lib/object_attorney/orm.rb +17 -9
- data/lib/object_attorney/version.rb +1 -1
- data/lib/object_attorney.rb +2 -12
- data/spec/object_attorney/post_form_spec.rb +11 -4
- data/spec/object_attorney/post_validations_form_spec.rb +54 -0
- data/spec/object_attorney/post_with_comment_validations_form_spec.rb +42 -0
- data/spec/object_attorney/post_with_comments_and_address_form_spec.rb +1 -0
- data/spec/spec_helper.rb +2 -1
- data/spec/support/form_objects/post_validations_form.rb +17 -0
- data/spec/support/form_objects/post_with_comment_validations_form.rb +17 -0
- metadata +10 -5
- data/spec/object_attorney/user_validations_form_spec.rb +0 -30
- data/spec/support/form_objects/user_validations_form.rb +0 -16
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MGMwYTYwMmZhY2E1MDNmYTk3ZTdmY2RmZmFiYThjZjRiYzkzN2FmYg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZDczNTc0ZjcxNmM2NGQ0ZjU0MmI2OWFiMWUzZDZhMjVhYTEwMTY5Ng==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZWMzZTMyZDIwNjY1YzM2OTgzOTc3MWU1NzM2ZWJiZjBjN2FjOGUyMjY2NGUy
|
10
|
+
Y2U1MjZjOTE5ZDA5ZjY3OTMyNDA5NjIyNGQzY2E3ZTE4Yzg3MThiYTlhZmQw
|
11
|
+
ZTkxYTU4MzNjYTA4YjdkYjA5OWE0MDAzOWViOTZkNmQ1MTYxYmU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZGFmMzQ0ZGM1NWUyMzFiYTYwYjRiYWRiYTcxNDhmZGQxMzVmZjFhMzU3N2Jm
|
14
|
+
MGM4NDdlMzg2NWVjN2EzZTBiMTE4ZjM1ZjRmMjE2MTVlYzM4OWRjN2IwMjJl
|
15
|
+
NjExZmE5MDQ5YTFjNzU3M2ZjY2FjNzM1ZGZlNTE0MzVkYjc2YTM=
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ObjectAttorney
|
2
|
+
module ImportedErrors
|
3
|
+
|
4
|
+
protected #################### PROTECTED METHODS DOWN BELOW ######################
|
5
|
+
|
6
|
+
def clear_imported_errors
|
7
|
+
@imported_errors = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def populate_imported_errors
|
11
|
+
if respond_to?(:represented_object)
|
12
|
+
represented_object.errors.each { |key, value| @imported_errors[key] = value } if represented_object.present?
|
13
|
+
else
|
14
|
+
errors.each { |key, value| @imported_errors[key] = value }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def validate_imported_errors
|
19
|
+
imported_errors = (@imported_errors || {})
|
20
|
+
|
21
|
+
incorporate_errors_from imported_errors
|
22
|
+
|
23
|
+
imported_errors.empty?
|
24
|
+
end
|
25
|
+
|
26
|
+
private #################### PRIVATE METHODS DOWN BELOW ######################
|
27
|
+
|
28
|
+
def incorporate_errors_from(errors)
|
29
|
+
errors.each { |key, value| self.errors.add(key, value) }
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -110,6 +110,12 @@ module ObjectAttorney
|
|
110
110
|
|
111
111
|
nested_instance_variable = reflection.has_many? ? get_existing_and_new_nested_objects(nested_object_name) : get_existing_or_new_nested_object(nested_object_name)
|
112
112
|
|
113
|
+
[*nested_instance_variable].each do |nested_object|
|
114
|
+
nested_object.extend(ImportedErrors)
|
115
|
+
|
116
|
+
# nested_object.singleton_class.validate(:validate_imported_errors)
|
117
|
+
end
|
118
|
+
|
113
119
|
self.instance_variable_set("@#{nested_object_name}", nested_instance_variable)
|
114
120
|
end
|
115
121
|
|
@@ -168,7 +174,6 @@ module ObjectAttorney
|
|
168
174
|
def build_nested_object(nested_object_name, attributes = {})
|
169
175
|
reflection = self.class.reflect_on_association(nested_object_name)
|
170
176
|
|
171
|
-
|
172
177
|
if can_represented_object_build_nested?(reflection, nested_object_name)
|
173
178
|
new_nested_object = build_from_represented_object(reflection, nested_object_name)
|
174
179
|
|
@@ -187,6 +192,7 @@ module ObjectAttorney
|
|
187
192
|
|
188
193
|
if reflection.klass == real_reflection_class
|
189
194
|
new_nested_object.assign_attributes(attributes)
|
195
|
+
new_nested_object
|
190
196
|
else
|
191
197
|
reflection.klass.respond_to?(:represents) ? reflection.klass.new(attributes, new_nested_object) : reflection.klass.new(attributes)
|
192
198
|
end
|
data/lib/object_attorney/orm.rb
CHANGED
@@ -43,15 +43,21 @@ module ObjectAttorney
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
46
|
+
def clear_nested_imported_errors
|
47
|
+
nested_objects.each do |reflection, nested_object|
|
48
|
+
nested_object.clear_imported_errors
|
49
49
|
|
50
|
-
|
51
|
-
|
50
|
+
nested_object.clear_nested_imported_errors if nested_object.respond_to?(:clear_nested_imported_errors)
|
51
|
+
end
|
52
|
+
end
|
52
53
|
|
53
|
-
|
54
|
-
|
54
|
+
def populate_nested_imported_errors
|
55
|
+
nested_objects.each do |reflection, nested_object|
|
56
|
+
next if nested_object.marked_for_destruction?
|
57
|
+
|
58
|
+
nested_object.populate_imported_errors
|
59
|
+
|
60
|
+
nested_object.populate_nested_imported_errors if nested_object.respond_to?(:populate_nested_imported_errors)
|
55
61
|
end
|
56
62
|
end
|
57
63
|
|
@@ -59,12 +65,14 @@ module ObjectAttorney
|
|
59
65
|
|
60
66
|
def save_or_!
|
61
67
|
clear_imported_errors
|
62
|
-
|
63
|
-
|
68
|
+
|
69
|
+
clear_imported_errors
|
70
|
+
clear_nested_imported_errors
|
64
71
|
|
65
72
|
save_result = valid? ? yield : false
|
66
73
|
|
67
74
|
populate_imported_errors
|
75
|
+
populate_nested_imported_errors
|
68
76
|
|
69
77
|
after_save if valid? && save_result
|
70
78
|
|
data/lib/object_attorney.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "object_attorney/version"
|
2
2
|
require "object_attorney/helpers"
|
3
3
|
require "object_attorney/reflection"
|
4
|
+
require "object_attorney/imported_errors"
|
4
5
|
require "object_attorney/nested_objects"
|
5
6
|
require "object_attorney/orm"
|
6
7
|
require 'active_record'
|
@@ -62,18 +63,6 @@ module ObjectAttorney
|
|
62
63
|
valid
|
63
64
|
end
|
64
65
|
|
65
|
-
def validate_imported_errors
|
66
|
-
imported_errors = (@imported_errors || {})
|
67
|
-
|
68
|
-
incorporate_errors_from imported_errors
|
69
|
-
|
70
|
-
imported_errors.empty?
|
71
|
-
end
|
72
|
-
|
73
|
-
def incorporate_errors_from(errors)
|
74
|
-
errors.each { |key, value| self.errors.add(key, value) }
|
75
|
-
end
|
76
|
-
|
77
66
|
def represented_object
|
78
67
|
@represented_object ||= self.class.represented_object_class.try(:new)
|
79
68
|
end
|
@@ -85,6 +74,7 @@ module ObjectAttorney
|
|
85
74
|
include ActiveModel::Validations
|
86
75
|
include ActiveModel::Validations::Callbacks
|
87
76
|
include ActiveModel::Conversion
|
77
|
+
include ObjectAttorney::ImportedErrors
|
88
78
|
include ObjectAttorney::NestedObjects
|
89
79
|
include ObjectAttorney::ORM
|
90
80
|
|
@@ -17,7 +17,8 @@ shared_examples "a PostForm" do
|
|
17
17
|
post_form = described_class.new(params[:post])
|
18
18
|
|
19
19
|
post_form.save.should == true
|
20
|
-
|
20
|
+
post_form.comments.length.should == 2
|
21
|
+
|
21
22
|
Post.all.count.should == 1
|
22
23
|
post = Post.first
|
23
24
|
post.title.should == 'First post'
|
@@ -47,7 +48,9 @@ shared_examples "a PostForm" do
|
|
47
48
|
Comment.first.body.should == 'body1'
|
48
49
|
|
49
50
|
post_form = described_class.new(params[:post], Post.find(params[:id]))
|
50
|
-
|
51
|
+
|
52
|
+
post_form.save.should == true
|
53
|
+
post_form.comments.length.should == 1
|
51
54
|
|
52
55
|
post = Post.first
|
53
56
|
post.title.should == 'altered post'
|
@@ -74,7 +77,9 @@ shared_examples "a PostForm" do
|
|
74
77
|
Comment.all.count.should == 1
|
75
78
|
|
76
79
|
post_form = described_class.new(params[:post], Post.find(params[:id]))
|
77
|
-
|
80
|
+
|
81
|
+
post_form.save.should == true
|
82
|
+
post_form.comments.length.should == 1
|
78
83
|
|
79
84
|
Post.first.title.should == 'altered post'
|
80
85
|
Comment.all.count.should == 0
|
@@ -100,7 +105,9 @@ shared_examples "a PostForm" do
|
|
100
105
|
Comment.all.count.should == 2
|
101
106
|
|
102
107
|
post_form = described_class.new(params[:post], Post.find(params[:id]))
|
103
|
-
|
108
|
+
|
109
|
+
post_form.save.should == true
|
110
|
+
post_form.comments.length.should == 3
|
104
111
|
|
105
112
|
post = Post.first
|
106
113
|
post.title.should == 'altered post'
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe PostValidationsForm do
|
4
|
+
|
5
|
+
it "asd", current: true do
|
6
|
+
post1 = Post.new
|
7
|
+
post1.valid?.should == true
|
8
|
+
|
9
|
+
post1.singleton_class.validates_presence_of :title
|
10
|
+
# binding.pry
|
11
|
+
post1.valid?.should == false
|
12
|
+
|
13
|
+
post2 = Post.new
|
14
|
+
post2.valid?.should == true
|
15
|
+
end
|
16
|
+
|
17
|
+
it "1. 'PostValidationsForm' becomes invalid if 'Post' has errors after the #submit method and incorporates its errors." do
|
18
|
+
params = {
|
19
|
+
post: {
|
20
|
+
title: 'First post',
|
21
|
+
body: 'post body',
|
22
|
+
comments_attributes: {
|
23
|
+
"0" => { body: "body1" }
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
post_form = PostValidationsForm.new(params[:post])
|
29
|
+
post_form.valid?.should == true
|
30
|
+
|
31
|
+
post_form.save
|
32
|
+
|
33
|
+
post_form.should have(1).error_on(:title)
|
34
|
+
post_form.errors.size.should == 2
|
35
|
+
post_form.comments.first.should have(1).error_on(:body)
|
36
|
+
|
37
|
+
post_form.valid?.should == false
|
38
|
+
post_form.should have(1).error_on(:title)
|
39
|
+
post_form.errors.size.should == 2
|
40
|
+
post_form.comments.first.should have(1).error_on(:body)
|
41
|
+
|
42
|
+
post_form.save
|
43
|
+
|
44
|
+
post_form.should have(1).error_on(:title)
|
45
|
+
post_form.errors.size.should == 2
|
46
|
+
post_form.comments.first.should have(1).error_on(:body)
|
47
|
+
|
48
|
+
post_form.valid?.should == false
|
49
|
+
post_form.should have(1).error_on(:title)
|
50
|
+
post_form.errors.size.should == 2
|
51
|
+
post_form.comments.first.should have(1).error_on(:body)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe PostWithCommentValidationsForm do
|
4
|
+
|
5
|
+
it "1. 'PostWithCommentValidationsForm' becomes invalid if 'Post' or nested 'Comment's has errors after the #submit method and incorporates its errors." do
|
6
|
+
params = {
|
7
|
+
post: {
|
8
|
+
title: 'First post',
|
9
|
+
body: 'post body',
|
10
|
+
comments_attributes: {
|
11
|
+
"0" => { body: "body1" }
|
12
|
+
}
|
13
|
+
}
|
14
|
+
}
|
15
|
+
|
16
|
+
post_form = PostWithCommentValidationsForm.new(params[:post])
|
17
|
+
post_form.valid?.should == true
|
18
|
+
|
19
|
+
post_form.save
|
20
|
+
|
21
|
+
post_form.should have(1).error_on(:title)
|
22
|
+
post_form.errors.size.should == 2
|
23
|
+
post_form.comments.first.should have(1).error_on(:body)
|
24
|
+
|
25
|
+
post_form.valid?.should == false
|
26
|
+
post_form.should have(1).error_on(:title)
|
27
|
+
post_form.errors.size.should == 2
|
28
|
+
post_form.comments.first.should have(1).error_on(:body)
|
29
|
+
|
30
|
+
post_form.save
|
31
|
+
|
32
|
+
post_form.should have(1).error_on(:title)
|
33
|
+
post_form.errors.size.should == 2
|
34
|
+
post_form.comments.first.should have(1).error_on(:body)
|
35
|
+
|
36
|
+
post_form.valid?.should == false
|
37
|
+
post_form.should have(1).error_on(:title)
|
38
|
+
post_form.errors.size.should == 2
|
39
|
+
post_form.comments.first.should have(1).error_on(:body)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -18,15 +18,16 @@ require 'support/models/post'
|
|
18
18
|
require 'support/models/user'
|
19
19
|
|
20
20
|
require 'support/form_objects/post_form'
|
21
|
+
require 'support/form_objects/post_validations_form'
|
21
22
|
require 'support/form_objects/comment_form'
|
22
23
|
require 'support/form_objects/post_with_comment_form'
|
24
|
+
require 'support/form_objects/post_with_comment_validations_form'
|
23
25
|
require 'support/form_objects/post_with_comments_and_address_form'
|
24
26
|
require 'support/form_objects/bulk_posts_form'
|
25
27
|
require 'support/form_objects/bulk_posts_allow_only_existing_form'
|
26
28
|
require 'support/form_objects/bulk_posts_allow_only_new_form'
|
27
29
|
require 'support/form_objects/bulk_posts_with_form_objects_form'
|
28
30
|
require 'support/form_objects/user_form'
|
29
|
-
require 'support/form_objects/user_validations_form'
|
30
31
|
|
31
32
|
RSpec.configure do |config|
|
32
33
|
#config.treat_symbols_as_metadata_keys_with_true_values = true
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class PostValidationsForm
|
2
|
+
|
3
|
+
include ObjectAttorney
|
4
|
+
|
5
|
+
represents :post, properties: [:title, :body]
|
6
|
+
|
7
|
+
has_many :comments
|
8
|
+
|
9
|
+
validates_presence_of :title
|
10
|
+
|
11
|
+
def submit
|
12
|
+
post.errors.add(:title, :blank)
|
13
|
+
post.comments.first.errors.add(:body, :blank)
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class PostWithCommentValidationsForm
|
2
|
+
|
3
|
+
include ObjectAttorney
|
4
|
+
|
5
|
+
represents :post, properties: [:title, :body]
|
6
|
+
|
7
|
+
has_many :comments, class_name: CommentForm
|
8
|
+
|
9
|
+
validates_presence_of :title
|
10
|
+
|
11
|
+
def submit
|
12
|
+
post.errors.add(:title, :blank)
|
13
|
+
post.comments.first.errors.add(:body, :blank)
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
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.
|
4
|
+
version: 2.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- João Gonçalves
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- lib/object_attorney.rb
|
76
76
|
- lib/object_attorney/association_reflection.rb
|
77
77
|
- lib/object_attorney/helpers.rb
|
78
|
+
- lib/object_attorney/imported_errors.rb
|
78
79
|
- lib/object_attorney/nested_objects.rb
|
79
80
|
- lib/object_attorney/nested_uniqueness_validator.rb
|
80
81
|
- lib/object_attorney/orm.rb
|
@@ -87,10 +88,11 @@ files:
|
|
87
88
|
- spec/object_attorney/bulk_posts_allow_only_new_form_spec.rb
|
88
89
|
- spec/object_attorney/bulk_posts_with_form_objects_form_spec.rb
|
89
90
|
- spec/object_attorney/post_form_spec.rb
|
91
|
+
- spec/object_attorney/post_validations_form_spec.rb
|
90
92
|
- spec/object_attorney/post_with_comment_form_spec.rb
|
93
|
+
- spec/object_attorney/post_with_comment_validations_form_spec.rb
|
91
94
|
- spec/object_attorney/post_with_comments_and_address_form_spec.rb
|
92
95
|
- spec/object_attorney/user_form_spec.rb
|
93
|
-
- spec/object_attorney/user_validations_form_spec.rb
|
94
96
|
- spec/spec_helper.rb
|
95
97
|
- spec/support/active_model/validations.rb
|
96
98
|
- spec/support/database_setup.rb
|
@@ -100,10 +102,11 @@ files:
|
|
100
102
|
- spec/support/form_objects/bulk_posts_with_form_objects_form.rb
|
101
103
|
- spec/support/form_objects/comment_form.rb
|
102
104
|
- spec/support/form_objects/post_form.rb
|
105
|
+
- spec/support/form_objects/post_validations_form.rb
|
103
106
|
- spec/support/form_objects/post_with_comment_form.rb
|
107
|
+
- spec/support/form_objects/post_with_comment_validations_form.rb
|
104
108
|
- spec/support/form_objects/post_with_comments_and_address_form.rb
|
105
109
|
- spec/support/form_objects/user_form.rb
|
106
|
-
- spec/support/form_objects/user_validations_form.rb
|
107
110
|
- spec/support/models/address.rb
|
108
111
|
- spec/support/models/comment.rb
|
109
112
|
- spec/support/models/post.rb
|
@@ -140,10 +143,11 @@ test_files:
|
|
140
143
|
- spec/object_attorney/bulk_posts_allow_only_new_form_spec.rb
|
141
144
|
- spec/object_attorney/bulk_posts_with_form_objects_form_spec.rb
|
142
145
|
- spec/object_attorney/post_form_spec.rb
|
146
|
+
- spec/object_attorney/post_validations_form_spec.rb
|
143
147
|
- spec/object_attorney/post_with_comment_form_spec.rb
|
148
|
+
- spec/object_attorney/post_with_comment_validations_form_spec.rb
|
144
149
|
- spec/object_attorney/post_with_comments_and_address_form_spec.rb
|
145
150
|
- spec/object_attorney/user_form_spec.rb
|
146
|
-
- spec/object_attorney/user_validations_form_spec.rb
|
147
151
|
- spec/spec_helper.rb
|
148
152
|
- spec/support/active_model/validations.rb
|
149
153
|
- spec/support/database_setup.rb
|
@@ -153,10 +157,11 @@ test_files:
|
|
153
157
|
- spec/support/form_objects/bulk_posts_with_form_objects_form.rb
|
154
158
|
- spec/support/form_objects/comment_form.rb
|
155
159
|
- spec/support/form_objects/post_form.rb
|
160
|
+
- spec/support/form_objects/post_validations_form.rb
|
156
161
|
- spec/support/form_objects/post_with_comment_form.rb
|
162
|
+
- spec/support/form_objects/post_with_comment_validations_form.rb
|
157
163
|
- spec/support/form_objects/post_with_comments_and_address_form.rb
|
158
164
|
- spec/support/form_objects/user_form.rb
|
159
|
-
- spec/support/form_objects/user_validations_form.rb
|
160
165
|
- spec/support/models/address.rb
|
161
166
|
- spec/support/models/comment.rb
|
162
167
|
- spec/support/models/post.rb
|
@@ -1,30 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe UserValidationsForm do
|
4
|
-
|
5
|
-
it "1. 'UserValidationsForm' becomes invalid if 'User' has errors after the #submit method and incorporates its errors.", current: true do
|
6
|
-
user = User.new(email: "email@gmail.com")
|
7
|
-
user.valid?.should == true
|
8
|
-
|
9
|
-
user_form = UserValidationsForm.new(email: "email@gmail.com", terms_of_service: true)
|
10
|
-
user_form.valid?.should == true
|
11
|
-
|
12
|
-
user_form.save
|
13
|
-
user_form.should have(1).error_on(:email)
|
14
|
-
user_form.errors.size.should == 1
|
15
|
-
|
16
|
-
user_form.valid?.should == false
|
17
|
-
user_form.should have(1).error_on(:email)
|
18
|
-
user_form.errors.size.should == 1
|
19
|
-
|
20
|
-
user_form.save
|
21
|
-
|
22
|
-
user_form.should have(1).error_on(:email)
|
23
|
-
user_form.errors.size.should == 1
|
24
|
-
|
25
|
-
user_form.valid?.should == false
|
26
|
-
user_form.should have(1).error_on(:email)
|
27
|
-
user_form.errors.size.should == 1
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
class UserValidationsForm
|
2
|
-
|
3
|
-
include ObjectAttorney
|
4
|
-
|
5
|
-
represents :user, properties: [:email]
|
6
|
-
|
7
|
-
attr_accessor :terms_of_service
|
8
|
-
|
9
|
-
validates_acceptance_of :terms_of_service, accept: true, allow_nil: false
|
10
|
-
|
11
|
-
def submit
|
12
|
-
user.errors.add(:email, :blank)
|
13
|
-
false
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|