diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index efd071fedc7fba842eff54b7945b69103f13d969..5e2c7b411d26bd335a18a8467ef901b7dddce9e7 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -71,8 +71,8 @@ module ClassMethods # end # # Options: - # * :on - Specifies when this validation is active (default is - # :save, other options :create, :update). + # * :on - Specifies the context where this validation is active + # (e.g. :on => :create or :on => :special_rules) # * :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 diff --git a/activemodel/lib/active_model/validations/acceptance.rb b/activemodel/lib/active_model/validations/acceptance.rb index a7296d8e1df150078cbc9ef8b7ed1bffcf9a6420..fba44defc1116279c6f17cd9ffebfec0e7b54395 100644 --- a/activemodel/lib/active_model/validations/acceptance.rb +++ b/activemodel/lib/active_model/validations/acceptance.rb @@ -38,7 +38,7 @@ module HelperMethods # * :message - A custom error message (default is: "must be # accepted"). # * :on - Specifies when this validation is active (default is - # :save, other options are :create and + # nil, other options are :create and # :update). # * :allow_nil - Skip validation if attribute is +nil+ (default # is true). diff --git a/activemodel/lib/active_model/validations/confirmation.rb b/activemodel/lib/active_model/validations/confirmation.rb index 00df10cef00da16ad90c000ca74105d089106daf..bcefc7d888c43a01913f04802b3d68b9e2edc329 100644 --- a/activemodel/lib/active_model/validations/confirmation.rb +++ b/activemodel/lib/active_model/validations/confirmation.rb @@ -46,7 +46,7 @@ module HelperMethods # * :message - A custom error message (default is: "doesn't match # confirmation"). # * :on - Specifies when this validation is active (default is - # :save, other options :create, :update). + # :nil, other options :create, :update). # * :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 diff --git a/activemodel/lib/active_model/validations/format.rb b/activemodel/lib/active_model/validations/format.rb index 104f4034923c7073c3eb3ef54a81ccef8909f8cb..ca8920bce667279c375154b77cd61be260a9edf6 100644 --- a/activemodel/lib/active_model/validations/format.rb +++ b/activemodel/lib/active_model/validations/format.rb @@ -51,7 +51,7 @@ module HelperMethods # * :allow_blank - If set to true, skips this validation if the attribute is blank (default is +false+). # * :with - Regular expression that if the attribute matches will result in a successful validation. # * :without - Regular expression that if the attribute does not match will result in a successful validation. - # * :on - Specifies when this validation is active (default is :save, other options :create, :update). + # * :on - Specifies when this validation is active (default is nil, other options :create, :update). # * :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, proc or string should return or evaluate to a true or false value. diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index 5a46ecb4ace10addbe5777b2205f01de398e7ca2..97a5cb012e3bb15f6800e60cee228d31c8af90eb 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -83,7 +83,7 @@ module HelperMethods # * :too_short - The error message if the attribute goes under the minimum (default is: "is too short (min is %{count} characters)"). # * :wrong_length - The error message if using the :is method and the attribute is the wrong size (default is: "is the wrong length (should be %{count} characters)"). # * :message - The error message to use for a :minimum, :maximum, or :is violation. An alias of the appropriate too_long/too_short/wrong_length message. - # * :on - Specifies when this validation is active (default is :save, other options :create, :update). + # * :on - Specifies the context where this validation is active (e.g. :on => :create or :on => :special_rules) # * :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, proc or string should return or evaluate to a true or false value. diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb index 95fe20de75792b6f527ec13f0ee5fcbde1d142fb..7b155bb7f79a7394ad1116d807b40991b327ea29 100644 --- a/activemodel/lib/active_model/validations/numericality.rb +++ b/activemodel/lib/active_model/validations/numericality.rb @@ -93,7 +93,7 @@ module HelperMethods # # Configuration options: # * :message - A custom error message (default is: "is not a number"). - # * :on - Specifies when this validation is active (default is :save, other options :create, :update). + # * :on - Specifies the context where this validation is active (e.g. :on => :create or :on => :special_rules) # * :only_integer - Specifies whether the value has to be an integer, e.g. an integral value (default is +false+). # * :allow_nil - Skip validation if attribute is +nil+ (default is +false+). Notice that for fixnum and float columns empty strings are converted to +nil+. # * :greater_than - Specifies the value must be greater than the supplied value. diff --git a/activemodel/lib/active_model/validations/presence.rb b/activemodel/lib/active_model/validations/presence.rb index 28c4640b17baec99ddbfb26f2456a502a6ed1d4b..3bfd6f15d18b314e4937aadfa4bbb1fb0d7b2333 100644 --- a/activemodel/lib/active_model/validations/presence.rb +++ b/activemodel/lib/active_model/validations/presence.rb @@ -26,8 +26,7 @@ module HelperMethods # # Configuration options: # * message - A custom error message (default is: "can't be blank"). - # * on - Specifies when this validation is active (default is :save, other options :create, - # :update). + # * :on - Specifies the context where this validation is active (e.g. :on => :create or :on => :special_rules) # * 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, proc or string should return or evaluate to a true or false value. diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 26c1a9db93114b8f804ba0645557f43978784972..c192e02660be148a4e89532382d5d69bfdcf1ad2 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -1,5 +1,5 @@ module ActiveRecord - # = Active Record Validations + # = Active Record RecordInvalid # # Raised by save! and create! when the record is invalid. Use the # +record+ method to retrieve the record which did not validate. @@ -18,6 +18,13 @@ def initialize(record) end end + # = Active Record Validations + # + # Active Record includes the majority of its validations from ActiveModel::Validations + # all of which accept the :on argument to define the context where the + # validations are active. Active Record will always supply either the context of + # :create or :update dependent on whether the model is a + # new_record?. module Validations extend ActiveSupport::Concern include ActiveModel::Validations @@ -50,6 +57,13 @@ def save!(options={}) end # Runs all the specified validations and returns true if no errors were added otherwise false. + # + # ==== Arguments + # + # * context - Context to scope the execution of the validations. Default is nil. + # If nil then the response of new_record? will determine the context. If new_record? + # returns true the the context will be :create, otherwise :update. Validation contexts + # for each validation can be defined using the :on option def valid?(context = nil) context ||= (new_record? ? :create : :update) output = super(context) diff --git a/activerecord/lib/active_record/validations/associated.rb b/activerecord/lib/active_record/validations/associated.rb index 183acd73b81ade1419467fc0182c2d50a60c5987..0f5f2a6e99ebc39b40c83156c6e2683ad39dcea4 100644 --- a/activerecord/lib/active_record/validations/associated.rb +++ b/activerecord/lib/active_record/validations/associated.rb @@ -33,7 +33,7 @@ module ClassMethods # # Configuration options: # * :message - A custom error message (default is: "is invalid") - # * :on - Specifies when this validation is active (default is :save, other options :create, :update). + # * :on - Specifies when this validation is active (default is nil, other options :create, :update). # * :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, proc or string should return or evaluate to a true or false value.