rest_my_case 1.10.6 → 1.10.7

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
- ZTE5NWY3NzA5ZDI4MThjZmM2MmY3ZGE2NDE3NDY0YTNiZGM2ZWM1Mg==
4
+ YmJmYmE1OGE0MGY2ZjA3MjMwOTE2ODQxYTIxYWZmM2E1M2IyZDkxOA==
5
5
  data.tar.gz: !binary |-
6
- NjZmZTUwYTY5NzZhMWFmNDljYTMyYzkwNjVlNTQ3OTRlYzg5YjlhOQ==
6
+ Zjk0NGQwMGRiNjRhODFmMDQ3MmM5N2RiODRlMThmMDBiYjljOGI1NA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NmM0OWE1NGZmYzZjZjgyYjQ1NzVkYWYzYjYzN2QyNmI5YTc4OWI4NmMwOTQw
10
- MWNhZTMzYTdhZWJiZDM1NzNlZTBmMDAzYmRhNmNhNzExZDJjYTZhMDBmYmE5
11
- NjI5Nzk5NWI5ODYyZGNlNzBhZmVjMDJhZTkwYzQxYzE4YjBhZTc=
9
+ ZjNkYTcxMGJhMzcxZDZlNjMwNDE5ZGQxMjQxMWJmMjNiODhiZGRlMjFiMzNm
10
+ MTdiODRkOTNjOTdhMTgyMmZmY2VlZTRjNTQ3ZGQyMzc2ZDY1ZjY0N2VjMDM0
11
+ NzNmMWMzMjQ0MzQ5NTAwYmZjMDg2MmVlOGE3ZmRjZGFiNWZjMWE=
12
12
  data.tar.gz: !binary |-
13
- NDc2ZmI4ZDAzOTA1NzJkNzBkMjhjNjBhNWVmZGJkMDNkZDRjZGMyMWYyZjdl
14
- ODg5MGViZTU4ZTEwZmNhOGM0ZmNmZDQ3MTNhYWJkMmE4ODNjNzEzNDQ3Njc5
15
- Y2YzNzM5ZjIzOWUyNjEwZTI3ZTQyNzc3ZTNiYzE1ZThlZjQ3MGI=
13
+ ZDViMmUzY2MyNzBiMTBkMjg5Y2Q5OTBhZGE0ZmExNmJjY2ZiYWY0ZDdhYzU4
14
+ OTFiM2I3OTk3ZDQ4MDI2MTE4ODU1OTEyMWEzMjEzMzU0OTk3ZjNkYjVhNDFj
15
+ NjAwOTZjYjk3NDRiMGM2M2YwMTE5NjE1ODViYmU3MjJlNDA5MmI=
@@ -11,12 +11,13 @@ module RestMyCase
11
11
  Judge::Base, DefenseAttorney::Base, Validator, Context::Base
12
12
  end
13
13
 
14
- def target_name
15
- @target_name || Helpers.super_method(self, :target_name)
14
+ def target_options
15
+ @target_options || Helpers.super_method(self, :target_options)
16
16
  end
17
17
 
18
- def target(target_name)
19
- @target_name = target_name
18
+ def target(target_name, target_options = {})
19
+ @target_options = target_options || {}
20
+ @target_options[:name] = target_name
20
21
  end
21
22
 
22
23
  def validators
@@ -62,26 +63,45 @@ module RestMyCase
62
63
 
63
64
  extend AccusationAttorneys::HelperMethods
64
65
 
65
- def target_name
66
- self.class.target_name
66
+ def target_options
67
+ self.class.target_options || {}
68
+ end
69
+
70
+ def parent_target
71
+ @parent_target ||=
72
+ target_options[:in] ? get_target(target_options[:in]) : nil
67
73
  end
68
74
 
69
75
  def target
70
- return nil if target_name.nil?
76
+ return nil if target_options[:name].nil?
71
77
 
72
- respond_to?(target_name) ? send(target_name) : context.send(target_name)
78
+ if parent_target
79
+ extend_errors_if_necessary(parent_target)
80
+
81
+ parent_target.send(target_options[:name])
82
+ else
83
+ get_target(target_options[:name])
84
+ end
73
85
  end
74
86
 
75
87
  def perform
76
88
  targets = [*target]
77
89
 
