simple_params 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,8 @@ module SimpleParams
14
14
 
15
15
  def matches?(subject)
16
16
  super(subject)
17
- subject.send(:nested_arrays).has_key?(@attribute)
17
+ subject.send(:nested_classes).has_key?(@attribute) &&
18
+ subject.send(:nested_classes)[@attribute.to_sym].array?
18
19
  end
19
20
 
20
21
  def description
@@ -14,7 +14,8 @@ module SimpleParams
14
14
 
15
15
  def matches?(subject)
16
16
  super(subject)
17
- subject.send(:nested_hashes).has_key?(@attribute)
17
+ subject.send(:nested_classes).has_key?(@attribute) &&
18
+ subject.send(:nested_classes)[@attribute.to_sym].hash?
18
19
  end
19
20
 
20
21
  def description
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "1.1.2"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/simple_params.rb CHANGED
@@ -2,14 +2,20 @@ require 'simple_params/version'
2
2
  require 'simple_params/attribute'
3
3
  require 'simple_params/formatter'
4
4
  require 'simple_params/errors'
5
- require 'simple_params/validations'
5
+ require 'simple_params/concerns/has_attributes'
6
+ require 'simple_params/concerns/date_time_helpers'
7
+ require 'simple_params/concerns/hash_helpers'
8
+ require 'simple_params/concerns/has_typed_params'
9
+ require 'simple_params/concerns/rails_helpers'
10
+ require 'simple_params/concerns/strict_params'
11
+ require 'simple_params/concerns/validations'
6
12
  require 'simple_params/params'
13
+ require 'simple_params/nested_params'
7
14
  require 'simple_params/simple_params_error'
8
15
  require 'simple_params/type_mappings'
9
16
  require 'simple_params/api_pie_doc'
10
17
  require 'simple_params/api_pie_doc/attribute_base'
11
18
  require 'simple_params/api_pie_doc/attribute'
12
- require 'simple_params/api_pie_doc/nested_array'
13
19
  require 'simple_params/api_pie_doc/nested_attribute'
14
20
  require 'simple_params/validation_matchers/validation_matcher'
15
21
  require 'simple_params/validation_matchers/coercion_matcher'
@@ -12,33 +12,20 @@ describe SimpleParams::ApiPieDoc do
12
12
  expect(api_pie_doc.base_attributes.keys).to include(:amount, :color, :first_initial)
13
13
  end
14
14
 
15
- specify "should give object nested_hashes" do
16
- expect(api_pie_doc.nested_hashes.keys).to eq [:address, :phone]
17
- end
18
-
19
- specify "should give object nested_arrays" do
20
- expect(api_pie_doc.nested_arrays.keys).to eq [:dogs]
15
+ specify "should give object nested_classes" do
16
+ expect(api_pie_doc.nested_classes.keys).to eq [:address, :phone, :dogs]
21
17
  end
22
18
 
23
19
  specify "should call #build_nested_attributes" do
24
- expect_any_instance_of(SimpleParams::ApiPieDoc).to receive(:build_nested_attributes)
25
- api_pie_doc
26
- end
27
-
28
- specify "should call #build_nested_array_attributes" do
29
- expect_any_instance_of(SimpleParams::ApiPieDoc).to receive(:build_nested_array_attributes)
20
+ expect_any_instance_of(SimpleParams::ApiPieDoc).to receive(:build_nested_classes)
30
21
  api_pie_doc
31
22
  end
32
23
 
33
24
  specify "should give object nested_attributes" do
34
- expect(api_pie_doc.nested_attributes.flat_map(&:keys)).to include(:address, :phone)
35
- expect(api_pie_doc.nested_attributes[0].values.flat_map(&:keys)).to eq [:street, :city, :zip_code, :state]
36
- expect(api_pie_doc.nested_attributes[1].values.flat_map(&:keys)).to eq [:cell_phone, :phone_number, :area_code]
37
- end
38
-
39
- specify "should give object nested_array_attributes" do
40
- expect(api_pie_doc.nested_array_attributes.flat_map(&:keys)).to include(:dogs)
41
- expect(api_pie_doc.nested_array_attributes[0].values.flat_map(&:keys)).to eq [:name, :age]
25
+ expect(api_pie_doc.nested_attributes.flat_map(&:keys)).to include(:address, :phone, :dogs)
26
+ expect(api_pie_doc.nested_attributes[0].values.flat_map(&:keys)).to eq [:street, :city, :zip_code, :state, :type]
27
+ expect(api_pie_doc.nested_attributes[1].values.flat_map(&:keys)).to eq [:cell_phone, :phone_number, :area_code, :type]
28
+ expect(api_pie_doc.nested_attributes[2].values.flat_map(&:keys)).to eq [:name, :age, :type]
42
29
  end
