module Remarkable
module ActiveModel
module Matchers
class ValidateAcceptanceOfMatcher < Remarkable::ActiveModel::Base #:nodoc:
arguments :collection => :attributes, :as => :attribute
optional :accept, :message
optional :allow_nil, :default => true
collection_assertions :requires_acceptance?, :accept_is_valid?, :allow_nil?
default_options :message => :accepted
protected
def requires_acceptance?
bad?(false)
end
def accept_is_valid?
return true unless @options.key?(:accept)
good?(@options[:accept])
end
end
# Ensures that the model cannot be saved if one of the attributes listed is not accepted.
#
# == Options
#
# * :accept - the expected value to be accepted.
# * :allow_nil - when supplied, validates if it allows nil or not.
# * :message - value the test expects to find in errors[:attribute].
# Regexp, string or symbol. Default = I18n.translate('activerecord.errors.messages.accepted')
#
# == Examples
#
# should_validate_acceptance_of :eula, :terms
# should_validate_acceptance_of :eula, :terms, :accept => true
#
# it { should validate_acceptance_of(:eula, :terms) }
# it { should validate_acceptance_of(:eula, :terms, :accept => true) }
#
def validate_acceptance_of(*attributes, &block)
ValidateAcceptanceOfMatcher.new(*attributes, &block).spec(self)
end
end
end
end