78
- return if Helpers.blank?(targets)
90
+ return if Helpers.blank?(targets) || all_validations_green?(targets)
79
91
 
80
- error('unprocessable_entity') unless all_validations_green? targets
92
+ if parent_target
93
+ parent_target.errors.add(target_options[:name], :invalid)
94
+ end
95
+
96
+ error('unprocessable_entity')
81
97
  end
82
98
 
83
99
  protected ######################## PROTECTED ###############################
84
100
 
101
+ def get_target(method)
102
+ respond_to?(method) ? send(method) : context.send(method)
103
+ end
104
+
85
105
  def all_validations_green?(targets)
86
106
  targets.map do |object_to_validate|
87
107
  extend_errors_if_necessary(object_to_validate)
@@ -1,5 +1,5 @@
1
1
  module RestMyCase
2
2
 
3
- VERSION = '1.10.6'
3
+ VERSION = '1.10.7'
4
4
 
5
5
  end
@@ -102,11 +102,11 @@ describe RestMyCase::Validator do
102
102
  expect(@context.ok?).to be true
103
103
  end
104
104
 
105
- it "@post1 should not cotain errors" do
105
+ it "@post1 should not contain errors" do
106
106
  expect(@post1.errors.count).to be 0
107
107
  end
108
108
 
109
- it "@post2 should not cotain errors" do
109
+ it "@post2 should not contain errors" do
110
110
  expect(@post2.errors.count).to be 0
111
111
  end
112
112
  end
@@ -122,11 +122,11 @@ describe RestMyCase::Validator do
122
122
  expect(@context.ok?).to be false
123
123
  end
124
124
 
125
- it "@post1 should not cotain errors" do
125
+ it "@post1 should not contain errors" do
126
126
  expect(@post1.errors.count).to be 0
127
127
  end
128
128
 
129
- it "@post2 should not cotain errors" do
129
+ it "@post2 should not contain errors" do
130
130
  expect(@post2.errors.count).to be 1
131
131
  expect(@post2.errors.added?(:body, :blank)).to be true
132
132
  end
@@ -143,12 +143,12 @@ describe RestMyCase::Validator do
143
143
  expect(@context.ok?).to be false
144
144
  end
145
145
 
146
- it "@post1 should not cotain errors" do
146
+ it "@post1 should not contain errors" do
147
147
  expect(@post1.errors.count).to be 1
148
148
  expect(@post1.errors.added?(:title, :blank)).to be true
149
149
  end
150
150
 
151
- it "@post2 should not cotain errors" do
151
+ it "@post2 should not contain errors" do
152
152
  expect(@post2.errors.count).to be 1
153
153
  expect(@post2.errors.keys).to eq [:body]
154
154
  end
@@ -165,11 +165,11 @@ describe RestMyCase::Validator do
165
165
  expect(@context.ok?).to be false
166
166
  end
167
167
 
168
- it "@post1 should not cotain errors" do
168
+ it "@post1 should not contain errors" do
169
169
  expect(@post1.errors.count).to be 0
170
170
  end
171
171
 
172
- it "@post2 should not cotain errors" do
172
+ it "@post2 should not contain errors" do
173
173
  expect(@post2.errors.count).to be 1
174
174
  expect(@post2.errors.include?(:body)).to be true
175
175
  end
@@ -186,11 +186,11 @@ describe RestMyCase::Validator do
186
186
  expect(@context.ok?).to be true
187
187
  end
188
188
 
189
- it "@post1 should not cotain errors" do
189
+ it "@post1 should not contain errors" do
190
190
  expect(@post1.errors.count).to be 0
191
191
  end
192
192
 
193
- it "@post2 should not cotain errors" do
193
+ it "@post2 should not contain errors" do
194
194
  expect(@post2.errors.count).to be 0
195
195
  end
196
196
  end
@@ -206,15 +206,74 @@ describe RestMyCase::Validator do
206
206
  expect(@context.ok?).to be true
207
207
  end
208
208
 
209
- it "@post1 should not cotain errors" do
209
+ it "@post1 should not contain errors" do
210
210
  expect(@post1.errors.count).to be 0
211
211
  end
212
212
 
213
- it "@post2 should not cotain errors" do
213
+ it "@post2 should not contain errors" do
214
214
  expect(@post2.errors.count).to be 0
215
215
  end
216
216
  end
217
217
 
218
218
  end
219
+
220
+ context "When passing an object with nested objects" do
221
+
222
+ context "and both of the nested object are valid" do
223
+ before do
224
+ @post_with_comments = RubyPostWithComments.new([
225
+ { title: 'first comment' },
226
+ { title: 'second comment' }
227
+ ])
228
+
229
+ @context = NestedValidation.perform(post: @post_with_comments)
230
+ end
231
+
232
+ it "@context.ok? should be true" do
233
+ expect(@context.ok?).to be true
234
+ end
235
+
236
+ it "@post_with_comments should not contain errors" do
237
+ expect(@post_with_comments.errors.count).to be 0
238
+ end
239
+
240
+ it "@post_with_comments.comments should not contain errors" do
241
+ @post_with_comments.comments.each do |comment|
242
+ expect(comment.errors.count).to be 0
243
+ end
244
+ end
245
+ end
246
+
247
+ context "and one of them is invalid" do
248
+ before do
249
+ @post_with_comments = RubyPostWithComments.new([
250
+ { title: 'first comment' },
251
+ {}
252
+ ])
253
+
254
+ @context = NestedValidation.perform(post: @post_with_comments)
255
+ end
256
+
257
+ it "@context.ok? should be false" do
258
+ expect(@context.ok?).to be false
259
+ end
260
+
261
+ it "@post_with_comments should contain one error" do
262
+ expect(@post_with_comments.errors.count).to be 1
263
+ expect(@post_with_comments.errors.added?(:comments, :invalid)).to be true
264
+ end
265
+
266
+ it "@post_with_comments.comments[0] should not contain errors" do
267
+ expect(@post_with_comments.comments[0].errors.count).to be 0
268
+ end
269
+
270
+ it "@post_with_comments.comments[1] should contain one error" do
271
+ expect(@post_with_comments.comments[1].errors.count).to be 1
272
+ expect(@post_with_comments.comments[1].errors.added?(:title, :blank)).to be true
273
+ end
274
+ end
275
+
276
+ end
277
+
219
278
  end
220
279
 
@@ -2,7 +2,7 @@ class RubyPostWithComments
2
2
 
3
3
  class RubyComment
4
4
 
5
- attr_accessor :title, :email, :post_id
5
+ attr_accessor :title
6
6
 
7
7
  def initialize(attributes = {})
8
8
  (attributes || {}).each { |name, value| send("#{name}=", value) }
@@ -10,19 +10,10 @@ class RubyPostWithComments
10
10
 
11
11
  end
12
12
 
13
-
14
13
  attr_reader :comments
15
14
 
16
- def initialize(comments = {})
15
+ def initialize(comments = [])
17
16
  @comments = comments.map { |comment| RubyComment.new(comment) }
18
- @comments = [] if @comments.nil?
19
- end
20
-
21
- def first_two_comments
22
- [
23
- comments[0],
24
- comments[1]
25
- ]
26
17
  end
27
18
 
28
19
  end
@@ -20,4 +20,4 @@ module HierarchyValidation
20
20
 
21
21
  end
22
22
 
23
- end
23
+ end
@@ -0,0 +1,7 @@
1
+ class NestedValidation < RestMyCase::Validator
2
+
3
+ target :comments, in: :post
4
+
5
+ validates_presence_of :title
6
+
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_my_case
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.6
4
+ version: 1.10.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - goncalvesjoao
@@ -164,6 +164,7 @@ files:
164
164
  - spec/support/validator/usecases/custom_validator.rb
165
165
  - spec/support/validator/usecases/hierarchy_validation.rb
166
166
  - spec/support/validator/usecases/length_validator.rb
167
+ - spec/support/validator/usecases/nested_validation.rb
167
168
  - spec/support/validator/usecases/numericality_validator.rb
168
169
  homepage: https://github.com/goncalvesjoao/rest_my_case
169
170
  licenses:
@@ -212,4 +213,5 @@ test_files:
212
213
  - spec/support/validator/usecases/custom_validator.rb
213
214
  - spec/support/validator/usecases/hierarchy_validation.rb
214
215
  - spec/support/validator/usecases/length_validator.rb
216
+ - spec/support/validator/usecases/nested_validation.rb
215
217
  - spec/support/validator/usecases/numericality_validator.rb