Skip to content

Commit f7855bc

Browse files
committed
Allow size validator to validate any value that respons to :size
1 parent 6bdd1bc commit f7855bc

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

lib/tram/validators/size_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
class SizeValidator < ActiveModel::EachValidator
88
def validate_each(record, attribute, value)
9-
size = value.size if value.is_a? Array
9+
size = value.size if value.respond_to? :size
1010
Tram::Validators::CONDITIONS.each do |key, block|
1111
check(key, record, attribute, size, &block)
1212
end

spec/tram-validators/size_validator_spec.rb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
it { is_expected.to be_invalid_with_error :foo, :size_equal_to }
3030
end
3131

32-
context "when attribute is not an array" do
33-
let(:foo) { 1 }
32+
context "when attribute has no size" do
33+
let(:foo) { double }
3434
it { is_expected.to be_invalid_with_error :foo, :size_equal_to }
3535
end
3636
end
@@ -53,8 +53,8 @@
5353
it { is_expected.to be_valid }
5454
end
5555

56-
context "when attribute is not an array" do
57-
let(:foo) { 1 }
56+
context "when attribute has no size" do
57+
let(:foo) { double }
5858
it { is_expected.to be_invalid_with_error :foo, :size_other_than }
5959
end
6060
end
@@ -77,8 +77,8 @@
7777
it { is_expected.to be_valid }
7878
end
7979

80-
context "when attribute is not an array" do
81-
let(:foo) { 1 }
80+
context "when attribute has no size" do
81+
let(:foo) { double }
8282
it { is_expected.to be_invalid_with_error :foo, :size_greater_than }
8383
end
8484
end
@@ -106,8 +106,8 @@
106106
it { is_expected.to be_valid }
107107
end
108108

109-
context "when attribute is not an array" do
110-
let(:foo) { 1 }
109+
context "when attribute has no size" do
110+
let(:foo) { double }
111111
it do
112112
is_expected
113113
.to be_invalid_with_error :foo, :size_greater_than_or_equal_to
@@ -133,8 +133,8 @@
133133
it { is_expected.to be_invalid_with_error :foo, :size_less_than }
134134
end
135135

136-
context "when attribute is not an array" do
137-
let(:foo) { 1 }
136+
context "when attribute has no size" do
137+
let(:foo) { double }
138138
it { is_expected.to be_invalid_with_error :foo, :size_less_than }
139139
end
140140
end
@@ -159,8 +159,8 @@
159159
end
160160
end
161161

162-
context "when attribute is not an array" do
163-
let(:foo) { 1 }
162+
context "when attribute has no size" do
163+
let(:foo) { double }
164164
it do
165165
is_expected.to be_invalid_with_error :foo, :size_less_than_or_equal_to
166166
end
@@ -189,8 +189,8 @@
189189
end
190190
end
191191

192-
context "when attribute is not an array" do
193-
let(:foo) { 1 }
192+
context "when attribute has no size" do
193+
let(:foo) { double }
194194
it do
195195
is_expected.to be_invalid_with_error :foo, :size_equal_to_bar_baz
196196
end
@@ -217,8 +217,8 @@
217217
it { is_expected.to be_valid }
218218
end
219219

220-
context "when attribute is not an array" do
221-
let(:foo) { 1 }
220+
context "when attribute has no size" do
221+
let(:foo) { double }
222222
it do
223223
is_expected.to be_invalid_with_error :foo, :size_other_than_bar_baz
224224
end
@@ -247,8 +247,8 @@
247247
it { is_expected.to be_valid }
248248
end
249249

250-
context "when attribute is not an array" do
251-
let(:foo) { 1 }
250+
context "when attribute has no size" do
251+
let(:foo) { double }
252252
it do
253253
is_expected.to be_invalid_with_error :foo, :size_greater_than_bar_baz
254254
end
@@ -280,8 +280,8 @@
280280
it { is_expected.to be_valid }
281281
end
282282

283-
context "when attribute is not an array" do
284-
let(:foo) { 1 }
283+
context "when attribute has no size" do
284+
let(:foo) { double }
285285
it do
286286
is_expected
287287
.to be_invalid_with_error :foo, :size_greater_than_or_equal_to_bar_baz
@@ -307,8 +307,8 @@
307307
it { is_expected.to be_invalid_with_error :foo, :size_less_than_bar_baz }
308308
end
309309

310-
context "when attribute is not an array" do
311-
let(:foo) { 1 }
310+
context "when attribute has no size" do
311+
let(:foo) { double }
312312
it { is_expected.to be_invalid_with_error :foo, :size_less_than_bar_baz }
313313
end
314314
end
@@ -336,8 +336,8 @@
336336
end
337337
end
338338

339-
context "when attribute is not an array" do
340-
let(:foo) { 1 }
339+
context "when attribute has no size" do
340+
let(:foo) { double }
341341
it do
342342
is_expected.to be_invalid_with_error \
343343
:foo, :size_less_than_or_equal_to_bar_baz

0 commit comments

Comments
 (0)