thoughtbot--shoulda-matchers/lib/shoulda/matchers/active_model/validate_confirmation_of_ma...

131 lines
3.4 KiB
Ruby
Raw Normal View History

module Shoulda
2011-10-23 13:41:37 +00:00
module Matchers
module ActiveModel
# The `validate_confirmation_of` matcher tests usage of the
# `validates_confirmation_of` validation.
2011-10-23 13:41:37 +00:00
#
# class User
# include ActiveModel::Model
# attr_accessor :email
#
# validates_confirmation_of :email
# end
#
# # RSpec
# describe User do
# it { should validate_confirmation_of(:email) }
# end
#
# # Test::Unit
# class UserTest < ActiveSupport::TestCase
# should validate_confirmation_of(:email)
# end
#
# #### Qualifiers
#
# ##### 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
#
# # Test::Unit
# class UserTest < ActiveSupport::TestCase
# should validate_confirmation_of(:password).
# with_message('Please re-enter your password')
# end
#
# @return [ValidateConfirmationOfMatcher]
2011-10-23 13:41:37 +00:00
#
def validate_confirmation_of(attr)
ValidateConfirmationOfMatcher.new(attr)
end
# @private
class ValidateConfirmationOfMatcher < ValidationMatcher
2011-10-23 13:41:37 +00:00
include Helpers
attr_reader :attribute, :confirmation_attribute
2011-10-23 13:41:37 +00:00
def initialize(attribute)
super(attribute)
@confirmation_attribute = "#{attribute}_confirmation"
2011-10-23 13:41:37 +00:00
end
def with_message(message)
@message = message if message
self
end
def description
"require #{@confirmation_attribute} to match #{@attribute}"
2011-10-23 13:41:37 +00:00
end
def matches?(subject)
super(subject)
@message ||= :confirmation
disallows_different_value &&
allows_same_value &&
allows_missing_confirmation
end
private
def disallows_different_value
2012-12-20 05:04:27 +00:00
set_confirmation('some value')
disallows_value_of('different value') do |matcher|
qualify_matcher(matcher)
end
2011-10-23 13:41:37 +00:00
end
def allows_same_value
2012-12-20 05:04:27 +00:00
set_confirmation('same value')
allows_value_of('same value') do |matcher|
qualify_matcher(matcher)
end
2011-10-23 13:41:37 +00:00
end
def allows_missing_confirmation
set_confirmation(nil)
allows_value_of('any value') do |matcher|
qualify_matcher(matcher)
end
2011-10-23 13:41:37 +00:00
end
def qualify_matcher(matcher)
matcher.with_message(@message,
against: error_attribute,
values: { attribute: attribute }
)
end
2011-10-23 13:41:37 +00:00
def set_confirmation(val)
setter = :"#{@confirmation_attribute}="
2012-12-20 05:04:27 +00:00
if @subject.respond_to?(setter)
2014-02-01 21:09:44 +00:00
@subject.__send__(setter, val)
2012-12-20 05:04:27 +00:00
end
2011-10-23 13:41:37 +00:00
end
def error_attribute
RailsShim.validates_confirmation_of_error_attribute(self)
end
2011-10-23 13:41:37 +00:00
end
end
end
end