diff options
| author | Mingdong Luo <mdluo@nsmss.com> | 2015-01-31 19:23:48 -0800 |
|---|---|---|
| committer | Mingdong Luo <mdluo@nsmss.com> | 2015-01-31 19:23:48 -0800 |
| commit | 549d171a90135999e3c670f489494b7a39dd6dd7 (patch) | |
| tree | 233466527b797fe3ea7c6a7a3673795cea28aebe /activemodel/lib/active_model/validations | |
| parent | c840b18ac31a852d99ff760229f2c087b6961727 (diff) | |
| parent | 70ac072976c8cc6f013f0df3777e54ccae3f4f8c (diff) | |
| download | rails-549d171a90135999e3c670f489494b7a39dd6dd7.tar.gz rails-549d171a90135999e3c670f489494b7a39dd6dd7.tar.bz2 rails-549d171a90135999e3c670f489494b7a39dd6dd7.zip | |
Merge branch 'master' into pr/18316
Conflicts:
activerecord/CHANGELOG.md
Diffstat (limited to 'activemodel/lib/active_model/validations')
| -rw-r--r-- | activemodel/lib/active_model/validations/absence.rb | 2 | ||||
| -rw-r--r-- | activemodel/lib/active_model/validations/acceptance.rb | 8 | ||||
| -rw-r--r-- | activemodel/lib/active_model/validations/callbacks.rb | 5 |
3 files changed, 9 insertions, 6 deletions
diff --git a/activemodel/lib/active_model/validations/absence.rb b/activemodel/lib/active_model/validations/absence.rb index 9b5416fb1d..75bf655578 100644 --- a/activemodel/lib/active_model/validations/absence.rb +++ b/activemodel/lib/active_model/validations/absence.rb @@ -1,6 +1,6 @@ module ActiveModel module Validations - # == Active Model Absence Validator + # == \Active \Model Absence Validator class AbsenceValidator < EachValidator #:nodoc: def validate_each(record, attr_name, value) record.errors.add(attr_name, :present, options) if value.present? diff --git a/activemodel/lib/active_model/validations/acceptance.rb b/activemodel/lib/active_model/validations/acceptance.rb index ac5e79859b..ee160fb483 100644 --- a/activemodel/lib/active_model/validations/acceptance.rb +++ b/activemodel/lib/active_model/validations/acceptance.rb @@ -3,12 +3,12 @@ module ActiveModel module Validations class AcceptanceValidator < EachValidator # :nodoc: def initialize(options) - super({ allow_nil: true, accept: "1" }.merge!(options)) + super({ allow_nil: true, accept: ["1", true] }.merge!(options)) setup!(options[:class]) end def validate_each(record, attribute, value) - unless value == options[:accept] + unless acceptable_option?(value) record.errors.add(attribute, :accepted, options.except(:accept, :allow_nil)) end end @@ -20,6 +20,10 @@ module ActiveModel klass.send(:attr_reader, *attr_readers) klass.send(:attr_writer, *attr_writers) end + + def acceptable_option?(value) + Array(options[:accept]).include?(value) + end end module HelperMethods diff --git a/activemodel/lib/active_model/validations/callbacks.rb b/activemodel/lib/active_model/validations/callbacks.rb index 25ccabd66b..4b58ef66e3 100644 --- a/activemodel/lib/active_model/validations/callbacks.rb +++ b/activemodel/lib/active_model/validations/callbacks.rb @@ -15,15 +15,14 @@ module ActiveModel # after_validation :do_stuff_after_validation # end # - # Like other <tt>before_*</tt> callbacks if +before_validation+ returns - # +false+ then <tt>valid?</tt> will not be called. + # Like other <tt>before_*</tt> callbacks if +before_validation+ throws + # +:abort+ then <tt>valid?</tt> will not be called. module Callbacks extend ActiveSupport::Concern included do include ActiveSupport::Callbacks define_callbacks :validation, - terminator: ->(_,result) { result == false }, skip_after_callbacks_if_terminated: true, scope: [:kind, :name] end |
