2008-03-31 20:05:48 -04:00
|
|
|
module ActiveModel
|
2010-06-15 13:43:30 -04:00
|
|
|
|
|
|
|
# == Active Model Acceptance Validator
|
2008-03-31 20:05:48 -04:00
|
|
|
module Validations
|
2009-12-22 18:36:51 -05:00
|
|
|
class AcceptanceValidator < EachValidator
|
2009-12-23 06:14:00 -05:00
|
|
|
def initialize(options)
|
|
|
|
super(options.reverse_merge(:allow_nil => true, :accept => "1"))
|
|
|
|
end
|
|
|
|
|
2009-12-22 18:36:51 -05:00
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless value == options[:accept]
|
2010-05-19 10:25:46 -04:00
|
|
|
record.errors.add(attribute, :accepted, options.except(:accept, :allow_nil))
|
2009-12-22 18:36:51 -05:00
|
|
|
end
|
|
|
|
end
|
2010-05-19 10:25:46 -04:00
|
|
|
|
2010-01-07 12:44:35 -05:00
|
|
|
def setup(klass)
|
|
|
|
# Note: instance_methods.map(&:to_s) is important for 1.9 compatibility
|
|
|
|
# as instance_methods returns symbols unlike 1.8 which returns strings.
|
2010-04-28 15:24:47 -04:00
|
|
|
attr_readers = attributes.reject { |name| klass.attribute_method?(name) }
|
|
|
|
attr_writers = attributes.reject { |name| klass.attribute_method?("#{name}=") }
|
|
|
|
klass.send(:attr_reader, *attr_readers)
|
|
|
|
klass.send(:attr_writer, *attr_writers)
|
2010-01-07 12:44:35 -05:00
|
|
|
end
|
2009-12-22 18:36:51 -05:00
|
|
|
end
|
|
|
|
|
2010-05-11 06:28:42 -04:00
|
|
|
module HelperMethods
|
2010-08-14 01:13:00 -04:00
|
|
|
# Encapsulates the pattern of wanting to validate the acceptance of a
|
2010-06-15 13:43:30 -04:00
|
|
|
# terms of service check box (or similar agreement). Example:
|
2008-03-31 20:05:48 -04:00
|
|
|
#
|
|
|
|
# class Person < ActiveRecord::Base
|
|
|
|
# validates_acceptance_of :terms_of_service
|
|
|
|
# validates_acceptance_of :eula, :message => "must be abided"
|
|
|
|
# end
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# If the database column does not exist, the +terms_of_service+ attribute
|
2010-06-15 13:43:30 -04:00
|
|
|
# is entirely virtual. This check is performed only if +terms_of_service+
|
|
|
|
# is not +nil+ and by default on save.
|
2008-03-31 20:05:48 -04:00
|
|
|
#
|
|
|
|
# Configuration options:
|
2010-08-14 01:13:00 -04:00
|
|
|
# * <tt>:message</tt> - A custom error message (default is: "must be
|
2010-06-15 13:43:30 -04:00
|
|
|
# accepted").
|
|
|
|
# * <tt>:on</tt> - Specifies when this validation is active (default is
|
2010-08-14 01:13:00 -04:00
|
|
|
# <tt>:save</tt>, other options are <tt>:create</tt> and
|
2010-06-15 13:43:30 -04:00
|
|
|
# <tt>:update</tt>).
|
|
|
|
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+ (default
|
|
|
|
# is true).
|
2010-08-14 01:13:00 -04:00
|
|
|
# * <tt>:accept</tt> - Specifies value that is considered accepted.
|
2010-06-15 13:43:30 -04:00
|
|
|
# The default value is a string "1", which makes it easy to relate to
|
2010-08-14 01:13:00 -04:00
|
|
|
# an HTML checkbox. This should be set to +true+ if you are validating
|
2010-06-15 13:43:30 -04:00
|
|
|
# a database column, since the attribute is typecast from "1" to +true+
|
|
|
|
# before validation.
|
|
|
|
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
|
|
|
# if the validation should occur (e.g. <tt>:if => :allow_validation</tt>,
|
|
|
|
# or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
|
2010-08-14 01:13:00 -04:00
|
|
|
# method, proc or string should return or evaluate to a true or false
|
2010-06-15 13:43:30 -04:00
|
|
|
# value.
|
2010-08-14 01:13:00 -04:00
|
|
|
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
|
|
|
|
# determine if the validation should not occur (for example,
|
|
|
|
# <tt>:unless => :skip_validation</tt>, or
|
2010-06-15 13:43:30 -04:00
|
|
|
# <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>).
|
2010-08-14 01:13:00 -04:00
|
|
|
# The method, proc or string should return or evaluate to a true or
|
2010-06-15 13:43:30 -04:00
|
|
|
# false value.
|
2008-03-31 20:05:48 -04:00
|
|
|
def validates_acceptance_of(*attr_names)
|
2010-01-07 12:44:35 -05:00
|
|
|
validates_with AcceptanceValidator, _merge_attributes(attr_names)
|
2008-03-31 20:05:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-08 21:32:08 -04:00
|
|
|
end
|