forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_confirmation_of_matcher.rb
147 lines (134 loc) · 3.94 KB
/
validate_confirmation_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
module Shoulda
module Matchers
module ActiveModel
# The `validate_confirmation_of` matcher tests usage of the
# `validates_confirmation_of` validation.
#
# class User
# include ActiveModel::Model
# attr_accessor :email
#
# validates_confirmation_of :email
# end
#
# # RSpec
# describe User do
# it { should validate_confirmation_of(:email) }
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_confirmation_of(:email)
# end
#
# #### Qualifiers
#
# ##### on
#
# Use `on` if your validation applies only under a certain context.
#
# class User
# include ActiveModel::Model
# attr_accessor :password
#
# validates_confirmation_of :password, on: :create
# end
#
# # RSpec
# describe User do
# it { should validate_confirmation_of(:password).on(:create) }
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_confirmation_of(:password).on(:create)
# end
#
# ##### with_message
#
# Use `with_message` if you are using a custom validation message.
#
# class User
# include ActiveModel::Model
# attr_accessor :password
#
# validates_confirmation_of :password,
# message: 'Please re-enter your password'
# end
#
# # RSpec
# describe User do
# it do
# should validate_confirmation_of(:password).
# with_message('Please re-enter your password')
# end
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_confirmation_of(:password).
# with_message('Please re-enter your password')
# end
#
# @return [ValidateConfirmationOfMatcher]
#
def validate_confirmation_of(attr)
ValidateConfirmationOfMatcher.new(attr)
end
# @private
class ValidateConfirmationOfMatcher < ValidationMatcher
include Helpers
attr_reader :attribute, :confirmation_attribute
def initialize(attribute)
super(attribute)
@confirmation_attribute = "#{attribute}_confirmation"
end
def with_message(message)
@message = message if message
self
end
def description
"require #{@confirmation_attribute} to match #{@attribute}"
end
def matches?(subject)
super(subject)
@message ||= :confirmation
disallows_different_value &&
allows_same_value &&
allows_missing_confirmation
end
private
def disallows_different_value
set_confirmation('some value')
disallows_value_of('different value') do |matcher|
qualify_matcher(matcher)
end
end
def allows_same_value
set_confirmation('same value')
allows_value_of('same value') do |matcher|
qualify_matcher(matcher)
end
end
def allows_missing_confirmation
set_confirmation(nil)
allows_value_of('any value') do |matcher|
qualify_matcher(matcher)
end
end
def qualify_matcher(matcher)
matcher.with_message(@message,
against: confirmation_attribute,
values: { attribute: attribute }
)
end
def set_confirmation(val)
setter = :"#{@confirmation_attribute}="
if @subject.respond_to?(setter)
@subject.__send__(setter, val)
end
end
end
end
end
end