2014-01-23 18:07:36 +00:00
|
|
|
module Shoulda
|
2011-10-23 13:41:37 +00:00
|
|
|
module Matchers
|
2014-01-23 18:07:36 +00:00
|
|
|
module ActiveModel
|
|
|
|
# The `validate_confirmation_of` matcher tests usage of the
|
|
|
|
# `validates_confirmation_of` validation.
|
2011-10-23 13:41:37 +00:00
|
|
|
#
|
2014-01-23 18:07:36 +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
|
|
|
|
#
|
2015-03-28 14:37:20 +00:00
|
|
|
# ##### 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
|
|
|
|
#
|
|
|
|
# # Test::Unit
|
|
|
|
# class UserTest < ActiveSupport::TestCase
|
|
|
|
# should validate_confirmation_of(:password).on(:create)
|
|
|
|
# end
|
|
|
|
#
|
2014-01-23 18:07:36 +00:00
|
|
|
# ##### 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
|
|
|
|
|
2014-01-23 18:07:36 +00:00
|
|
|
# @private
|
|
|
|
class ValidateConfirmationOfMatcher < ValidationMatcher
|
2011-10-23 13:41:37 +00:00
|
|
|
include Helpers
|
|
|
|
|
2013-07-24 21:46:01 +00:00
|
|
|
attr_reader :attribute, :confirmation_attribute
|
|
|
|
|
2011-10-23 13:41:37 +00:00
|
|
|
def initialize(attribute)
|
2014-06-27 22:39:39 +00:00
|
|
|
super(attribute)
|
2013-07-24 21:46:01 +00:00
|
|
|
@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
|
2013-07-24 21:46:01 +00:00
|
|
|
"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')
|
2013-07-24 21:46:01 +00:00
|
|
|
disallows_value_of('different value') do |matcher|
|
2014-10-15 06:13:17 +00:00
|
|
|
qualify_matcher(matcher)
|
2013-07-24 21:46:01 +00:00
|
|
|
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')
|
2013-07-24 21:46:01 +00:00
|
|
|
allows_value_of('same value') do |matcher|
|
2014-10-15 06:13:17 +00:00
|
|
|
qualify_matcher(matcher)
|
2013-07-24 21:46:01 +00:00
|
|
|
end
|
2011-10-23 13:41:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def allows_missing_confirmation
|
|
|
|
set_confirmation(nil)
|
2013-07-24 21:46:01 +00:00
|
|
|
allows_value_of('any value') do |matcher|
|
2014-10-15 06:13:17 +00:00
|
|
|
qualify_matcher(matcher)
|
2013-07-24 21:46:01 +00:00
|
|
|
end
|
2011-10-23 13:41:37 +00:00
|
|
|
end
|
|
|
|
|
2014-10-15 06:13:17 +00:00
|
|
|
def qualify_matcher(matcher)
|
|
|
|
matcher.with_message(@message,
|
2014-10-22 04:35:12 +00:00
|
|
|
against: confirmation_attribute,
|
2014-10-15 06:13:17 +00:00
|
|
|
values: { attribute: attribute }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2011-10-23 13:41:37 +00:00
|
|
|
def set_confirmation(val)
|
2013-07-24 21:46:01 +00:00
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|