forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_absence_of_matcher.rb
151 lines (141 loc) · 4.39 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
141
142
143
144
145
146
147
148
149
150
151
module Shoulda
module Matchers
module ActiveModel
# The `validate_absence_of` matcher tests the usage of the
# `validates_absence_of` validation.
#
# class PowerHungryCountry
# include ActiveModel::Model
# attr_accessor :nuclear_weapons
#
# validates_absence_of :nuclear_weapons
# end
#
# # RSpec
# RSpec.describe PowerHungryCountry, type: :model do
# it { should validate_absence_of(:nuclear_weapons) }
# end
#
# # Minitest (Shoulda)
# class PowerHungryCountryTest < ActiveSupport::TestCase
# should validate_absence_of(:nuclear_weapons)
# end
#
# #### Qualifiers
#
# ##### on
#
# Use `on` if your validation applies only under a certain context.
#
# class PowerHungryCountry
# include ActiveModel::Model
# attr_accessor :nuclear_weapons
#
# validates_absence_of :nuclear_weapons, on: :create
# end
#
# # RSpec
# RSpec.describe PowerHungryCountry, type: :model do
# it { should validate_absence_of(:nuclear_weapons).on(:create) }
# end
#
# # Minitest (Shoulda)
# class PowerHungryCountryTest < ActiveSupport::TestCase
# should validate_absence_of(:nuclear_weapons).on(:create)
# end
#
# ##### with_message
#
# Use `with_message` if you are using a custom validation message.
#
# class PowerHungryCountry
# include ActiveModel::Model
# attr_accessor :nuclear_weapons
#
# validates_absence_of :nuclear_weapons,
# message: "there shall be peace on Earth"
# end
#
# # RSpec
# RSpec.describe PowerHungryCountry, type: :model do
# it do
# should validate_absence_of(:nuclear_weapons).
# with_message("there shall be peace on Earth")
# end
# end
#
# # Minitest (Shoulda)
# class PowerHungryCountryTest < ActiveSupport::TestCase
# should validate_absence_of(:nuclear_weapons).
# with_message("there shall be peace on Earth")
# end
#
# @return [ValidateAbsenceOfMatcher}
#
def validate_absence_of(attr)
ValidateAbsenceOfMatcher.new(attr)
end
# @private
class ValidateAbsenceOfMatcher < ValidationMatcher
def initialize(attribute)
super
@expected_message = :present
end
def matches?(subject)
super(subject)
disallows_value_of(value, @expected_message)
end
def does_not_match?(subject)
super(subject)
allows_value_of(value, @expected_message)
end
def simple_description
"validate that :#{@attribute} is empty/falsy"
end
private
def value
if reflection
obj = reflection.klass.new
if collection?
[obj]
else
obj
end
elsif array_column?
['an arbitary value']
else
case column_type
when :integer, :float then 1
when :decimal then BigDecimal(1, 0)
when :datetime, :time, :timestamp then Time.current
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
def array_column?
@subject.class.respond_to?(:columns_hash) &&
@subject.class.columns_hash[@attribute.to_s].respond_to?(:array) &&
@subject.class.columns_hash[@attribute.to_s].array
end
end
end
end
end