2008-03-31 20:05:48 -04:00
|
|
|
module ActiveModel
|
2012-05-16 11:01:43 -04:00
|
|
|
|
2008-03-31 20:05:48 -04:00
|
|
|
module Validations
|
2012-10-25 21:41:35 -04:00
|
|
|
class AcceptanceValidator < EachValidator # :nodoc:
|
2009-12-23 06:14:00 -05:00
|
|
|
def initialize(options)
|
2013-05-01 20:10:06 -04:00
|
|
|
super({ allow_nil: true, accept: "1" }.merge!(options))
|
2013-05-12 23:59:28 -04:00
|
|
|
setup!(options[:class])
|
2009-12-23 06:14:00 -05:00
|
|
|
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
|
|
|
|
2013-05-12 23:59:28 -04:00
|
|
|
private
|
|
|
|
def setup!(klass)
|
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
|
2012-05-16 02:23:46 -04:00
|
|
|
# terms of service check box (or similar agreement).
|
2008-03-31 20:05:48 -04:00
|
|
|
#
|
|
|
|
# class Person < ActiveRecord::Base
|
|
|
|
# validates_acceptance_of :terms_of_service
|
2012-10-25 21:41:35 -04:00
|
|
|
# validates_acceptance_of :eula, message: 'must be abided'
|
2008-03-31 20:05:48 -04:00
|
|
|
# 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").
|
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.
|
2012-07-10 03:07:38 -04:00
|
|
|
#
|
|
|
|
# There is also a list of default options supported by every validator:
|
2014-01-26 14:47:56 -05:00
|
|
|
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
|
|
|
|
# See <tt>ActiveModel::Validation#validates</tt> for more information.
|
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
|