43
30
 
44
31
  specify "should give object docs" do
data/spec/errors_spec.rb CHANGED
@@ -4,10 +4,7 @@ describe SimpleParams::Errors do
4
4
  class Person
5
5
  extend ActiveModel::Naming
6
6
  def initialize
7
- @errors = SimpleParams::Errors.new(self,
8
- { dog: dog.errors },
9
- { cats: cats_errors },
10
- )
7
+ @errors = SimpleParams::Errors.new(self, {dog: dog, cats: cats})
11
8
  end
12
9
 
13
10
  attr_accessor :name, :age
@@ -21,10 +18,6 @@ describe SimpleParams::Errors do
21
18
  @cats ||= [Cat.new]
22
19
  end
23
20
 
24
- def cats_errors
25
- cats.map { |cat| cat.errors }
26
- end
27
-
28
21
  def read_attribute_for_validation(attr)
29
22
  send(attr)
30
23
  end
@@ -47,6 +40,10 @@ describe SimpleParams::Errors do
47
40
  attr_accessor :breed
48
41
  attr_reader :errors
49
42
 
43
+ def id
44
+ nil
45
+ end
46
+
50
47
  def read_attribute_for_validation(attr)
51
48
  send(attr)
52
49
  end
@@ -70,6 +67,10 @@ describe SimpleParams::Errors do
70
67
  attr_accessor :age
71
68
  attr_reader :errors
72
69
 
70
+ def id
71
+ 123
72
+ end
73
+
73
74
  def read_attribute_for_validation(attr)
74
75
  send(attr)
75
76
  end
@@ -194,7 +195,7 @@ describe SimpleParams::Errors do
194
195
  person.errors[:cats][0].should eq(cat_errors)
195
196
  end
196
197
 
197
- it "can add to nested errors through []", failing: true do
198
+ it "can add to nested errors through []" do
198
199
  person = Person.new
199
200
  person.errors[:cats].first[:base] = 'should not be nil'
200
201
  person.errors[:cats].first[:base].should eq(['should not be nil'])
@@ -375,7 +376,7 @@ describe SimpleParams::Errors do
375
376
  end
376
377
 
377
378
  describe "#to_hash", to_hash: true do
378
- it "to_hash returns the error messages hash", hash_failing: true do
379
+ it "to_hash returns the error messages hash" do
379
380
  person = Person.new
380
381
  person.errors.add(:name, "can not be blank")
381
382
  person.errors.to_hash.should eq({ name: ["can not be blank"] })
@@ -408,6 +409,28 @@ describe SimpleParams::Errors do
408
409
  }
409
410
  })
410
411
  end
412
+
413
+ it "handles nested attributes with base errors and array errors" do
414
+ person = Person.new
415
+ person.errors.add(:base, :invalid)
416
+ person.errors.add(:name, "can not be blank")
417
+ person.dog.errors.add(:base, :invalid)
418
+ person.dog.errors.add(:breed, "can not be nil")
419
+ person.cats.first.errors.add(:name, "can not be blank")
420
+ person.errors.to_hash.should eq({
421
+ base: ["is invalid"],
422
+ name: ["can not be blank"],
423
+ dog: {
424
+ base: ["is invalid"],
425
+ breed: ["can not be nil"]
426
+ },
427
+ cats: [
428
+ {
429
+ name: ["can not be blank"]
430
+ }
431
+ ]
432
+ })
433
+ end
411
434
  end
412
435
 
413
436
  describe "#as_json", as_json: true do
