active_model-better_errors 1.6.3 → 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.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +37 -0
  3. data/.rspec +4 -1
  4. data/.ruby-gemset +1 -0
  5. data/.travis.yml +5 -1
  6. data/Gemfile +9 -13
  7. data/Gemfile.devtools +66 -0
  8. data/Guardfile +32 -0
  9. data/LICENSE.txt +2 -0
  10. data/README.md +12 -10
  11. data/Rakefile +20 -49
  12. data/active_model-better_errors.gemspec +27 -107
  13. data/config/devtools.yml +4 -0
  14. data/config/flay.yml +3 -0
  15. data/config/flog.yml +2 -0
  16. data/config/mutant.yml +3 -0
  17. data/config/reek.yml +103 -0
  18. data/config/rubocop.yml +62 -0
  19. data/config/yardstick.yml +2 -0
  20. data/lib/active_model/better_errors.rb +64 -4
  21. data/lib/active_model/better_errors/array_reporter.rb +14 -0
  22. data/lib/active_model/{error_collecting → better_errors}/emulation.rb +15 -8
  23. data/lib/active_model/{error_collecting → better_errors}/error_collection.rb +14 -6
  24. data/lib/active_model/{error_collecting → better_errors}/error_message.rb +16 -5
  25. data/lib/active_model/{error_collecting → better_errors}/error_message_set.rb +7 -2
  26. data/lib/active_model/{error_collecting → better_errors}/errors.rb +10 -3
  27. data/lib/active_model/better_errors/formatter.rb +26 -0
  28. data/lib/active_model/better_errors/hash_reporter.rb +14 -0
  29. data/lib/active_model/{error_collecting → better_errors}/human_array_reporter.rb +6 -1
  30. data/lib/active_model/{error_collecting → better_errors}/human_hash_reporter.rb +8 -3
  31. data/lib/active_model/better_errors/human_message_formatter.rb +66 -0
  32. data/lib/active_model/{error_collecting → better_errors}/human_message_reporter.rb +19 -12
  33. data/lib/active_model/{error_collecting → better_errors}/machine_array_reporter.rb +10 -2
  34. data/lib/active_model/{error_collecting → better_errors}/machine_hash_reporter.rb +10 -3
  35. data/lib/active_model/{error_collecting → better_errors}/message_reporter.rb +10 -5
  36. data/lib/active_model/{error_collecting → better_errors}/reporter.rb +7 -2
  37. data/lib/active_model/better_errors/version.rb +10 -0
  38. data/spec/spec_helper.rb +19 -10
  39. data/spec/support/i18n_deprecation_silence.rb +4 -0
  40. data/spec/support/models.rb +11 -4
  41. data/spec/support/string_ext.rb +14 -0
  42. data/spec/{lib/active_model/error_collecting → unit/lib/active_model/better_errors}/emulation_spec.rb +8 -6
  43. data/spec/{lib/active_model/error_collecting → unit/lib/active_model/better_errors}/error_collection_spec.rb +66 -62
  44. data/spec/{lib/active_model/error_collecting → unit/lib/active_model/better_errors}/error_message_set_spec.rb +27 -25
  45. data/spec/unit/lib/active_model/better_errors/error_message_spec.rb +315 -0
  46. data/spec/unit/lib/active_model/better_errors/errors_spec.rb +98 -0
  47. data/spec/unit/lib/active_model/better_errors/human_array_reporter_spec.rb +39 -0
  48. data/spec/unit/lib/active_model/better_errors/human_hash_reporter_spec.rb +37 -0
  49. data/spec/{lib/active_model/error_collecting → unit/lib/active_model/better_errors}/human_message_formatter_spec.rb +13 -7
  50. data/spec/unit/lib/active_model/better_errors/human_message_reporter_spec.rb +65 -0
  51. data/spec/unit/lib/active_model/better_errors/machine_array_reporter_spec.rb +45 -0
  52. data/spec/unit/lib/active_model/better_errors/machine_hash_reporter_spec.rb +45 -0
  53. data/spec/unit/lib/active_model/better_errors_spec.rb +37 -0
  54. metadata +98 -143
  55. data/.document +0 -5
  56. data/VERSION +0 -1
  57. data/lib/active_model/error_collecting.rb +0 -49
  58. data/lib/active_model/error_collecting/array_reporter.rb +0 -9
  59. data/lib/active_model/error_collecting/core_ext.rb +0 -6
  60. data/lib/active_model/error_collecting/hash_reporter.rb +0 -9
  61. data/lib/active_model/error_collecting/human_message_formatter.rb +0 -58
  62. data/spec/lib/active_model/better_errors_spec.rb +0 -7
  63. data/spec/lib/active_model/error_collecting/error_message_spec.rb +0 -293
  64. data/spec/lib/active_model/error_collecting/errors_spec.rb +0 -95
  65. data/spec/lib/active_model/error_collecting/human_array_reporter_spec.rb +0 -33
  66. data/spec/lib/active_model/error_collecting/human_hash_reporter_spec.rb +0 -32
  67. data/spec/lib/active_model/error_collecting/human_message_reporter_spec.rb +0 -61
  68. data/spec/lib/active_model/error_collecting/machine_array_reporter_spec.rb +0 -40
  69. data/spec/lib/active_model/error_collecting/machine_hash_reporter_spec.rb +0 -40
  70. data/spec/lib/active_model/error_collecting_spec.rb +0 -22
  71. data/test/integration.rb +0 -10
@@ -1,14 +1,18 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe ActiveModel::ErrorCollecting::ErrorCollection do
5
+ describe ActiveModel::BetterErrors::ErrorCollection do
4
6
  subject(:collection) { klass.new(base) }
5
- let(:klass) { ActiveModel::ErrorCollecting::ErrorCollection }
7
+ let(:klass) { ActiveModel::BetterErrors::ErrorCollection }
6
8
  let(:base) { User.new }
7
- let(:errors){{
8
- :first_name => [ [ :too_long, { count: 3 } ] ],
9
- 'last_name' => [ [ :invalid, { message: "Invalid" } ] ],
10
- :email => [ :invalid ],
11
- }}
9
+ let(:errors) do
10
+ {
11
+ :first_name => [[:too_long, { count: 3 }]],
12
+ 'last_name' => [[:invalid, { message: 'Invalid' }]],
13
+ :email => [:invalid],
14
+ }
15
+ end
12
16
 
13
17
  before do
14
18
  errors.each do |attribute, error_list|
@@ -18,12 +22,12 @@ describe ActiveModel::ErrorCollecting::ErrorCollection do
18
22
  end
19
23
  end
20
24
 
21
- describe "#clear" do
25
+ describe '#clear' do
22
26
  before { collection.clear }
23
27
  it { should be_empty }
24
28
  end
25
29
 
26
- describe "#include?" do
30
+ describe '#include?' do
27
31
  it { should be_include :first_name }
28
32
  it { should be_include :last_name }
29
33
  it { should be_include :email }
@@ -32,82 +36,82 @@ describe ActiveModel::ErrorCollecting::ErrorCollection do
32
36
  it { should_not be_include 'email' }
33
37
  end
34
38
 
35
- describe "#get" do
39
+ describe '#get' do
36
40
  subject { collection.get(:first_name) }
37
- it { should be_a ActiveModel::ErrorCollecting::ErrorMessageSet }
41
+ it { should be_a ActiveModel::BetterErrors::ErrorMessageSet }
38
42
  its(:length) { should be 1 }
39
43
 
40
- describe "when value is nil" do
44
+ describe 'when value is nil' do
41
45
  before { collection.delete :first_name }
42
46
  it { should be nil }
43
47
  end
44
48
  end
45
49
 
46
- describe "#set" do
50
+ describe '#set' do
47
51
  subject { collection.get :first_name }
48
52
 
49
- describe "when value is array" do
53
+ describe 'when value is array' do
50
54
  before { collection.set(:first_name, []) }
51
- it { should be_a ActiveModel::ErrorCollecting::ErrorMessageSet }
55
+ it { should be_a ActiveModel::BetterErrors::ErrorMessageSet }
52
56
  its(:length) { should be 0 }
53
57
  end
54
58
 
55
- describe "when value is nil" do
56
- before { collection.set(:first_name, nil) }
57
- it { should be_nil }
58
- end
59
+ describe 'when value is nil' do
60
+ before { collection.set(:first_name, nil) }
61
+ it { should be_nil }
62
+ end
59
63
  end
60
64
 
61
- describe "#delete" do
65
+ describe '#delete' do
62
66
  subject { collection.get(:first_name) }
63
67
  before { collection.delete(:first_name) }
64
68
  it { should be_nil }
65
69
  end
66
70
 
