simple_params 1.6.4 → 1.6.5

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
- MTM0ZTQ4ZDljMTUxYmJhMWIwMjg3OWRjNWZmYzJjYTgwYmJlNmE4Yg==
4
+ YWNlZDM0YjZkNWQxNjhhOGRjZDJjMWJiMzBiMTU1NGQ3ZjljNTZmMQ==
5
5
  data.tar.gz: !binary |-
6
- ODM2NGY2NGU2MTVkMTIzMjdhNWY4YjJmYzI2NGVjYTliMDJjMTQ3Mw==
6
+ MTExMDdjMmE1MWJkOWEzNWJkOWEyOTg5MThmMWQ5ZTNhNjRiY2U5YQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDI0ZjU1YmQwNGUyMWIwYWZkYjQwZjgwYjgxMWM1OTI4N2E3ZjYzOGI1Mzhk
10
- M2UxODE5YzFiNjhkNTkxNDM3NGVmNmQ1ODVkNjAxYmI0NDFiYjFhOWQzOTc3
11
- YjdiMTJlMGZlNzYwYzA0ZmQ1MDJkZTRiYTRkNGUwMDViNmJkY2M=
9
+ YmY4ZDg1NjFjNDYxNTBjZGRhNDhkMTBjNGI3Y2RmOTEyZWExYWVjZmQ0OWQz
10
+ YTBiNzBhMjY4N2NlYzgyNmQwNTg0MjQxZGNkMmU5ZDNkOWI5MDhiY2VhZTg3
11
+ ZmE5MTYxMThjNWU1OTBiZTk2MGVkNTJiZjBlNzNiYWIyM2YxN2Y=
12
12
  data.tar.gz: !binary |-
13
- M2UyNTA3ODk3ZTk4NDY3NmM4MTIyNDMwNmZiNzc2NDM2MTAyZjY0MTNlMmFj
14
- OGRlMGVjOTgyZDcxODJlNmYxMTUxMzAyZjU1OGIyYzNhODFhMDE1YTBmN2Nk
15
- MGQ2YjIwNzkyYjA0ZGEyOWE5NjA2NTU4NmYzMGJiYWU0YmRjYmE=
13
+ ZjVkZTVmNWRlZWJmNzVhOWI3ZWE5NDllMDg2NGZlZWQzYWJlMmFiZTlkOTc1
14
+ NWJhZTdjNGE2MTQ4NDk5YjdhZTFiYjlhOTc4OWYyZTg2Y2EzNjg2Y2U5ZDFm
15
+ ZDJiZmFkNGVlZmU3OGU4MzU0NTczYjZmNjg0NjU5YTEwYTA1OWQ=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_params (1.6.3)
4
+ simple_params (1.6.4)
5
5
  activemodel (>= 3.0, < 5.0)
6
6
  shoulda-matchers (~> 2.8)
7
7
  virtus (>= 1.0.0)
@@ -64,7 +64,7 @@ module SimpleParams
64
64
  # TODO: This needs more testing
65
65
  singular_key = singularized_key(name)
66
66
  define_method("build_#{singular_key}") do |value={}|
67
- klass.new(value, self, name)
67
+ klass.new(value, self)
68
68
  end
69
69
  end
70
70
 
@@ -23,19 +23,10 @@ module SimpleParams
23
23
 
24
24
  private
25
25
  def nested_classes_valid?
26
- nested_classes.map do |key, array|
27
- nested_class_valid?(send("#{key}") )
28
- end.all?
29
- end
30
-
31
- def nested_class_valid?(nested_class)
32
- if nested_class.is_a?(Array)
33
- # Have to map? & THEN all?, or else it won't
34
- # necessarily call valid? on every object
35
- nested_class.map { |klass| klass.nil? || klass.valid? }.all?
36
- else
37
- nested_class.nil? || nested_class.valid?
38
- end
26
+ # Need to map them, THEN call all?, otherwise validations won't get run
27
+ # on every nested class
28
+ validations = all_nested_classes.map { |klass| klass.valid? }
29
+ validations.all?
39
30
  end
40
31
  end
41
32
  end
@@ -4,10 +4,11 @@ module SimpleParams
4
4
  class Errors < ActiveModel::Errors