@@ -0,0 +1,193 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/dummy_params'
3
+
4
+ describe SimpleParams::NestedParams do
5
+ # Turn off Constant Redefined errors
6
+ before(:each) do
7
+ $VERBOSE = nil
8
+ end
9
+
10
+ class DummyParentClass < SimpleParams::Params
11
+ end
12
+
13
+ describe "class_methods", class_methods: true do
14
+ let!(:parent) { DummyParentClass }
15
+ let!(:name) { :my_special_params }
16
+
17
+ describe "define_new_hash_class", define_new_hash_class: true do
18
+ let(:options) do
19
+ {
20
+ first_option: "yes",
21
+ second_option: "totally"
22
+ }
23
+ end
24
+
25
+ let(:defined_class) do
26
+ described_class.define_new_hash_class(parent, name, options) do
27
+ param :name, default: "Tom"
28
+ param :age
29
+ end
30
+ end
31
+
32
+ it "has correct name" do
33
+ defined_class.name.should eq("DummyParentClass::MySpecialParams")
34
+ end
35
+
36
+ it "has correct options" do
37
+ defined_class.options.should eq(
38
+ {
39
+ first_option: "yes",
40
+ second_option: "totally",
41
+ type: :hash
42
+ }
43
+ )
44
+ end
45
+
46
+ it "has correct type" do
47
+ defined_class.type.should eq(:hash)
48
+ end
49
+
50
+ it "does not use ids" do
51
+ defined_class.with_ids?.should eq(false)
52
+ end
53
+ end
54
+
55
+ describe "define_new_array_class", define_new_array_class: true do
56
+ let(:options) do
57
+ {
58
+ first_option: "yes",
59
+ second_option: "totally",
60
+ with_ids: true
61
+ }
62
+ end
63
+
64
+ let(:defined_class) do
65
+ described_class.define_new_array_class(parent, name, options) do
66
+ param :name, default: "Tom"
67
+ param :age
68
+ end
69
+ end
70
+
71
+ it "has correct name" do
72
+ defined_class.name.should eq("DummyParentClass::MySpecialParams")
73
+ end
74
+
75
+ it "has correct options" do
76
+ defined_class.options.should eq(
77
+ {
78
+ first_option: "yes",
79
+ second_option: "totally",
80
+ with_ids: true,
81
+ type: :array
82
+ }
83
+ )
84
+ end
85
+
86
+ it "has correct type" do
87
+ defined_class.type.should eq(:array)
88
+ end
89
+
90
+ it "doe use ids" do
91
+ defined_class.with_ids?.should eq(true)
92
+ end
93
+ end
94
+ end
95
+
96
+ describe "initialization" do
97
+ context "with hash class" do
98
+ context "without ids" do
99
+ let(:defined_class) do
100
+ described_class.define_new_hash_class(DummyParentClass, :demo, {}) do
101
+ param :name, default: "Tom"
102
+ param :age, type: :integer
103
+ end
104
+ end
105
+
106
+ subject { defined_class.new(name: "Bill", age: 21) }
107
+
108
+ specify "name" do
109
+ expect(subject.name).to eq "Bill"
110
+ end
111
+
112
+ specify "age" do
113
+ expect(subject.age).to eq 21
114
+ end
115
+
116
+ specify "id" do
117
+ expect(subject.id).to eq nil
118
+ end
119
+ end
120
+
121
+ context "with ids" do
122
+ let(:defined_class) do
123
+ described_class.define_new_hash_class(DummyParentClass, :demo, { with_ids: true }) do
124
+ param :name, default: "Tom"
125
+ param :age, type: :integer
126
+ end
127
+ end
128
+
129
+ subject { defined_class.new("132" => { name: "Bill", age: 21 }) }
130
+
131
+ specify "name" do
132
+ expect(subject.name).to eq "Bill"
133
+ end
134
+
135
+ specify "age" do
136
+ expect(subject.age).to eq 21
137
+ end
138
+
139
+ specify "id" do
140
+ expect(subject.id).to eq "132"
141
+ end
142
+ end
143
+ end
144
+
145
+ context "with array class" do
146
+ context "without ids" do
147
+ let(:defined_class) do
148
+ described_class.define_new_array_class(DummyParentClass, :demo, {}) do
149
+ param :name, default: "Tom"
150
+ param :age, type: :integer
151
+ end
152
+ end
153
+
154
+ subject { defined_class.new(name: "Bill", age: 21) }
155
+
156
+ specify "name" do
157
+ expect(subject.name).to eq "Bill"
158
+ end
159
+
160
+ specify "age" do
161
+ expect(subject.age).to eq 21
162
+ end
163
+
164
+ specify "id" do
165
+ expect(subject.id).to eq nil
166
+ end
167
+ end
168
+
169
+ context "with ids" do
170
+ let(:defined_class) do
171
+ described_class.define_new_array_class(DummyParentClass, :demo, { with_ids: true }) do
172
+ param :name, default: "Tom"
173
+ param :age, type: :integer
174
+ end
175
+ end
176
+
177
+ subject { defined_class.new("132" => { name: "Bill", age: 21 }) }
178
+
179
+ specify "name" do
180
+ expect(subject.name).to eq "Bill"
181
+ end
182
+
183
+ specify "age" do
184
+ expect(subject.age).to eq 21
185
+ end
186
+
187
+ specify "id" do
188
+ expect(subject.id).to eq "132"
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
data/spec/params_spec.rb CHANGED
@@ -66,6 +66,13 @@ describe SimpleParams::Params do
66
66
  params.address.zip_code = "20165"