67
- describe "#[]" do
71
+ describe '#[]' do
68
72
  subject { collection[:first_name] }
69
73
 
70
- describe "when no error messages" do
74
+ describe 'when no error messages' do
71
75
  before { collection.clear }
72
76
  it { should be_blank }
73
77
  it { should_not be_nil }
74
- it { should be_a ActiveModel::ErrorCollecting::ErrorMessageSet }
78
+ it { should be_a ActiveModel::BetterErrors::ErrorMessageSet }
75
79
  end
76
80
 
77
- describe "when there are error messages" do
81
+ describe 'when there are error messages' do
78
82
  it { should_not be_blank }
79
83
  it { should_not be_nil }
80
- it { should be_a ActiveModel::ErrorCollecting::ErrorMessageSet }
84
+ it { should be_a ActiveModel::BetterErrors::ErrorMessageSet }
81
85
  end
82
86
  end
83
87
 
84
- describe "#[]=" do
88
+ describe '#[]=' do
85
89
  subject(:error_message_set) { collection.get field }
86
90
 
87
- describe "when assigning existing attribute" do
91
+ describe 'when assigning existing attribute' do
88
92
  let(:field) { :first_name }
89
- it "should append to existing set" do
90
- expect {
93
+ it 'should append to existing set' do
94
+ expect do
91
95
  collection[field] = "I'm invalid."
92
- }.to change { error_message_set.length }.by(1)
96
+ end.to change { error_message_set.length }.by(1)
93
97
  end
94
98
  end
95
99
 
96
- describe "when assigning to new attribute" do
100
+ describe 'when assigning to new attribute' do
97
101
  let(:field) { :a_new_attribute }
98
102
  before { collection[field] = "I'm invalid" }
99
103
  it { should_not be_nil }
100
- it { should be_a ActiveModel::ErrorCollecting::ErrorMessageSet }
104
+ it { should be_a ActiveModel::BetterErrors::ErrorMessageSet }
101
105
  its(:length) { should be 1 }
102
106
  end
103
107
  end
104
108
 
105
- describe "#each" do
106
- it "should loop through each error" do
109
+ describe '#each' do
110
+ it 'should loop through each error' do
107
111
  count = 0
108
112
  collection.each do |attribute, error|
109
113
  attribute.should be_a Symbol
110
- error.should be_a ActiveModel::ErrorCollecting::ErrorMessage
114
+ error.should be_a ActiveModel::BetterErrors::ErrorMessage
111
115
  count += 1
112
116
  end
113
117
 
@@ -115,51 +119,51 @@ describe ActiveModel::ErrorCollecting::ErrorCollection do
115
119
  end
116
120
  end
117
121
 
118
- describe "#size" do
122
+ describe '#size' do
119
123
  subject { collection.size }
120
124
  it { should be 3 }
121
125
 
122
- describe "when adding one more error" do
123
- before { collection[:name] = "Not Valid" }
126
+ describe 'when adding one more error' do
127
+ before { collection[:name] = 'Not Valid' }
124
128
  it { should be 4 }
125
129
  end
126
130
  end
127
131
 
128
- describe "#values" do
132
+ describe '#values' do
129
133
  subject { collection.values }
130
134
 
131
135
  it { should be_a Array }
132
136
  its(:length) { should be 3 }
133
137
 
134
- it "should contain ErrorMessageSet as elements" do
138
+ it 'should contain ErrorMessageSet as elements' do
135
139
  collection.values.each do |el|
136
- el.should be_a ActiveModel::ErrorCollecting::ErrorMessageSet
140
+ el.should be_a ActiveModel::BetterErrors::ErrorMessageSet
137
141
  end
138
142
  end
139
143
  end
140
144
 
141
- describe "#keys" do
145
+ describe '#keys' do
142
146
  subject { collection.keys }
143
147
  it { should == [:first_name, :last_name, :email] }
144
148
  end
145
149
 
146
- describe "#to_a" do
150
+ describe '#to_a' do
147
151
  subject { collection.to_a }
148
152
  its(:size) { should == 3 }
149
153
 
150
- describe "when adding one more error" do
151
- before { collection[:name] = "Not Valid" }
154
+ describe 'when adding one more error' do
155
+ before { collection[:name] = 'Not Valid' }
152
156
  its(:size) { should == 4 }
153
157
  end
154
158
 