5
5
  attr_reader :base
6
6
 
7
- def initialize(base, nested_classes = {})
7
+ def initialize(base, nested_classes = {}, nested_classes_array = [])
8
8
  super(base)
9
9
  @base = base
10
10
  @nested_classes = symbolize_nested(nested_classes)
11
+ @nested_classes_array = nested_classes_array
11
12
  end
12
13
 
13
14
  def [](attribute)
@@ -98,11 +99,6 @@ module SimpleParams
98
99
  @nested_classes.keys.include?(attribute.to_sym)
99
100
  end
100
101
 
101
- def set_nested(attribute)
102
- klass = nested_class(attribute)
103
- errors = run_or_mapped_run(klass) { |k| k.errors if k.present? }
104
- set(attribute.to_sym, errors)
105
- end
106
102
 
107
103
  def add_error_to_attribute(attribute, error)
108
104
  if nested_attribute?(attribute)
@@ -138,6 +134,11 @@ module SimpleParams
138
134
  nested.inject({}) { |memo,(k,v) | memo[k.to_sym] = v; memo }
139
135
  end
140
136
 
137
+ def set_nested(attribute)
138
+ klass = nested_class(attribute)
139
+ errors = run_or_mapped_run(klass) { |k| k.errors if k.present? }
140
+ set(attribute.to_sym, errors)
141
+ end
141
142
 
142
143
  def nested_instances
143
144
  array =[]
@@ -0,0 +1,46 @@
1
+ module SimpleParams
2
+ class NestedClassList
3
+ attr_reader :parent
4
+
5
+ def initialize(parent)
6
+ @parent = parent
7
+ end
8
+
9
+ def to_hash
10
+ nested_class_hash
11
+ end
12
+
13
+ def get_class_key(klass)
14
+ nested_class_hash.each do |key, value|
15
+ if value.is_a?(Array)
16
+ return key if value.include?(klass)
17
+ else
18
+ return key if value == klass
19
+ end
20
+ end
21
+ end
22
+
23
+ def class_instances
24
+ nested_class_hash.each_pair.inject([]) do |array, (key, value)|
25
+ array << value
26
+ array.flatten.compact
27
+ end
28
+ end
29
+
30
+ private
31
+ def nested_class_hash
32
+ @nested_class_hash ||= nested_class_attributes.inject({}) do |hash, param|
33
+ hash[param.to_sym] = get_nested_class_from_parent(param)
34
+ hash
35
+ end
36
+ end
37
+
38
+ def nested_class_attributes
39
+ @parent.nested_class_attributes
40
+ end
41
+
42
+ def get_nested_class_from_parent(klass)
43
+ @parent.send(klass)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,89 @@
1
+ require "active_model"
2
+
3
+ module SimpleParams
4
+ class NestedErrors < ActiveModel::Errors
5
+ attr_reader :base
6
+
7
+ def initialize(base)
8
+ super(base)
9
+ @base = base
10
+ end
11
+
12
+ def array?
13
+ @base.array?
14
+ end
15
+
16
+ def hash?
17
+ @base.hash?
18
+ end
19
+
20
+ def [](attribute)
21
+ get(attribute.to_sym) || set(attribute.to_sym, [])
22
+ end
23
+
24
+ def []=(attribute, error)
25
+ add_error_to_attribute(attribute, error)
26
+ end
27
+
28
+ def add(attribute, message = :invalid, options = {})
29
+ message = normalize_message(attribute, message, options)
30
+ if exception = options[:strict]
31
+ exception = ActiveModel::StrictValidationFailed if exception == true
32
+ raise exception, full_message(attribute, message)
33
+ end
34
+
35
+ add_error_to_attribute(attribute, message)
36
+ end
37
+
38
+ def clear
39
+ super
40
+ end
41
+
42
+ def empty?
43
+ super
44
+ end
45
+ alias_method :blank?, :empty?
46
+
47
+ def include?(attribute)
48
+ messages[attribute].present?
49
+ end
50
+ alias_method :has_key?, :include?
51
+ alias_method :key?, :include?
52
+
53
+ def values
54
+ messages.values
55
+ end
56
+
57
+ def full_messages
58
+ map { |attribute, message| full_message(attribute, message) }
59
+ end
60
+
61
+ def to_hash(full_messages = false)
62
+ get_messages(self, full_messages)
63
+ end
64
+
65
+ def to_s(full_messages = false)
66
+ array = to_a.compact
67
+ array.join(', ')
68
+ end
69
+
70
+ private
71
+ def add_error_to_attribute(attribute, error)
72
+ self[attribute] << error
73
+ end
74
+
75
+ def get_messages(object, full_messages = false)
76
+ if full_messages
77
+ object.messages.each_with_object({}) do |(attribute, array), messages|
78
+ messages[attribute] = array.map { |message| object.full_message(attribute, message) }
79
+ end
80
+ else
81
+ object.messages.dup
82
+ end
83
+ end
84
+
85
+ def empty_messages?(msgs)
86
+ msgs.nil? || msgs.empty?
87
+ end
88
+ end
89
+ end
@@ -31,11 +31,11 @@ module SimpleParams
31
31
 
