Class: ActiveModel::Validations::AcceptanceValidator
- Inherits:
-
EachValidator
- Object
- ActiveModel::Validator
- EachValidator
- ActiveModel::Validations::AcceptanceValidator
- Defined in:
- lib/active_model/validations/acceptance.rb
Overview
:nodoc:
Instance Attribute Summary
Attributes inherited from EachValidator
Attributes inherited from ActiveModel::Validator
Instance Method Summary collapse
-
#initialize(options) ⇒ AcceptanceValidator
constructor
A new instance of AcceptanceValidator.
- #setup(klass) ⇒ Object
- #validate_each(record, attribute, value) ⇒ Object
Methods inherited from EachValidator
Methods inherited from ActiveModel::Validator
Constructor Details
#initialize(options) ⇒ AcceptanceValidator
Returns a new instance of AcceptanceValidator.
5 6 7 |
# File 'lib/active_model/validations/acceptance.rb', line 5 def initialize() super({ :allow_nil => true, :accept => "1" }.merge!()) end |
Instance Method Details
#setup(klass) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/active_model/validations/acceptance.rb', line 15 def setup(klass) 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) end |
#validate_each(record, attribute, value) ⇒ Object
9 10 11 12 13 |
# File 'lib/active_model/validations/acceptance.rb', line 9 def validate_each(record, attribute, value) unless value == [:accept] record.errors.add(attribute, :accepted, .except(:accept, :allow_nil)) end end |