155
- it "should contain ErrorMessage as elements" do
159
+ it 'should contain ErrorMessage as elements' do
156
160
  collection.to_a.each do |error|
157
- error.should be_a ActiveModel::ErrorCollecting::ErrorMessage
161
+ error.should be_a ActiveModel::BetterErrors::ErrorMessage
158
162
  end
159
163
  end
160
164
  end
161
165
 
162
- describe "#to_hash" do
166
+ describe '#to_hash' do
163
167
  subject { collection.to_hash }
164
168
 
165
169
  it { should be_a Hash }
@@ -167,38 +171,38 @@ describe ActiveModel::ErrorCollecting::ErrorCollection do
167
171
  it { should_not be collection.instance_variable_get(:@collection) }
168
172
  end
169
173
 
170
- describe "#empty?" do
174
+ describe '#empty?' do
171
175
  subject { collection.empty? }
172
- it{ should be false }
176
+ it { should be false }
173
177
 
174
- describe "after clearing the collection" do
178
+ describe 'after clearing the collection' do
175
179
  before { collection.clear }
176
180
 
177
181
  it { should be true }
178
182
  end
179
183
  end
180
184
 
181
- describe "#add" do
182
- it "should add error to collection" do
183
- expect {
184
- collection.add :name, "Invalid"
185
- }.to change { collection[:name].length }.by(1)
185
+ describe '#add' do
186
+ it 'should add error to collection' do
187
+ expect do
188
+ collection.add(:name, 'Invalid')
189
+ end.to change { collection[:name].length }.by(1)
186
190
  end
187
191
  end
188
192
 
189
- describe "#added?" do
190
- describe "when an error message is not added" do
193
+ describe '#added?' do
194
+ describe 'when an error message is not added' do
191
195
  subject { collection.added? :name, :not_there }
192
196
  it { should be false }
193
197
  end
194
198
 
195
- describe "when an error message with the same option is added" do
196
- subject { collection.added? :first_name, :too_long, { :count => 3 } }
199
+ describe 'when an error message with the same option is added' do
200
+ subject { collection.added? :first_name, :too_long, count: 3 }
197
201
  it { should be true }
198
202
  end
199
203
 
200
- describe "when an error message with the different option is added" do
201
- subject { collection.added? :first_name, :too_long, { :count => 4 } }
204
+ describe 'when an error message with the different option is added' do
205
+ subject { collection.added? :first_name, :too_long, count: 4 }
202
206
  it { should be false }
203
207
  end
204
208
  end
@@ -1,66 +1,68 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe ActiveModel::ErrorCollecting::ErrorMessageSet do
5
+ describe ActiveModel::BetterErrors::ErrorMessageSet do
4
6
  subject(:set) { klass.new base, attribute, errors }
5
- let(:klass) { ActiveModel::ErrorCollecting::ErrorMessageSet }
7
+ let(:klass) { ActiveModel::BetterErrors::ErrorMessageSet }
6
8
  let(:attribute) { :first_name }
7
9
  let(:errors) { [] }
8
10
  let(:base) { User.new }
9
11
 
10
- describe "#initialize" do
11
- context "with no errors" do
12
+ describe '#initialize' do
13
+ context 'with no errors' do
12
14
  its(:length) { should == 0 }
13
15
  end
14
16
 
15
- context "with one error" do
17
+ context 'with one error' do
16
18
  let(:errors) { [:invalid] }
17
19
  its(:length) { should == 1 }
18
20
  end
19
21
  end
20
22
 
21
- describe "#push" do
23
+ describe '#push' do
22
24
  before { set.push error, options }
23
25
  let(:error) { :invalid }
24
26
  let(:options) { nil }
25
27
  subject { set.first }
26
28
 
27
- describe "without options" do
28
- it { should be_a ActiveModel::ErrorCollecting::ErrorMessage }
29
- its(:message) { should == nil }
29
+ describe 'without options' do
30
+ it { should be_a ActiveModel::BetterErrors::ErrorMessage }
31
+ its(:message) { should be_nil }
30
32
  its(:type) { should == error }
31
33
  end
32
34
 
33
- describe "with options" do
35
+ describe 'with options' do
34
36
  let(:options) { { message: 'Invalid' } }
35
37
 
36
- it { should be_a ActiveModel::ErrorCollecting::ErrorMessage }
38
+ it { should be_a ActiveModel::BetterErrors::ErrorMessage }
37
39
  its(:message) { should == options[:message] }