32
32
  def build(params, parent, name)
33
33
  if params.is_a?(Array)
34
- params.map { |p| build_instance(p, parent, name) }.compact
34
+ params.map { |p| build_instance(p, parent) }.compact
35
35
  elsif with_ids?
36
- params.each_pair.map { |key, val| build_instance({key => val}, parent, name) }.compact
36
+ params.each_pair.map { |key, val| build_instance({key => val}, parent) }.compact
37
37
  else
38
- build_instance(params, parent, name)
38
+ build_instance(params, parent)
39
39
  end
40
40
  end
41
41
 
@@ -44,8 +44,8 @@ module SimpleParams
44
44
  NestedParamsClassBuilder.new(parent).build(self, name, options, &block)
45
45
  end
46
46
 
47
- def build_instance(params, parent, name)
48
- instance = self.new(params, parent, name)
47
+ def build_instance(params, parent)
48
+ instance = self.new(params, parent)
49
49
  instance.destroyed? ? nil : instance
50
50
  end
51
51
  end
@@ -54,14 +54,21 @@ module SimpleParams
54
54
 
55
55
  # Should allow NestedParams to be initialized with no arguments, in order
56
56
  # to be compatible with some Rails form gems like 'nested_form'
57
- def initialize(params={}, parent = nil, parent_attribute_name = nil)
57
+ def initialize(params={}, parent = nil)
58
58
  @parent = parent
59
- @parent_attribute_name = parent_attribute_name
60
59
  @id = extract_id(params)
61
60
  @params = extract_initialization_params(params)
62
61
  super(@params)
63
62
  end
64
63
 
64
+ def array?
65
+ self.class.array?
66
+ end
67
+
68
+ def symbol
69
+ self.class.name_symbol
70
+ end
71
+
65
72
  def destroyed?
66
73
  sym_params = symbolize_params(params)
67
74
  [true, 1, "1", "true"].include?(sym_params[:_destroy])
@@ -7,14 +7,21 @@ module SimpleParams
7
7
  #TODO: Need to test this!
8
8
  def build(nested_params, name, options, &block)
9
9
  klass_name = name.to_s.split('_').collect(&:capitalize).join
10
+ name_symbol = name.to_sym
10
11
  Class.new(nested_params).tap do |klass|
11
12
  @parent.send(:remove_const, klass_name) if @parent.const_defined?(klass_name)
12
13
  @parent.const_set(klass_name, klass)
14
+
13
15
  klass.instance_eval <<-DEF
14
16
  def parent_class
15
17
  #{@parent}
16
18
  end
19
+
20
+ def name_symbol
21
+ :#{name_symbol}
22
+ end
17
23
  DEF
24
+
18
25
  klass.class_eval('extend ActiveModel::Naming')
19
26
  klass.class_eval(&block)
20
27
  klass.class_eval("self.options = #{options}")
@@ -78,13 +78,18 @@ module SimpleParams
78
78
  init_value = if opts[:optional]
79
79
  klass.hash? ? nil : []
80
80
  else
81
- klass_instance = klass.new({}, self, name)
81
+ klass_instance = klass.new({}, self)
82
82
  klass.hash? ? klass_instance : [klass_instance]
83
83
  end
84
84
  instance_variable_set("@#{name}", init_value)
