From 8d96b89110d2c16d395ec3a5dc13e33be76b8e78 Mon Sep 17 00:00:00 2001 From: Peer Allan Date: Fri, 18 Feb 2011 20:51:56 -0600 Subject: Clarification of ActiveRecord ActiveModel validation documentation --- activerecord/lib/active_record/validations.rb | 16 +++++++++++++++- activerecord/lib/active_record/validations/associated.rb | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 26c1a9db93..c192e02660 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 183acd73b8..0f5f2a6e99 100644 --- a/activerecord/lib/active_record/validations/associated.rb +++ b/activerecord/lib/active_record/validations/associated.rb @@ -33,7 +33,7 @@ module ActiveRecord # # 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. -- cgit v1.2.3