object_attorney 2.2.13 → 2.4.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/nested_objects.rb +0 -1
- data/lib/object_attorney/orm.rb +38 -7
- data/lib/object_attorney/orm_handlers/smooth_operator.rb +12 -7
- data/lib/object_attorney/version.rb +1 -1
- data/lib/object_attorney.rb +16 -5
- data/spec/object_attorney/user_validations_form_spec.rb +30 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/form_objects/user_validations_form.rb +16 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjViM2Y4OGE1ODBmYjU1ODZlMzI2Y2UxNTVkOTg3MWQ1NmI1YzMxMQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZWU0OWJhYTViMGQwNjkxNzlhYWM3NjQ5MzY2NjUxYjczY2RjMDlmMw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZDIyNjczOTM4MjZlMTVmZDEyZDUyNGViOTEyOWNlMDU5YTE1MjdkMGZmNDkw
|
10
|
+
MmM1OTZkN2YxMTY1YjQ3MzNhNDFkYjM1ZmIzYWMxZTg0Yjk3ODlhMjU5YTk4
|
11
|
+
NmI4N2Y1NDVkMjZjMWVhOWM1MGQ2ZjIzOWU5OGM4YjQ2YzA5YTk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZTZmN2NlNTllY2JhYzdmY2VhZGE4ZjZjZWM5ODVkNDhjYTdjNTcyOThmZWIw
|
14
|
+
MDU4OGRkMGU1MjY2MzlhMzFhNzMzMzg5NTUwZDA5MzZhMTQwOGQxMzg0ODRk
|
15
|
+
ZjhjNWRmYWIwYWUxOTc3Y2E1MjgyNjdhZTE3NmUyYWQ5YjYxZTk=
|
data/lib/object_attorney/orm.rb
CHANGED
@@ -14,14 +14,19 @@ module ObjectAttorney
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def save
|
17
|
-
|
17
|
+
save_or_! { submit }
|
18
18
|
end
|
19
19
|
|
20
20
|
def save!(save_method = :save!)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
save_or_! { submit! }
|
22
|
+
end
|
23
|
+
|
24
|
+
def submit
|
25
|
+
submit_or_!(:save)
|
26
|
+
end
|
27
|
+
|
28
|
+
def submit!
|
29
|
+
submit_or_!(:save!)
|
25
30
|
end
|
26
31
|
|
27
32
|
def destroy
|
@@ -38,14 +43,40 @@ module ObjectAttorney
|
|
38
43
|
end
|
39
44
|
end
|
40
45
|
|
46
|
+
def clear_imported_errors
|
47
|
+
@imported_errors = {}
|
48
|
+
end
|
49
|
+
|
50
|
+
def populate_imported_errors
|
51
|
+
represented_object.errors.each { |key, value| @imported_errors[key] = value } if represented_object.present?
|
52
|
+
|
53
|
+
nested_objects.map do |reflection, nested_object|
|
54
|
+
nested_object.clear_imported_errors_and_import_new if nested_object.respond_to?(:clear_imported_errors_and_import_new)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
41
58
|
protected #################### PROTECTED METHODS DOWN BELOW ######################
|
42
59
|
|
60
|
+
def save_or_!
|
61
|
+
clear_imported_errors
|
62
|
+
|
63
|
+
before_save
|
64
|
+
|
65
|
+
save_result = valid? ? yield : false
|
66
|
+
|
67
|
+
populate_imported_errors
|
68
|
+
|
69
|
+
after_save if valid? && save_result
|
70
|
+
|
71
|
+
save_result
|
72
|
+
end
|
73
|
+
|
43
74
|
def before_save; end
|
44
75
|
def after_save; end
|
45
76
|
|
46
|
-
def
|
77
|
+
def submit_or_!(save_method)
|
47
78
|
save_result = save_or_destroy_nested_objects(save_method, :belongs_to)
|
48
|
-
save_result = save_or_destroy_represented_object(save_method)
|
79
|
+
save_result = save_or_destroy_represented_object(save_method)
|
49
80
|
save_result = save_or_destroy_nested_objects(save_method, :has_many) if save_result
|
50
81
|
save_result = save_or_destroy_nested_objects(save_method, :has_one) if save_result
|
51
82
|
save_result
|
@@ -4,14 +4,19 @@ module ObjectAttorney
|
|
4
4
|
module SmoothOperator
|
5
5
|
|
6
6
|
def save(options = {})
|
7
|
-
|
7
|
+
save_or_! { submit(options) }
|
8
8
|
end
|
9
9
|
|
10
|
-
def save!(options = {}
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
def save!(options = {})
|
11
|
+
save_or_! { submit!(options) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def submit(options = {})
|
15
|
+
submit_or_!(:save, options)
|
16
|
+
end
|
17
|
+
|
18
|
+
def submit!(options = {})
|
19
|
+
submit_or_!(:save!, options)
|
15
20
|
end
|
16
21
|
|
17
22
|
def destroy(options = {})
|
@@ -30,7 +35,7 @@ module ObjectAttorney
|
|
30
35
|
|
31
36
|
protected #################### PROTECTED METHODS DOWN BELOW ######################
|
32
37
|
|
33
|
-
def
|
38
|
+
def submit_or_!(save_method, options = {})
|
34
39
|
save_result = save_or_destroy_nested_objects(save_method, :belongs_to, options)
|
35
40
|
save_result = save_or_destroy_represented_object(save_method, options) if save_result
|
36
41
|
save_result = save_or_destroy_nested_objects(save_method, :has_many, options) if save_result
|
data/lib/object_attorney.rb
CHANGED
@@ -56,12 +56,22 @@ module ObjectAttorney
|
|
56
56
|
|
57
57
|
def validate_represented_object
|
58
58
|
valid = override_validations? ? true : Helpers.try_or_return(represented_object, :valid?, true)
|
59
|
-
|
59
|
+
|
60
|
+
incorporate_errors_from(represented_object.errors) unless valid
|
61
|
+
|
60
62
|
valid
|
61
63
|
end
|
62
64
|
|
63
|
-
def
|
64
|
-
|
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) }
|
65
75
|
end
|
66
76
|
|
67
77
|
def represented_object
|
@@ -79,9 +89,10 @@ module ObjectAttorney
|
|
79
89
|
include ObjectAttorney::ORM
|
80
90
|
|
81
91
|
validate :validate_represented_object
|
92
|
+
validate :validate_imported_errors
|
82
93
|
|
83
|
-
def valid?
|
84
|
-
override_validations? ? true : super
|
94
|
+
def valid?(context = nil)
|
95
|
+
override_validations? ? true : super(context)
|
85
96
|
end
|
86
97
|
end
|
87
98
|
|
@@ -0,0 +1,30 @@
|
|
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
|
data/spec/spec_helper.rb
CHANGED
@@ -26,6 +26,7 @@ require 'support/form_objects/bulk_posts_allow_only_existing_form'
|
|
26
26
|
require 'support/form_objects/bulk_posts_allow_only_new_form'
|
27
27
|
require 'support/form_objects/bulk_posts_with_form_objects_form'
|
28
28
|
require 'support/form_objects/user_form'
|
29
|
+
require 'support/form_objects/user_validations_form'
|
29
30
|
|
30
31
|
RSpec.configure do |config|
|
31
32
|
#config.treat_symbols_as_metadata_keys_with_true_values = true
|
@@ -0,0 +1,16 @@
|
|
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
|
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.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- João Gonçalves
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- spec/object_attorney/post_with_comment_form_spec.rb
|
91
91
|
- spec/object_attorney/post_with_comments_and_address_form_spec.rb
|
92
92
|
- spec/object_attorney/user_form_spec.rb
|
93
|
+
- spec/object_attorney/user_validations_form_spec.rb
|
93
94
|
- spec/spec_helper.rb
|
94
95
|
- spec/support/active_model/validations.rb
|
95
96
|
- spec/support/database_setup.rb
|
@@ -102,6 +103,7 @@ files:
|
|
102
103
|
- spec/support/form_objects/post_with_comment_form.rb
|
103
104
|
- spec/support/form_objects/post_with_comments_and_address_form.rb
|
104
105
|
- spec/support/form_objects/user_form.rb
|
106
|
+
- spec/support/form_objects/user_validations_form.rb
|
105
107
|
- spec/support/models/address.rb
|
106
108
|
- spec/support/models/comment.rb
|
107
109
|
- spec/support/models/post.rb
|
@@ -141,6 +143,7 @@ test_files:
|
|
141
143
|
- spec/object_attorney/post_with_comment_form_spec.rb
|
142
144
|
- spec/object_attorney/post_with_comments_and_address_form_spec.rb
|
143
145
|
- spec/object_attorney/user_form_spec.rb
|
146
|
+
- spec/object_attorney/user_validations_form_spec.rb
|
144
147
|
- spec/spec_helper.rb
|
145
148
|
- spec/support/active_model/validations.rb
|
146
149
|
- spec/support/database_setup.rb
|
@@ -153,8 +156,8 @@ test_files:
|
|
153
156
|
- spec/support/form_objects/post_with_comment_form.rb
|
154
157
|
- spec/support/form_objects/post_with_comments_and_address_form.rb
|
155
158
|
- spec/support/form_objects/user_form.rb
|
159
|
+
- spec/support/form_objects/user_validations_form.rb
|
156
160
|
- spec/support/models/address.rb
|
157
161
|
- spec/support/models/comment.rb
|
158
162
|
- spec/support/models/post.rb
|
159
163
|
- spec/support/models/user.rb
|
160
|
-
has_rdoc:
|