85
85
  end
86
86
  end
87
87
 
88
+ define_method("#{name}_params") do
89
+ original_params = instance_variable_get("@original_params")
90
+ original_params[:"#{name}"] || original_params[:"#{name}_attributes"]
91
+ end
92
+
88
93
  define_method("#{name}=") do |initializer|
89
94
  init_value = klass.build(initializer, self, name)
90
95
  instance_variable_set("@#{name}", init_value)
@@ -101,15 +106,12 @@ module SimpleParams
101
106
  params = InitializationHash.new(params)
102
107
  @original_params = params.original_params
103
108
  define_attributes(@original_params)
104
-
105
- # Nested Classes
106
- @nested_classes = nested_classes.keys
107
109
  set_accessors(params)
108
110
  end
109
111
 
110
112
  def define_attributes(params)
111
113
  self.class.defined_attributes.each_pair do |key, opts|
112
- send("#{key}_attribute=", Attribute.new(self, key, opts))
114
+ self.send("#{key}_attribute=", Attribute.new(self, key, opts))
113
115
  end
114
116
  end
115
117
 
@@ -117,11 +119,19 @@ module SimpleParams
117
119
  HashBuilder.new(self).build
118
120
  end
119
121
 
122
+ def nested_class_attributes
123
+ nested_classes.keys
124
+ end
125
+
126
+ def nested_class_hash
127
+ nested_class_list.to_hash
128
+ end
129
+
130
+ def all_nested_classes
131
+ nested_class_list.class_instances
132
+ end
133
+
120
134
  def errors
121
- nested_class_hash = {}
122
- @nested_classes.each do |param|
123
- nested_class_hash[param.to_sym] = send(param)
124
- end
125
135
  @errors ||= SimpleParams::Errors.new(self, nested_class_hash)
126
136
  end
127
137
 
@@ -132,6 +142,10 @@ module SimpleParams
132
142
  end
133
143
  end
134
144
 
145
+ def nested_class_list
146
+ @nested_class_list ||= NestedClassList.new(self)
147
+ end
148
+
135
149
  def defined_attributes
136
150
  self.class.defined_attributes
137
151
  end
@@ -7,7 +7,7 @@ module SimpleParams
7
7
  end
8
8
  end
9
9
 
10
-
10
+ # Just Leaving this here for reference as to what we're inhereting from
11
11
  # module Shoulda
12
12
  # module Matchers
13
13
  # module ActiveModel
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "1.6.4"
2
+ VERSION = "1.6.5"
3
3
  end
data/lib/simple_params.rb CHANGED
@@ -11,6 +11,8 @@ require 'simple_params/concerns/validations'
11
11
  require 'simple_params/params'
12
12
  require 'simple_params/initialization_hash'
13
13
  require 'simple_params/hash_builder'
14
+ require 'simple_params/nested_class_list'
15
+ require 'simple_params/nested_errors'
14
16
  require 'simple_params/nested_params'
15
17
  require 'simple_params/nested_params_class_builder'
16
18
  require 'simple_params/simple_params_error'
@@ -104,6 +104,23 @@ describe SimpleParams::Params do
104
104
  end
105
105
  end
106
106
 
107
+
108
+ describe "nested_parameter_accessors", nested_parameter_accessors: true do
109
+ it "returns params hash for nested attribute" do
110
+ params = AcceptanceParams.new(name: "Tom", address: { "street" => "1 Main St."} )
111
+ params.address_params.should eq({
112
+ street: "1 Main St."
113
+ })
114
+ end
115
+
116
+ it "returns params hash for nested attribute" do
117
+ params = AcceptanceParams.new(name: "Tom", address_attributes: { "street" => "1 Main St."} )
118
+ params.address_params.should eq({
119
+ street: "1 Main St."
120
+ })
121
+ end
122
+ end
123
+
107
124
  describe "to_hash", to_hash: true do
108
125
  it "returns params hash" do