38
40
  its(:type) { should == error }
39
41
  end
40
42
  end
41
43
 
42
- describe "#<<" do
44
+ describe '#<<' do
43
45
  before { set << error }
44
46
  subject { set.first }
45
47
 
46
- describe "when accepting error as a symbol" do
48
+ describe 'when accepting error as a symbol' do
47
49
  let(:error) { :invalid }
48
50
 
49
- it { should be_a ActiveModel::ErrorCollecting::ErrorMessage }
50
- its(:message) { should == nil }
51
+ it { should be_a ActiveModel::BetterErrors::ErrorMessage }
52
+ its(:message) { should be_nil }
51
53
  its(:type) { should == error }
52
54
  end
53
55
 
54
- describe "when accepting error as a tuple" do
55
- let(:error) { [ :invalid, { message: "OMG!!" } ]}
56
+ describe 'when accepting error as a tuple' do
57
+ let(:error) { [:invalid, { message: 'OMG!!' }] }
56
58
 
57
- it { should be_a ActiveModel::ErrorCollecting::ErrorMessage }
58
- its(:message) { should == "OMG!!" }
59
+ it { should be_a ActiveModel::BetterErrors::ErrorMessage }
60
+ its(:message) { should == 'OMG!!' }
59
61
  its(:type) { should == :invalid }
60
62
  end
61
63
  end
62
64
 
63
- describe "#to_a" do
65
+ describe '#to_a' do
64
66
  let(:errors) { [:invalid, :too_long] }
65
67
  subject { set.to_a }
66
68
 
@@ -68,23 +70,23 @@ describe ActiveModel::ErrorCollecting::ErrorMessageSet do
68
70
  its(:length) { should == 2 }
69
71
  end
70
72
 
71
- describe "#empty?" do
73
+ describe '#empty?' do
72
74
  subject { set.empty? }
73
75
 
74
- describe "when no error messages" do
76
+ describe 'when no error messages' do
75
77
  it { should be_true }
76
78
  end
77
79
 
78
- describe "when contains error messages" do
80
+ describe 'when contains error messages' do
79
81
  let(:errors) { [:invalid, :too_long] }
80
82
  it { should be_false }
81
83
  end
82
84
  end
83
85
 
84
- describe "#==" do
86
+ describe '#==' do
85
87
  subject { errors == set }
86
88
  let(:errors) { [:invalid] }
87
- let(:expected) { ["is invalid"] }
89
+ let(:expected) { ['is invalid'] }
88
90
  specify do
89
91
  set.should == expected
90
92
  end
