mirror of
https://github.com/github/rails.git
synced 2026-01-14 09:07:58 -05:00
commite6afd8b273Author: Xavier Noria <fxn@hashref.com> Date: Thu May 8 23:49:36 2008 +0200 Overall documentation improvement and markup corrections. Zillion changes. commit2fead68b31Author: Austin Putman <austin@emmanuel.local> Date: Wed May 7 19:35:46 2008 -0700 Documented class methods on ActionController::Routing. These are dangerous, and mostly used for testing. commitf5b84182dbAuthor: Teflon Ted <github@rudiment.net> Date: Wed May 7 16:08:49 2008 -0400 Added explanation about errant inflections not being patched in the future in order to avoid breaking legacy applications. commit370f4f5172Author: Sunny Ripert <negatif@gmail.com> Date: Wed May 7 14:00:59 2008 +0200 Applied list conventions in AR::Base commit5bd18429f0Author: Sunny Ripert <negatif@gmail.com> Date: Wed May 7 13:53:35 2008 +0200 Renamed Options list to Attributes list whenever they weren't option hashes in AR::Base commitd912bd5672Author: Yaroslav Markin <yaroslav@markin.net> Date: Wed May 7 13:50:28 2008 +0400 Add a filter_parameter_logging usage hint to generated ApplicationController. This may help to remind the developer to filter sensitive information from application logs. Closes #11578 commitb243de0db3Author: Jack Danger Canty <git@6brand.com> Date: Tue May 6 23:39:47 2008 -0700 doc: disambiguating an example ActiveRecord class commitf81d771f06Author: Jack Danger Canty <git@6brand.com> Date: Tue May 6 23:35:05 2008 -0700 doc: ActiveRecord::Reflection::AssociationReflection#through_reflection Added documentation demonstrating the use of #through_reflection for finding intervening reflection objects for HasManyThrough and HasOneThrough. commitae6b46f00bAuthor: Cheah Chu Yeow <chuyeow@gmail.com> Date: Wed May 7 13:47:41 2008 +0800 Document AttributeAssignmentError and MultiparameterAssignmentErrors. commit8f463550b5Author: John Barnette <jbarnette@gmail.com> Date: Tue May 6 22:46:44 2008 -0700 Killing/fixing a bunch of outdated language in the AR README. commitaca44bcd92Author: Cheah Chu Yeow <chuyeow@gmail.com> Date: Wed May 7 13:34:52 2008 +0800 Make a note about ActiveResource::Timeouterror being raised when ARes calls timeout. commit284a930a93Author: Jonathan Dance <jd@wuputah.com> Date: Tue May 6 14:58:26 2008 -0400 improvements to the page caching docs commit9482da6213Author: Sunny Ripert <negatif@gmail.com> Date: Mon May 5 18:13:40 2008 +0200 validates_numericality_of() "integer" option really is "only_integer" commite9afd6790aAuthor: Sunny Ripert <negatif@gmail.com> Date: Mon May 5 12:11:59 2008 +0200 Harmonized hash notation in AR::Base commit67ebf14a91Author: Sunny Ripert <negatif@gmail.com> Date: Mon May 5 12:06:19 2008 +0200 Turned options into rdoc-lists in AR::Base commit0ec7c0a41dAuthor: Marshall Huss <mwhuss@Macbook.local> Date: Sun May 4 23:21:33 2008 -0400 Added information of how to set element_name in the case the user has a name confliction with an existing model Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
46 lines
2.3 KiB
Ruby
46 lines
2.3 KiB
Ruby
module ActiveModel
|
|
module Validations
|
|
module ClassMethods
|
|
# Validates whether the associated object or objects are all valid themselves. Works with any kind of association.
|
|
#
|
|
# class Book < ActiveRecord::Base
|
|
# has_many :pages
|
|
# belongs_to :library
|
|
#
|
|
# validates_associated :pages, :library
|
|
# end
|
|
#
|
|
# Warning: If, after the above definition, you then wrote:
|
|
#
|
|
# class Page < ActiveRecord::Base
|
|
# belongs_to :book
|
|
#
|
|
# validates_associated :book
|
|
# end
|
|
#
|
|
# ...this would specify a circular dependency and cause infinite recursion.
|
|
#
|
|
# NOTE: This validation will not fail if the association hasn't been assigned. If you want to ensure that the association
|
|
# is both present and guaranteed to be valid, you also need to use +validates_presence_of+.
|
|
#
|
|
# Configuration options:
|
|
# * <tt>:message</tt> - A custom error message (default is: "is invalid")
|
|
# * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>)
|
|
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
|
|
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
|
|
# method, proc or string should return or evaluate to a true or false value.
|
|
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
|
|
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
|
# method, proc or string should return or evaluate to a true or false value.
|
|
def validates_associated(*attr_names)
|
|
configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save }
|
|
configuration.update(attr_names.extract_options!)
|
|
|
|
validates_each(attr_names, configuration) do |record, attr_name, value|
|
|
record.errors.add(attr_name, configuration[:message]) unless
|
|
(value.is_a?(Array) ? value : [value]).inject(true) { |v, r| (r.nil? || r.valid?) && v }
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end |