109
126
  params = AcceptanceParams.new(
@@ -282,12 +299,6 @@ describe SimpleParams::Params do
282
299
  params.attributes.should eq([:reference, :name, :date_of_birth, :content, :current_time, :age, :color, :sibling_names, :address, :phone, :dogs, :cats, :birds])
283
300
  end
284
301
 
285
- it "returns array of attribute symbols for nested class" do
286
- params = AcceptanceParams::Address.new({}, nil, :address)
287
- params.parent_attribute_name.should eq(:address)
288
- params.attributes.should eq([:street, :city, :zip_code, :state, :company, :_destroy])
289
- end
290
-
291
302
  it "initializes attributes correctly" do
292
303
  params = AcceptanceParams.new
293
304
  attribute = params.instance_variable_get("@age_attribute")
@@ -600,7 +611,7 @@ describe SimpleParams::Params do
600
611
  }
601
612
  end
602
613
 
603
- it "is valid after multiple times" do
614
+ it "is valid after multiple times", failing: true do
604
615
  acceptance_params = AcceptanceParams.new(params)
605
616
  acceptance_params.valid?
606
617
  acceptance_params.should be_valid
@@ -0,0 +1,333 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleParams::NestedErrors do
4
+ class Car
5
+ extend ActiveModel::Naming
6
+ def initialize
7
+ @errors = SimpleParams::NestedErrors.new(self)
8
+ end
9
+
10
+ attr_accessor :make
11
+ attr_accessor :model
12
+ attr_reader :errors
13
+
14
+ def array?; false; end
15
+ def hash?; true; end
16
+
17
+ def read_attribute_for_validation(attr)
18
+ send(attr)
19
+ end
20
+
21
+ def self.human_attribute_name(attr, options = {})
22
+ attr
23
+ end
24
+
25
+ def self.lookup_ancestors
26
+ [self]
27
+ end
28
+ end
29
+
30
+ class Plane
31
+ extend ActiveModel::Naming
32
+ def initialize
33
+ @errors = SimpleParams::NestedErrors.new(self)
34
+ end
35
+
36
+ attr_accessor :pilot
37
+ attr_accessor :number
38
+ attr_reader :errors
39
+
40
+ def array?; true; end
41
+ def hash?; false; end
42
+
43
+ def read_attribute_for_validation(attr)
44
+ send(attr)
45
+ end
46
+
47
+ def self.human_attribute_name(attr, options = {})
48
+ attr
49
+ end
50
+
51
+ def self.lookup_ancestors
52
+ [self]
53
+ end
54
+ end
55
+
56
+ describe "setting and getting errors", setters_getters: true do
57
+ it "get returns the errors for the provided key" do
58
+ errors = SimpleParams::Errors.new(self)
59
+ errors[:foo] = "omg"
60
+ errors.get(:foo).should eq(["omg"])
61
+ end
62
+
63
+ it "sets the error with the provided key" do
64
+ errors = SimpleParams::Errors.new(self)
65
+ errors.set(:foo, "omg")
66
+ errors.messages.should eq({ foo: "omg" })
67
+ end
68
+
69
+ it "values returns an array of messages" do
70
+ errors = SimpleParams::Errors.new(self)
71
+ errors.set(:foo, "omg")
72
+ errors.set(:baz, "zomg")
73
+ errors.values.should eq(["omg", "zomg"])
74
+ end
75
+
76
+ it "keys returns the error keys" do
77
+ errors = SimpleParams::Errors.new(self)
78
+ errors.set(:foo, "omg")
79
+ errors.set(:baz, "zomg")
80
+ errors.keys.should eq([:foo, :baz])
81
+ end
82
+
83
+ describe "setting on model" do
84
+ it "assign error" do
85
+ car = Car.new
86
+ car.errors[:make] = 'should not be nil'
87
+ car.errors[:make].should eq(["should not be nil"])
88
+ end
89
+
90
+ it "add an error message on a specific attribute" do
91
+ car = Car.new
92
+ car.errors.add(:make, "can not be blank")
93
+ car.errors[:make].should eq(["can not be blank"])
94
+ end
95
+
96
+ it "add an error with a symbol" do
97
+ car = Car.new
98
+ car.errors.add(:make, :blank)
99
+ message = car.errors.generate_message(:make, :blank)
100
+ car.errors[:make].should eq([message])
101
+ end
102
+
103
+ it "add an error with a proc" do
104
+ car = Car.new
105
+ message = Proc.new { "can not be blank" }
106
+ car.errors.add(:make, message)
107
+ car.errors[:make].should eq(["can not be blank"])
108
+ end
109
+ end
110
+ end
111
+
112
+ describe "#clear", clear: true do
113
+ it "clears errors" do
114
+ car = Car.new
115
+ car.errors[:make] = 'should not be nil'
116
+ car.errors[:make].should eq(["should not be nil"])
117
+ car.errors.clear
118
+ car.errors.should be_empty
119
+ end
120
+ end
121
+
122
+ describe "#empty?, #blank?, and #include?", empty: true do
123
+ it "is empty without any errors" do
124
+ car = Car.new
125
+ car.errors.should be_empty
126
+ car.errors.should be_blank
127
+ car.errors.should_not have_key(:make)
128
+ end
129
+
130
+ it "is not empty with errors" do
131
+ car = Car.new
132
+ car.errors[:make] = 'should not be nil'
133
+ car.errors.should_not be_empty
134
+ car.errors.should_not be_blank
135
+ car.errors.should have_key(:make)
136
+ end
137
+ end
138
+
139
+ describe "#added?", added: true do
140
+ it "added? detects if a specific error was added to the object" do
141
+ car = Car.new
142
+ car.errors.add(:make, "can not be blank")
143
+ car.errors.added?(:make, "can not be blank").should be_truthy
144
+ end
145
+
146
+ it "added? handles symbol message" do
147
+ car = Car.new
148
+ car.errors.add(:make, :blank)
149
+ car.errors.added?(:make, :blank).should be_truthy
150
+ end
151
+
152
+ it "added? handles proc messages" do
153
+ car = Car.new
154
+ message = Proc.new { "can not be blank" }
155
+ car.errors.add(:make, message)
156
+ car.errors.added?(:make, message).should be_truthy
157
+ end
158
+
159
+ it "added? defaults message to :invalid" do
160
+ car = Car.new
161
+ car.errors.add(:make)
162
+ car.errors.added?(:make).should be_truthy
163
+ end
164
+
165
+ it "added? matches the given message when several errors are present for the same attribute" do
166
+ car = Car.new
167
+ car.errors.add(:make, "can not be blank")
168
+ car.errors.add(:make, "is invalid")
169
+ car.errors.added?(:make, "can not be blank").should be_truthy
170
+ end
171
+
172
+ it "added? returns false when no errors are present" do
173
+ car = Car.new
174
+ car.errors.added?(:make).should_not be_truthy
175
+ end
176
+
177
+ it "added? returns false when checking a nonexisting error and other errors are present for the given attribute" do
178
+ car = Car.new
179
+ car.errors.add(:make, "is invalid")
180
+ car.errors.added?(:make, "can not be blank").should_not be_truthy
181
+ end
182
+ end
183
+
184
+ describe "#size", size: true do
185
+ it "size calculates the number of error messages" do
186
+ car = Car.new
187
+ car.errors.add(:make, "can not be blank")
188
+ car.errors.size.should eq(1)
189
+ end
190
+ end
191
+
192
+ describe "#to_a", to_a: true do
193
+ it "to_a returns the list of errors with complete messages containing the attribute names" do
194
+ car = Car.new
195
+ car.errors.add(:make, "can not be blank")
196
+ car.errors.add(:make, "can not be nil")
197
+ car.errors.to_a.should eq(["make can not be blank", "make can not be nil"])
198
+ end
199
+ end
200
+
201
+ describe "#to_s", to_s: true do
202
+ it "to_a returns the list of errors with complete messages containing the attribute names" do
203
+ car = Car.new
204
+ car.errors.add(:make, "can not be blank")
205
+ car.errors.add(:make, "can not be nil")
206
+ car.errors.to_s.should eq("make can not be blank, make can not be nil")
207
+ end
208
+
209
+ end
210
+
211
+ describe "#to_hash", to_hash: true do
212
+ it "to_hash returns the error messages hash" do
213
+ car = Car.new
214
+ car.errors.add(:make, "can not be blank")
215
+ car.errors.to_hash.should eq({ make: ["can not be blank"] })
216
+ end
217
+ end
218
+
219
+ describe "#as_json", as_json: true do
220
+ it "as_json creates a json formatted representation of the errors hash" do
221
+ car = Car.new
222
+ car.errors[:make] = 'can not be nil'
223
+ car.errors[:make].should eq(["can not be nil"])
224
+ car.errors.as_json.should eq({ make: ["can not be nil"] })
225
+ end
226
+
227
+ it "as_json with :full_messages option creates a json formatted representation of the errors containing complete messages" do
228
+ car = Car.new
229
+ car.errors[:make] = 'can not be nil'
230
+ car.errors[:make].should eq(["can not be nil"])
231
+ car.errors.as_json(full_messages: true).should eq({ make: ["make can not be nil"] })
232
+ end
233
+ end
234
+
235
+ describe "#full_messages", full_messages: true do
236
+ it "full_messages creates a list of error messages with the attribute name included" do
237
+ car = Car.new
238
+ car.errors.add(:make, "can not be blank")
239
+ car.errors.add(:make, "can not be nil")
240
+ car.errors.full_messages.should eq(["make can not be blank", "make can not be nil"])
241
+ end
242
+
243
+ it "full_messages_for contains all the error messages for the given attribute" do
244
+ car = Car.new
245
+ car.errors.add(:make, "can not be blank")
246
+ car.errors.add(:make, "can not be nil")
247
+ car.errors.full_messages_for(:make).should eq(["make can not be blank", "make can not be nil"])
248
+ end
249
+
250
+ it "full_messages_for does not contain error messages from other attributes" do
251
+ car = Car.new
252
+ car.errors.add(:make, "can not be blank")
253
+ car.errors.add(:model, "can not be blank")
254
+ car.errors.full_messages_for(:make).should eq(["make can not be blank"])
255
+ end
256
+
257
+ it "full_messages_for returns an empty list in case there are no errors for the given attribute" do
258
+ car = Car.new
259
+ car.errors.add(:make, "can not be blank")
260
+ car.errors.full_messages_for(:model).should eq([])
261
+ end
262
+
263
+ it "full_message returns the given message when attribute is :base" do
264
+ car = Car.new
265
+ car.errors.full_message(:base, "press the button").should eq("press the button")
266
+ end
267
+
268
+ it "full_message returns the given message with the attribute name included" do
269
+ car = Car.new
270
+ car.errors.full_message(:make, "can not be blank").should eq("make can not be blank")
271
+ car.errors.full_message(:model, "can not be blank").should eq("model can not be blank")
272
+ end
273
+ end
274
+
275
+ describe "#generate_message", generate_message: true do
276
+ it "generate_message works without i18n_scope" do
277
+ car = Car.new
278
+ Car.should_not respond_to(:i18n_scope)
279
+ expect {
280
+ car.errors.generate_message(:make, :blank)
281
+ }.to_not raise_error
282
+ end
283
+ end
284
+
285
+ describe "#adds_on_empty", add_on_empty: true do
286
+ it "add_on_empty generates message" do
287
+ car = Car.new
288
+ car.errors.should_receive(:generate_message).with(:make, :empty, {})
289
+ car.errors.add_on_empty :make
290
+ end
291
+
292
+ it "add_on_empty generates message for multiple attributes" do
293
+ car = Car.new
294
+ car.errors.should_receive(:generate_message).with(:make, :empty, {})
295
+ car.errors.should_receive(:generate_message).with(:model, :empty, {})
296
+ car.errors.add_on_empty [:make, :model]
297
+ end
298
+
299
+ it "add_on_empty generates message with custom default message" do
300
+ car = Car.new
301
+ car.errors.should_receive(:generate_message).with(:make, :empty, { message: 'custom' })
302
+ car.errors.add_on_empty :make, message: 'custom'
303
+ end
304
+
305
+ it "add_on_empty generates message with empty string value" do
306
+ car = Car.new
307
+ car.make = ''
308
+ car.errors.should_receive(:generate_message).with(:make, :empty, {})
309
+ car.errors.add_on_empty :make
310
+ end
311
+ end
312
+
313
+ describe "#adds_on_blank", add_on_blank: true do
314
+ it "add_on_blank generates message" do
315
+ car = Car.new
316
+ car.errors.should_receive(:generate_message).with(:make, :blank, {})
317
+ car.errors.add_on_blank :make
318
+ end
319
+
320
+ it "add_on_blank generates message for multiple attributes" do
321
+ car = Car.new
322
+ car.errors.should_receive(:generate_message).with(:make, :blank, {})
323
+ car.errors.should_receive(:generate_message).with(:model, :blank, {})
324
+ car.errors.add_on_blank [:make, :model]
325
+ end
326
+
327
+ it "add_on_blank generates message with custom default message" do
328
+ car = Car.new
329
+ car.errors.should_receive(:generate_message).with(:make, :blank, { message: 'custom' })
330
+ car.errors.add_on_blank :make, message: 'custom'
331
+ end
332
+ end
333
+ end
@@ -28,6 +28,10 @@ describe SimpleParams::NestedParams do
28
28
  defined_class.name.should eq("DummyParentClass::MySpecialParams")
