-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnumericality_of.rb
41 lines (36 loc) · 1.18 KB
/
numericality_of.rb
1
2
3
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
module RSpecRailsMatchers
module Validations
module NumericalityOf
def validate_numericality_of( attribute, options = {} )
RSpec::Matchers::Matcher.new :validate_numericality_of, attribute do |_attr_|
match do |model|
invalid_on_non_numeric?(model, _attr_) &&
(options[:allow_blank] == true ?
invalid_on_blank?(model, _attr_) : true
)
end
failure_message_for_should do |model|
RSpecRailsMatchers::Message.error(
:expected =>
[ "%s to validate numericality of %s, %s", model, _attr_, options ]
)
end
def invalid_on_non_numeric?( model, attr )
model.send("#{attr}=", 'abcd')
model.invalid? &&
model.errors[attr].include?(
I18n::t('errors.messages.not_a_number')
)
end
def invalid_on_blank?( model, attr )
model.send("#{attr}=", nil)
model.valid?
!model.errors[attr].include?(
I18n::t('errors.messages.not_a_number')
)
end
end
end
end
end
end