simple_params 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Gemfile.lock +1 -1
- data/lib/simple_params/concerns/rails_helpers.rb +19 -1
- data/lib/simple_params/concerns/validations.rb +1 -1
- data/lib/simple_params/errors.rb +14 -4
- data/lib/simple_params/params.rb +9 -6
- data/lib/simple_params/version.rb +1 -1
- data/spec/acceptance_spec.rb +85 -22
- data/spec/params_spec.rb +6 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzM2MTE0ZTVmMmFkMDNlOGQ5YjZiMDQ5MDc2NGIxNDIzOGU0YTI0ZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OTFiZGUxYTM0MzI4MWQyZGE3OWZiMDFkNzk3YzNmNmM5YTQ2OWYwZA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MjgyZWJhMDE2OTcxMjc1ZTA0NzM0NDYwYzU3MDI0NTZmNTdhYmZiYjU5ZDc3
|
10
|
+
ODQxNDQ0NTkzMTQ0MTIyMjAxZjZjYzEzOWE3NDJjYWIyZGZkYzcyY2JhYzIw
|
11
|
+
MTA3MDBiNjJjMGQyMWM3ZjQyNzViODczOTJjYjdiN2M5NjUwMDU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZDEwNzA0MDVjMzVkNjY4NzNmNjFjZDVhZTliNzFiNWYzZGZmNGFjZDJiMDNl
|
14
|
+
Mzk5ZGRmM2Q0MDk4ZDUyY2M4MDZlODBhYWQzNmRiOWJkNTg0Njk1M2Y3MWE2
|
15
|
+
ODM2NmRmODhjYWE2YjAwYjRlNjA0NzJiYTNhODE4MmRjNjM3YTQ=
|
data/Gemfile.lock
CHANGED
@@ -5,10 +5,28 @@ module SimpleParams
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
included do
|
7
7
|
# Required for ActiveModel
|
8
|
-
def persisted
|
8
|
+
def persisted?
|
9
|
+
false
|
10
|
+
end
|
9
11
|
end
|
10
12
|
|
11
13
|
module ClassMethods
|
14
|
+
# http://apidock.com/rails/ActiveRecord/Reflection/AssociationReflection/klass
|
15
|
+
# class Author < ActiveRecord::Base
|
16
|
+
# has_many :books
|
17
|
+
# end
|
18
|
+
|
19
|
+
# Author.reflect_on_association(:books).klass
|
20
|
+
# # => Book
|
21
|
+
def reflect_on_association(assoc_sym)
|
22
|
+
nested_classes[assoc_sym]
|
23
|
+
end
|
24
|
+
|
25
|
+
# Used with reflect_on_association
|
26
|
+
def klass
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
12
30
|
def define_rails_helpers(name, klass)
|
13
31
|
# E.g. if we have a nested_class named :phones, then we need:
|
14
32
|
# - a method called :phones_attributes that also sets :phones
|
data/lib/simple_params/errors.rb
CHANGED
@@ -35,14 +35,20 @@ module SimpleParams
|
|
35
35
|
def clear
|
36
36
|
super
|
37
37
|
@nested_classes.map do |attribute, klass|
|
38
|
-
run_or_mapped_run(klass)
|
38
|
+
run_or_mapped_run(klass) do |k|
|
39
|
+
if k.present?
|
40
|
+
k.errors.clear
|
41
|
+
end
|
42
|
+
end
|
39
43
|
end
|
40
44
|
end
|
41
45
|
|
42
46
|
def empty?
|
43
47
|
super &&
|
44
48
|
@nested_classes.all? do |attribute, klass|
|
45
|
-
run_or_mapped_run(klass)
|
49
|
+
run_or_mapped_run(klass) do |k|
|
50
|
+
k.nil? || k.errors.empty?
|
51
|
+
end
|
46
52
|
end
|
47
53
|
end
|
48
54
|
alias_method :blank?, :empty?
|
@@ -80,7 +86,11 @@ module SimpleParams
|
|
80
86
|
msgs = get_messages(self, full_messages)
|
81
87
|
|
82
88
|
@nested_classes.map do |attribute, klass|
|
83
|
-
nested_msgs = run_or_mapped_run(klass)
|
89
|
+
nested_msgs = run_or_mapped_run(klass) do |k|
|
90
|
+
unless k.nil?
|
91
|
+
get_messages(k.errors, full_messages)
|
92
|
+
end
|
93
|
+
end
|
84
94
|
unless empty_messages?(nested_msgs)
|
85
95
|
msgs.merge!(attribute.to_sym => nested_msgs)
|
86
96
|
end
|
@@ -104,7 +114,7 @@ module SimpleParams
|
|
104
114
|
|
105
115
|
def set_nested(attribute)
|
106
116
|
klass = nested_class(attribute)
|
107
|
-
errors = run_or_mapped_run(klass) { |k| k.errors }
|
117
|
+
errors = run_or_mapped_run(klass) { |k| k.errors if k.present? }
|
108
118
|
set(attribute.to_sym, errors)
|
109
119
|
end
|
110
120
|
|
data/lib/simple_params/params.rb
CHANGED
@@ -44,30 +44,33 @@ module SimpleParams
|
|
44
44
|
|
45
45
|
def nested_hash(name, opts={}, &block)
|
46
46
|
klass = NestedParams.define_new_hash_class(self, name, opts, &block)
|
47
|
-
add_nested_class(name, klass)
|
47
|
+
add_nested_class(name, klass, opts)
|
48
48
|
end
|
49
49
|
alias_method :nested_param, :nested_hash
|
50
50
|
alias_method :nested, :nested_hash
|
51
51
|
|
52
52
|
def nested_array(name, opts={}, &block)
|
53
53
|
klass = NestedParams.define_new_array_class(self, name, opts, &block)
|
54
|
-
add_nested_class(name, klass)
|
54
|
+
add_nested_class(name, klass, opts)
|
55
55
|
end
|
56
56
|
|
57
57
|
private
|
58
|
-
def add_nested_class(name, klass)
|
58
|
+
def add_nested_class(name, klass, opts)
|
59
59
|
@nested_classes ||= {}
|
60
60
|
@nested_classes[name.to_sym] = klass
|
61
|
-
define_nested_accessor(name, klass)
|
61
|
+
define_nested_accessor(name, klass, opts)
|
62
62
|
define_rails_helpers(name, klass)
|
63
63
|
end
|
64
64
|
|
65
|
-
def define_nested_accessor(name, klass)
|
65
|
+
def define_nested_accessor(name, klass, opts)
|
66
66
|
define_method("#{name}") do
|
67
67
|
if instance_variable_defined?("@#{name}")
|
68
68
|
instance_variable_get("@#{name}")
|
69
69
|
else
|
70
|
-
|
70
|
+
# This logic basically sets the nested class to an instance of itself, unless
|
71
|
+
# it is optional.
|
72
|
+
init_value = opts[:optional] ? nil : klass.new({}, self)
|
73
|
+
init_value = klass.hash? ? init_value : [init_value]
|
71
74
|
instance_variable_set("@#{name}", init_value)
|
72
75
|
end
|
73
76
|
end
|
data/spec/acceptance_spec.rb
CHANGED
@@ -19,6 +19,10 @@ class AcceptanceParams < SimpleParams::Params
|
|
19
19
|
param :company, optional: true
|
20
20
|
end
|
21
21
|
|
22
|
+
nested_hash :phone, optional: true do
|
23
|
+
param :phone_number
|
24
|
+
end
|
25
|
+
|
22
26
|
nested_array :dogs do
|
23
27
|
param :name
|
24
28
|
param :age, type: :integer, validations: { inclusion: { in: 1..20 } }
|
@@ -40,6 +44,18 @@ describe SimpleParams::Params do
|
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
47
|
+
describe "reflect_on_association", reflect_on_association: true do
|
48
|
+
it "can get hash association classes" do
|
49
|
+
klass = AcceptanceParams.reflect_on_association(:address).klass
|
50
|
+
klass.should eq(AcceptanceParams::Address)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "can get array association classes" do
|
54
|
+
klass = AcceptanceParams.reflect_on_association(:dogs).klass
|
55
|
+
klass.should eq(AcceptanceParams::Dogs)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
43
59
|
describe "original_params", original_params: true do
|
44
60
|
it "returns symbolized params hash" do
|
45
61
|
params = AcceptanceParams.new(name: "Tom", address: { "street" => "1 Main St."} )
|
@@ -89,6 +105,7 @@ describe SimpleParams::Params do
|
|
89
105
|
state: "North Carolina",
|
90
106
|
company: nil
|
91
107
|
},
|
108
|
+
phone: nil,
|
92
109
|
dogs: [
|
93
110
|
{
|
94
111
|
name: "Spot",
|
@@ -187,7 +204,7 @@ describe SimpleParams::Params do
|
|
187
204
|
describe "attributes", attributes: true do
|
188
205
|
it "returns array of attribute symbols" do
|
189
206
|
params = AcceptanceParams.new
|
190
|
-
params.attributes.should eq([:reference, :name, :date_of_birth, :current_time, :age, :color, :sibling_names, :address, :dogs])
|
207
|
+
params.attributes.should eq([:reference, :name, :date_of_birth, :current_time, :age, :color, :sibling_names, :address, :phone, :dogs])
|
191
208
|
end
|
192
209
|
|
193
210
|
it "returns array of attribute symbols for nested class" do
|
@@ -308,36 +325,79 @@ describe SimpleParams::Params do
|
|
308
325
|
|
309
326
|
it "raises error with validation descriptions" do
|
310
327
|
expect { params.validate! }.to raise_error(SimpleParamsError,
|
311
|
-
"{:name=>[\"can't be blank\"], :address=>{:street=>[\"can't be blank\"], :city=>[\"is too short (minimum is 4 characters)\", \"can't be blank\"]}}"
|
328
|
+
"{:name=>[\"can't be blank\"], :address=>{:street=>[\"can't be blank\"], :city=>[\"is too short (minimum is 4 characters)\", \"can't be blank\"]}, :dogs=>[{:name=>[\"can't be blank\"], :age=>[\"is not included in the list\", \"can't be blank\"]}]}"
|
312
329
|
)
|
313
330
|
end
|
314
331
|
end
|
315
332
|
|
316
333
|
describe "acceptance cases" do
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
334
|
+
context "without phone" do
|
335
|
+
let(:params) do
|
336
|
+
{
|
337
|
+
name: "Tom",
|
338
|
+
age: 41,
|
339
|
+
address: {
|
340
|
+
street: "1 Main St.",
|
341
|
+
city: "Chicago",
|
342
|
+
state: "IL",
|
343
|
+
zip_code: 33440
|
344
|
+
},
|
345
|
+
dogs: [
|
346
|
+
name: "Spot",
|
347
|
+
age: 6
|
348
|
+
]
|
326
349
|
}
|
327
|
-
|
328
|
-
end
|
350
|
+
end
|
329
351
|
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
352
|
+
it "is valid after multiple times", failing: true do
|
353
|
+
acceptance_params = AcceptanceParams.new(params)
|
354
|
+
acceptance_params.valid?
|
355
|
+
acceptance_params.should be_valid
|
356
|
+
acceptance_params.should be_valid
|
357
|
+
end
|
358
|
+
|
359
|
+
it "is invalidated if validity changes after initial assignment" do
|
360
|
+
acceptance_params = AcceptanceParams.new(params)
|
361
|
+
acceptance_params.should be_valid
|
362
|
+
acceptance_params.name = nil
|
363
|
+
acceptance_params.should_not be_valid
|
364
|
+
end
|
334
365
|
end
|
335
366
|
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
367
|
+
context "with phone" do
|
368
|
+
let(:params) do
|
369
|
+
{
|
370
|
+
name: "Tom",
|
371
|
+
age: 41,
|
372
|
+
address: {
|
373
|
+
street: "1 Main St.",
|
374
|
+
city: "Chicago",
|
375
|
+
state: "IL",
|
376
|
+
zip_code: 33440
|
377
|
+
},
|
378
|
+
phone: {
|
379
|
+
phone_number: "234"
|
380
|
+
},
|
381
|
+
dogs: [
|
382
|
+
name: "Spot",
|
383
|
+
age: 6
|
384
|
+
]
|
385
|
+
}
|
386
|
+
end
|
387
|
+
|
388
|
+
it "is valid after multiple times", failing: true do
|
389
|
+
acceptance_params = AcceptanceParams.new(params)
|
390
|
+
acceptance_params.valid?
|
391
|
+
acceptance_params.should be_valid
|
392
|
+
acceptance_params.should be_valid
|
393
|
+
end
|
394
|
+
|
395
|
+
it "is invalidated if validity changes after initial assignment" do
|
396
|
+
acceptance_params = AcceptanceParams.new(params)
|
397
|
+
acceptance_params.should be_valid
|
398
|
+
acceptance_params.name = nil
|
399
|
+
acceptance_params.should_not be_valid
|
400
|
+
end
|
341
401
|
end
|
342
402
|
end
|
343
403
|
end
|
@@ -380,6 +440,9 @@ describe SimpleParams::Params do
|
|
380
440
|
param :state, String, desc: '', required: false
|
381
441
|
param :company, String, desc: '', required: false
|
382
442
|
end
|
443
|
+
param :phone, Hash, desc: '', required: false do
|
444
|
+
param :phone_number, String, desc: '', required: true
|
445
|
+
end
|
383
446
|
param :dogs, Array, desc: '', required: true do
|
384
447
|
param :name, String, desc: '', required: true
|
385
448
|
param :age, Integer, desc: '', required: true
|
data/spec/params_spec.rb
CHANGED
@@ -77,23 +77,21 @@ describe SimpleParams::Params do
|
|
77
77
|
|
78
78
|
describe "nested arrays", nested: true do
|
79
79
|
it "can access nested arrays as arrays" do
|
80
|
-
params.dogs.
|
81
|
-
params.dogs.
|
80
|
+
params.dogs[0].should_not be_nil
|
81
|
+
params.dogs[0].name.should be_nil
|
82
|
+
params.dogs[0].age.should be_nil
|
82
83
|
end
|
83
84
|
|
84
85
|
it "can access nested arrays as arrays with data" do
|
85
86
|
params = DummyParams.new(dogs: [{ name: "Spot", age: 20 }])
|
86
|
-
params.dogs.should
|
87
|
-
params.dogs.first.should_not be_nil
|
88
|
-
params.dogs.first.name.should eq("SPOT")
|
87
|
+
params.dogs[0].name.should eq("SPOT")
|
89
88
|
end
|
90
89
|
|
91
90
|
it "can set nested arrays with arrays" do
|
92
91
|
params.dogs = [{ name: "Spot", age: 20 }]
|
93
92
|
params.dogs.count.should eq(1)
|
94
|
-
params.dogs.
|
95
|
-
params.dogs.
|
96
|
-
params.dogs.first.age.should eq(20)
|
93
|
+
params.dogs[0].name.should eq("SPOT")
|
94
|
+
params.dogs[0].age.should eq(20)
|
97
95
|
end
|
98
96
|
end
|
99
97
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- brycesenz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|