29
29
  end
30
30
 
31
+ it "has correct name_symbol", failing: true do
32
+ defined_class.name_symbol.should eq(:my_special_params)
33
+ end
34
+
31
35
  it "has correct parent class" do
32
36
  defined_class.parent_class.should eq(DummyParentClass)
33
37
  end
@@ -102,7 +106,7 @@ describe SimpleParams::NestedParams do
102
106
  end
103
107
  end
104
108
 
105
- subject { defined_class.new({name: "Bill", age: 21}, nil, :my_special_params) }
109
+ subject { defined_class.new(name: "Bill", age: 21) }
106
110
 
107
111
  specify "name" do
108
112
  expect(subject.name).to eq "Bill"
@@ -125,7 +129,7 @@ describe SimpleParams::NestedParams do
125
129
  end
126
130
  end
127
131
 
128
- subject { defined_class.new({ "132" => { name: "Bill", age: 21 } }, nil, :my_special_params) }
132
+ subject { defined_class.new({ "132" => { name: "Bill", age: 21 } }) }
129
133
 
130
134
  specify "name" do
131
135
  expect(subject.name).to eq "Bill"
@@ -150,7 +154,7 @@ describe SimpleParams::NestedParams do
150
154
  end
151
155
  end
152
156
 
153
- subject { defined_class.new({name: "Bill", age: 21}, nil, :my_special_params) }
157
+ subject { defined_class.new(name: "Bill", age: 21) }
154
158
 
