forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_absence_of_matcher.rb
140 lines (131 loc) · 3.75 KB
/
validate_absence_of_matcher.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
module Shoulda
module Matchers
module ActiveModel
# The `validate_absence_of` matcher tests the usage of the
# `validates_absence_of` validation.
#
# class Artillery
# include ActiveModel::Model
# attr_accessor :arms
#
# validates_absence_of :arms
# end
#
# # RSpec
# describe Artillery do
# it { should validate_absence_of(:arms) }
# end
#
# # Minitest (Shoulda)
# class ArtilleryTest < ActiveSupport::TestCase
# should validate_absence_of(:arms)
# end
#
# #### Qualifiers
#
# ##### on
#
# Use `on` if your validation applies only under a certain context.
#
# class Artillery
# include ActiveModel::Model
# attr_accessor :arms
#
# validates_absence_of :arms, on: :create
# end
#
# # RSpec
# describe Artillery do
# it { should validate_absence_of(:arms).on(:create) }
# end
#
# # Minitest (Shoulda)
# class ArtilleryTest < ActiveSupport::TestCase
# should validate_absence_of(:arms).on(:create)
# end
#
# ##### with_message
#
# Use `with_message` if you are using a custom validation message.
#
# class Artillery
# include ActiveModel::Model
# attr_accessor :arms
#
# validates_absence_of :arms,
# message: "We're fresh outta arms here, soldier!"
# end
#
# # RSpec
# describe Artillery do
# it do
# should validate_absence_of(:arms).
# with_message("We're fresh outta arms here, soldier!")
# end
# end
#
# # Minitest (Shoulda)
# class ArtilleryTest < ActiveSupport::TestCase
# should validate_absence_of(:arms).
# with_message("We're fresh outta arms here, soldier!")
# end
#
# @return [ValidateAbsenceOfMatcher}
#
def validate_absence_of(attr)
ValidateAbsenceOfMatcher.new(attr)
end
# @private
class ValidateAbsenceOfMatcher < ValidationMatcher
def with_message(message)
@expected_message = message
self
end
def matches?(subject)
super(subject)
@expected_message ||= :present
disallows_value_of(value, @expected_message)
end
def description
"require #{@attribute} to not be set"
end
private
def value
if reflection
obj = reflection.klass.new
if collection?
[ obj ]
else
obj
end
else
case column_type
when :integer, :float then 1
when :decimal then BigDecimal.new(1, 0)
when :datetime, :time, :timestamp then Time.now
when :date then Date.new
when :binary then "0"
else 'an arbitrary value'
end
end
end
def column_type
@subject.class.respond_to?(:columns_hash) &&
@subject.class.columns_hash[@attribute.to_s].respond_to?(:type) &&
@subject.class.columns_hash[@attribute.to_s].type
end
def collection?
if reflection
[:has_many, :has_and_belongs_to_many].include?(reflection.macro)
else
false
end
end
def reflection
@subject.class.respond_to?(:reflect_on_association) &&
@subject.class.reflect_on_association(@attribute)
end
end
end
end
end