4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/rspec_rails_matchers/validations/length_of.rb', line 4
def validate_length_of(attribute, options)
options.assert_valid_keys( :within, :minimum, :maximum )
min, max = min_max_for(options)
Rspec::Matchers::Matcher.new :validate_length_of, attribute do |_attribute_|
match do |model|
validates_minimum?(model, min, _attribute_) &&
validates_maximum?(model, max, _attribute_)
end
failure_message_for_should do |model|
RspecRailsMatchers::Message.error(
:expected =>
[ "%s to validate length of %s, %s", model, _attribute_, options ]
)
end
def validates_minimum?( model, min, attr )
return true if not min
if min && min >= 1
model.send("#{attr}=", 'a' * (min - 1))
model.invalid? && model.errors[attr].any?
end
end
def validates_maximum?( model, max, attr )
return true if not max
if max
model.send("#{attr}=", 'a' * (max + 1))
model.invalid? && model.errors[attr].any?
end
end
end
end
|