155
159
  specify "name" do
156
160
  expect(subject.name).to eq "Bill"
@@ -173,7 +177,7 @@ describe SimpleParams::NestedParams do
173
177
  end
174
178
  end
175
179
 
176
- subject { defined_class.new({ "132" => { name: "Bill", age: 21 } }, nil, :my_special_params) }
180
+ subject { defined_class.new({ "132" => { name: "Bill", age: 21 } }) }
177
181
 
178
182
  specify "name" do
179
183
  expect(subject.name).to eq "Bill"
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.6.4
4
+ version: 1.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-03 00:00:00.000000000 Z
11
+ date: 2015-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -143,6 +143,8 @@ files:
143
143
  - lib/simple_params/formatter.rb
144
144
  - lib/simple_params/hash_builder.rb
145
145
  - lib/simple_params/initialization_hash.rb
146
+ - lib/simple_params/nested_class_list.rb
147
+ - lib/simple_params/nested_errors.rb
146
148
  - lib/simple_params/nested_params.rb
147
149
  - lib/simple_params/nested_params_class_builder.rb
148
150
  - lib/simple_params/params.rb
@@ -169,6 +171,7 @@ files:
169
171
  - spec/formatter_spec.rb
170
172
  - spec/initialization_hash_spec.rb
171
173
  - spec/multiple_classes_spec.rb
174
+ - spec/nested_errors_spec.rb
172
175
  - spec/nested_params_spec.rb
173
176
  - spec/params_spec.rb
174
177
  - spec/rails_integration_spec.rb
@@ -217,6 +220,7 @@ test_files:
217
220
  - spec/formatter_spec.rb
218
221
  - spec/initialization_hash_spec.rb
219
222
  - spec/multiple_classes_spec.rb
223
+ - spec/nested_errors_spec.rb
220
224
  - spec/nested_params_spec.rb
221
225
  - spec/params_spec.rb
222
226
  - spec/rails_integration_spec.rb