Skip to content

Instantly share code, notes, and snippets.

@josevalim
Created January 28, 2009 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josevalim/54238 to your computer and use it in GitHub Desktop.
Save josevalim/54238 to your computer and use it in GitHub Desktop.
module Remarkable # :nodoc:
module ActiveRecord # :nodoc:
module Matchers # :nodoc:
class EnsureConfirmationOfMatcher < Remarkable::Matcher::Base
include Remarkable::ActiveRecord::Helpers
def initialize(*attributes)
load_options(attributes.extract_options!)
@attributes = attributes
end
def message(message)
@options[:message] = message
self
end
def matches?(subject)
@subject = subject
assert_matcher_for(@attributes) do |attribute|
@attribute = attribute
confirmed?
end
end
def description
"ensure confirmation of #{@attributes.to_sentence}"
end
private
def confirmed?
confirmation_assignment = "#{@attribute}_confirmation="
if @subject.respond_to? confirmation_assignment
@subject.send(confirmation_assignment, 'something')
return true if assert_bad_value(@subject, @attribute, 'different', @options[:message])
@missing = "not ensure confirmation of #{@attribute}"
return false
else
@missing = "#{confirmation_assignment.chop} attribute does not exist"
return false
end
end
def load_options(options)
@options = {
:message => default_error_message(:confirmation)
}.merge(options)
end
def expectation
"that the #{model_name} cannot be saved if #{@attribute} is not confirmed"
end
end
# Ensures that the model cannot be saved if one of the attributes is not confirmed.
#
# If an instance variable has been created in the setup named after the
# model being tested, then this method will use that. Otherwise, it will
# create a new instance to test against.
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.confirmation')</tt>
#
# Example:
# it { should ensure_confirmation_of(:email, :password) }
#
def ensure_confirmation_of(*attributes)
EnsureConfirmationOfMatcher.new(*attributes)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment