From 8855163bdbce23fa539be2c1ef2a49f1aabc6458 Mon Sep 17 00:00:00 2001 From: Vince Puzzella Date: Sat, 18 Jan 2014 11:34:38 -0500 Subject: Ability to specify multiple contexts when defining a validation. Example: validates_presence_of :name, on: [:update, :custom_validation_context] --- activemodel/lib/active_model/validations.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 08928a713d..8e76edf945 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -66,8 +66,10 @@ module ActiveModel # end # # Options: - # * :on - Specifies the context where this validation is active - # (e.g. on: :create or on: :custom_validation_context) + # * :on - Specifies the contexts where this validation is active. + # You can pass a symbol or an array of symbols. + # (e.g. on: :create or on: :custom_validation_context or + # on: [:create, :custom_validation_context]) # * :allow_nil - Skip validation if attribute is +nil+. # * :allow_blank - Skip validation if attribute is blank. # * :if - Specifies a method, proc or string to call to determine @@ -124,8 +126,10 @@ module ActiveModel # end # # Options: - # * :on - Specifies the context where this validation is active - # (e.g. on: :create or on: :custom_validation_context) + # * :on - Specifies the contexts where this validation is active. + # You can pass a symbol or an array of symbols. + # (e.g. on: :create or on: :custom_validation_context or + # on: [:create, :custom_validation_context]) # * :if - Specifies a method, proc or string to call to determine # if the validation should occur (e.g. if: :allow_validation, # or if: Proc.new { |user| user.signup_step > 2 }). The method, @@ -141,7 +145,7 @@ module ActiveModel options = options.dup options[:if] = Array(options[:if]) options[:if].unshift lambda { |o| - o.validation_context == options[:on] + Array(options[:on]).include?(o.validation_context) } end args << options -- cgit v1.2.3