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') 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 From 89b3bc480761ee7afd858ab4a78d5b79be3a267c Mon Sep 17 00:00:00 2001 From: Nicholas Rowe Date: Sat, 19 Feb 2011 23:46:30 -0500 Subject: Typo: fixing the the --- activerecord/lib/active_record/validations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index c192e02660..292de3ee61 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -62,7 +62,7 @@ module ActiveRecord # # * 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 + # returns true 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) -- cgit v1.2.3 From 2a75c190d43dc6ea9d43216d324b7c0f38a1c65d Mon Sep 17 00:00:00 2001 From: Nicholas Rowe Date: Sat, 19 Feb 2011 23:47:12 -0500 Subject: Tpyo: fixing several cases of the the --- activerecord/test/cases/autosave_association_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 0e93b468c1..0dfdaa3443 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -743,7 +743,7 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase 2.times { |i| @pirate.birds.create!(:name => "birds_#{i}") } before = @pirate.birds.map { |c| c.mark_for_destruction ; c } - # Stub the destroy method of the the second child to raise an exception + # Stub the destroy method of the second child to raise an exception class << before.last def destroy(*args) super -- cgit v1.2.3 From 4da0157aaffef6063d5ad2984c4a4e472f15f0d5 Mon Sep 17 00:00:00 2001 From: Rodrigo Navarro Date: Sun, 20 Feb 2011 22:03:29 -0300 Subject: Adding examples --- activerecord/lib/active_record/relation.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index cb684c1109..948f33f24e 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -249,6 +249,7 @@ module ActiveRecord # # Person.destroy_all("last_login < '2004-04-04'") # Person.destroy_all(:status => "inactive") + # Person.where(:age => 0..18).destroy_all def destroy_all(conditions = nil) if conditions where(conditions).destroy_all @@ -298,6 +299,7 @@ module ActiveRecord # # Post.delete_all("person_id = 5 AND (category = 'Something' OR category = 'Else')") # Post.delete_all(["person_id = ? AND (category = ? OR category = ?)", 5, 'Something', 'Else']) + # Post.where(:person_id => 5).where(:category => ['Something', 'Else']).delete_all # # Both calls delete the affected posts all at once with a single DELETE statement. # If you need to destroy dependent associations or call your before_* or -- cgit v1.2.3 From 62fd3346843845cbf0266e8eab895d7cf285c24e Mon Sep 17 00:00:00 2001 From: Rodrigo Navarro Date: Sun, 20 Feb 2011 22:22:52 -0300 Subject: Adding new examples for update_all method --- activerecord/lib/active_record/relation.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 948f33f24e..c6cd8891e3 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -176,6 +176,12 @@ module ActiveRecord # # # Update all books that match conditions, but limit it to 5 ordered by date # Book.update_all "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5 + # + # # Conditions from the current relation also works + # Book.where('title LIKE ?', '%Rails%').update_all(:author => 'David') + # + # # The same idea applies to limit and order + # Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(:author => 'David') def update_all(updates, conditions = nil, options = {}) if conditions || options.present? where(conditions).apply_finder_options(options.slice(:limit, :order)).update_all(updates) -- cgit v1.2.3 From b481574a33764e2db1caf01c233a9c9ac9723780 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 21 Feb 2011 11:37:08 +0100 Subject: copy-edits 8d96b89 --- activerecord/lib/active_record/validations.rb | 14 +++++++------- activerecord/lib/active_record/validations/associated.rb | 4 +++- 2 files changed, 10 insertions(+), 8 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 292de3ee61..d73fce9fd0 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -20,7 +20,7 @@ module ActiveRecord # = Active Record Validations # - # Active Record includes the majority of its validations from ActiveModel::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 @@ -56,14 +56,14 @@ module ActiveRecord perform_validations(options) ? super : raise(RecordInvalid.new(self)) end - # Runs all the specified validations and returns true if no errors were added otherwise false. + # Runs all the validations within the specified context. Returns true if no errors are found, + # false otherwise. # - # ==== Arguments + # If the argument is false (default is +nil+), the context is set to :create if + # new_record? is true, and to :update if it is not. # - # * 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 context will be :create, otherwise :update. Validation contexts - # for each validation can be defined using the :on option + # Validations with no :on option will run no matter the context. Validations with + # some :on option will only run in the specified context. 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 0f5f2a6e99..3a783aeb00 100644 --- a/activerecord/lib/active_record/validations/associated.rb +++ b/activerecord/lib/active_record/validations/associated.rb @@ -33,7 +33,9 @@ module ActiveRecord # # Configuration options: # * :message - A custom error message (default is: "is invalid") - # * :on - Specifies when this validation is active (default is nil, other options :create, :update). + # * :on - Specifies when this validation is active. Runs in all + # validation contexts by default (+nil+), other options are :create + # and :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