67
67
  params.address.zip_code.should eq("20165")
68
68
  end
69
+
70
+ it "can set nested_array with hash" do
71
+ params.address= { street: "2 Oak Ave.", city: "Miami", zip_code: "90210" }
72
+ params.address.street.should eq("2 Oak Ave.")
73
+ params.address.city.should eq("Miami")
74
+ params.address.zip_code.should eq("90210")
75
+ end
69
76
  end
70
77
 
71
78
  describe "nested arrays", nested: true do
@@ -80,6 +87,14 @@ describe SimpleParams::Params do
80
87
  params.dogs.first.should_not be_nil
81
88
  params.dogs.first.name.should eq("SPOT")
82
89
  end
90
+
91
+ it "can set nested arrays with arrays" do
92
+ params.dogs = [{ name: "Spot", age: 20 }]
93
+ params.dogs.count.should eq(1)
94
+ params.dogs.first.should_not be_nil
95
+ params.dogs.first.name.should eq("SPOT")
96
+ params.dogs.first.age.should eq(20)
97
+ end
83
98
  end
84
99
  end
85
100
 
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ class RailsIntegrationParams < SimpleParams::Params
4
+ param :name
5
+ param :age, type: :integer, optional: true, validations: { inclusion: { in: 18..100 } }
6
+ param :current_time, type: :datetime, optional: true
7
+
8
+ nested_hash :address do
9
+ param :street
10
+ param :city, validations: { length: { in: 4..40 } }
11
+ param :zip_code, optional: true
12
+ param :state, default: "North Carolina"
13
+ end
14
+
15
+ nested_array :dogs, with_ids: true do
16
+ param :name
17
+ param :age, type: :integer, validations: { inclusion: { in: 1..20 } }
18
+ end
19
+ end
20
+
21
+ describe SimpleParams::Params do
22
+ let!(:params) do
23
+ RailsIntegrationParams.new(
24
+ name: "Tom",
25
+ age: 21,
26
+ "current_time(6i)" => 59,
27
+ "current_time(5i)" => 58,
28
+ "current_time(4i)" => 11,
29
+ "current_time(3i)" => 4,
30
+ "current_time(2i)" => 3,
31
+ "current_time(1i)" => 2009,
32
+ address_attributes: {
33
+ street: "1 Main St.",
34
+ city: "Charlotte"
35
+ },
36
+ dogs_attributes: [
37
+ "0" => {
38
+ name: "Spot",
39
+ age: 4
40
+ },
41
+ "1" => {
42
+ age: 6
43
+ }
44
+ ]
45
+ )
46
+ end
47
+
48
+ specify "is not persisted" do
49
+ expect(params).to_not be_persisted
50
+ end
51
+
52
+ specify "setting datetime" do
53
+ expect(params.current_time).to eq DateTime.new(2009, 3, 4, 11, 58, 59)
54
+ end
55
+
56
+ specify "sets address" do
57
+ address = params.address
58
+ expect(address.street).to eq "1 Main St."
59
+ expect(address.city).to eq "Charlotte"
60
+ end
61
+
62
+ specify "sets dogs"do
63
+ dogs = params.dogs
64
+ expect(dogs.count).to eq 2
65
+ expect(dogs[0].name).to eq "Spot"
66
+ expect(dogs[0].age).to eq 4
67
+ expect(dogs[1].name).to eq nil
68
+ expect(dogs[1].age).to eq 6
69
+ end
70
+ end