@@ -0,0 +1,315 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe ActiveModel::BetterErrors::ErrorMessage do
6
+ let(:klass) { ActiveModel::BetterErrors::ErrorMessage }
7
+ let(:attribute) { :name }
8
+ let(:base) { User.new }
9
+ subject(:error_message) { klass.build(base, attribute, message, options) }
10
+
11
+ describe '.normalize' do
12
+ subject { klass.normalize message }
13
+ context 'when message is a symbol' do
14
+ let(:message) { :invalid }
15
+ it { should == message }
16
+ end
17
+
18
+ context 'when message is nil' do
19
+ let(:message) { nil }
20
+ it { should be nil }
21
+ end
22
+
23
+ context 'when message is a string' do
24
+ let(:message) { 'bad' }
25
+ it { should == message }
26
+ end
27
+
28
+ context 'when message is a proc' do
29
+ let(:message) { proc { 'bad' } }
30
+ it { should == 'bad' }
31
+ end
32
+ end
33
+
34
+ describe '.identify' do
35
+ subject { klass.identify message, override }
36
+ context 'when message is a symbol and' do
37
+ let(:message) { :invalid }
38
+
39
+ context 'when override is a symbol' do
40
+ let(:override) { :not_good }
41
+ it { should == [:not_good, nil] }
42
+ end
43
+
44
+ context 'when override is nil' do
45
+ let(:override) { nil }
46
+ it { should == [:invalid, nil] }
47
+ end
48
+
49
+ context 'when override is a string' do
50
+ let(:override) { 'invalid' }
51
+ it { should == [:invalid, 'invalid'] }
52
+ end
53
+
54
+ context 'when override is a proc' do
55
+ let(:override) { proc { 'Not good' } }
56
+ it { should == [:invalid, 'Not good'] }
57
+ end
58
+ end
59
+
60
+ context 'when message is a string and' do
61
+ let(:message) { 'Invalid' }
62
+
63
+ context 'when override is a symbol' do
64
+ let(:override) { :not_good }
65
+ it { should == [:not_good, 'Invalid'] }
66
+ end
67
+
68
+ context 'when override is nil' do
69
+ let(:override) { nil }
70
+ it { should == [nil, 'Invalid'] }
71
+ end
72
+
73
+ context 'when override is a string' do
74
+ let(:override) { 'not good' }
75
+ it { should == [nil, 'not good'] }
76
+ end
77
+
78
+ context 'when override is a proc' do
79
+ let(:override) { proc { 'Not Good' } }
80
+ it { should == [nil, 'Not Good'] }
81
+ end
82
+ end
83
+
84
+ context 'when message is nil and' do
85
+ let(:message) { nil }
86
+
87
+ context 'when override is a symbol' do
88
+ let(:override) { :not_good }
89
+ it { should == [:not_good, nil] }
90
+ end
91
+
92
+ context 'when override is nil' do
93
+ let(:override) { nil }
94
+ it { should == [nil, nil] }
95
+ end
96
+
97
+ context 'when override is a string' do
98
+ let(:override) { 'not good' }
99
+ it { should == [nil, 'not good'] }
100
+ end
101
+
102
+ context 'when override is a proc' do
103
+ let(:override) { proc { 'Not Good' } }
104
+ it { should == [nil, 'Not Good'] }
105
+ end
106
+ end
107
+
108
+ context 'when message is a proc and' do
109
+ let(:message) { proc { 'invalid' } }
110
+
111
+ context 'when override is a symbol' do
112
+ let(:override) { :not_good }
113
+ it { should == [:not_good, 'invalid'] }
114
+ end
115
+
116
+ context 'when override is nil' do
117
+ let(:override) { nil }
118
+ it { should == [nil, 'invalid'] }
119
+ end
120
+
121
+ context 'when override is a string' do
122
+ let(:override) { 'not good' }
123
+ it { should == [nil, 'not good'] }
124
+ end
125
+
126
+ context 'when override is a proc' do
127
+ let(:override) { proc { 'Not Good' } }
128
+ it { should == [nil, 'Not Good'] }
129
+ end
130
+ end
131
+ end
132
+
133
+ describe '.build' do
134
+ context 'when message is a symbol and' do
135
+ let(:message) { :invalid }
136
+
137
+ context 'when options[:message] is a string' do
138
+ let(:options) { { message: 'Not Really Valid!' } }
139
+ its(:type) { should == message }
140
+ its(:message) { should == options[:message] }
141
+ end
142
+
143
+ context 'when options[:message] is a symbol' do
144
+ let(:options) { { message: :not_really_valid } }
145
+ its(:type) { should == options[:message] }
146
+ its(:message) { should be_nil }
147
+ end
148
+
149
+ context 'when options[:message] is nil' do
150
+ let(:options) { { message: nil } }
151
+ its(:type) { should == message }
152
+ its(:message) { should be_nil }
153
+ end
154
+
155
+ context 'when options[:message] is proc' do
156
+ let(:options) { { message: proc { 'Not Really Valid!' } } }
157
+ its(:type) { should == message }
158
+ its(:message) { should == options[:message].call }
159
+ end
160
+ end
161
+
162
+ context 'when message is a string and' do
163
+ let(:message) { 'Invalid!' }
164
+
165
+ context 'when options[:message] is a string' do
166
+ let(:options) { { message: 'Not Really Valid!' } }
167
+ its(:type) { should be_nil }
168
+ its(:message) { should == options[:message] }
169
+ end
170
+
171
+ context 'when options[:message] is a symbol' do
172
+ let(:options) { { message: :not_really_valid } }
173
+ its(:type) { should == options[:message] }
174
+ its(:message) { should == message }
175
+ end
176
+
177
+ context 'when options[:message] is nil' do
178
+ let(:options) { {} }
179
+ its(:type) { should be_nil }
180
+ its(:message) { should == message }
181
+ end
182
+
183
+ context 'when options[:message] is proc' do
184
+ let(:options) { { message: proc { 'Not Really Valid!' } } }
185
+ its(:type) { should be_nil }
186
+ its(:message) { should == options[:message].call }
187
+ end
188
+ end
189
+
190
+ context 'when message is a proc and' do
191
+ let(:message) { proc { 'Invalid!' } }
192
+
193
+ context 'when options[:message] is a string' do
194
+ let(:options) { { message: 'Not Really Valid!' } }
195
+ its(:type) { should be_nil }
196
+ its(:message) { should == options[:message] }
197
+ end
198
+
199
+ context 'when options[:message] is a symbol' do
200
+ let(:options) { { message: :not_really_valid } }
201
+ its(:type) { should == options[:message] }
202
+ its(:message) { should == message.call }
203
+ end
204
+
205
+ context 'when options[:message] is nil' do
206
+ let(:options) { {} }
207
+ its(:type) { should be_nil }
208
+ its(:message) { should == message.call }
209
+ end
210
+
211
+ context 'when options[:message] is proc' do
212
+ let(:options) { { message: proc { 'Not Really Valid!' } } }
213
+ its(:type) { should be_nil }
214
+ its(:message) { should == options[:message].call }
215
+ end
216
+ end
217
+
218
+ context 'when message is nil and' do
219
+ let(:message) { nil }
220
+
221
+ context 'when options[:message] is a string' do
222
+ let(:options) { { message: 'Not Really Valid!' } }
223
+ its(:type) { should be_nil }
224
+ its(:message) { should == options[:message] }
225
+ end
226
+
227
+ context 'when options[:message] is a symbol' do
228
+ let(:options) { { message: :not_really_valid } }
229
+ its(:type) { should == options[:message] }
230
+ its(:message) { should be_nil }
231
+ end
232
+
233
+ context 'when options[:message] is nil' do
234
+ let(:options) { {} }
235
+ its(:type) { should be_nil }
236
+ its(:message) { should be_nil }
237
+ end
238
+
239
+ context 'when options[:message] is proc' do
240
+ let(:options) { { message: proc { 'Not Really Valid!' } } }
241
+ its(:type) { should be_nil }
242
+ its(:message) { should == options[:message].call }
243
+ end
244
+ end
245
+
246
+ [
247
+ :if, :unless, :on, :allow_nil, :allow_blank, :strict, :message
248
+ ].each do |field|
249
+ context "when options includes :#{field}" do
250
+ let(:message) { :invalid }
251
+ let(:options) { {} }
252
+ before { options[field] = proc {} }
253
+ its(:options) { should == {} }
254
+ end
255
+ end
256
+ end
257
+
258
+ describe '#initialize' do
259
+ let(:message) { :invalid }
260
+ let(:options) { { message: 'Invalid!', count: 12 } }
261
+
262
+ its(:options) { should == { count: 12 } }
263
+ its(:attribute) { should == attribute }
264
+ end
265
+
266
+ describe '#to_hash' do
267
+ let(:message) { :invalid }
268
+ let(:options) { {} }
269
+ let(:expected) do
270
+ { attribute: attribute, type: :invalid, message: nil, options: {} }
271
+ end
272
+
273
+ its(:to_hash) { should == expected }
274
+ end
275
+
276
+ describe '#as_json' do
277
+ let(:message) { :invalid }
278
+ let(:options) { {} }
279
+ let(:expected) do
280
+ { attribute: attribute, type: :invalid, message: nil, options: {} }
281
+ end
282
+
283
+ it 'should respond to as_json taking json arguments' do
284
+ subject.as_json(test: :arguments).should == expected
285
+ end
286
+ end
287
+
288
+ describe '#<=>' do
289
+ subject { e1 <=> e2 }
290
+
291
+ context 'when attribute is different' do
292
+ let(:e1) { klass.build base, :name, :invalid }
293
+ let(:e2) { klass.build base, :name1, :invalid }
294
+ it { should be_nil }
295
+ end
296
+
297
+ context 'when type is different' do
298
+ let(:e1) { klass.build base, :name, :invalid }
299
+ let(:e2) { klass.build base, :name, :invalid1 }
300
+ it { should be_nil }
301
+ end
302
+
303
+ context 'when message is different' do
304
+ let(:e1) { klass.build base, :name, :invalid, message: 'a' }
305
+ let(:e2) { klass.build base, :name, :invalid, message: 'b' }
306
+ it { should be_nil }
307
+ end
308
+
309
+ context 'when it is the same' do
310
+ let(:e1) { klass.build base, :name, :invalid }
311
+ let(:e2) { klass.build base, :name, :invalid }
312
+ it { should == 0 }
313
+ end
314
+ end
315
+ end