diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-12-10 00:47:55 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-12-10 00:47:55 +0000 |
commit | 1542886e40e89905d75f53af8fd5ecaf42135d55 (patch) | |
tree | 013ddeb2a3a0f151a238f9c7e1eeaff73f3aac1f /activerecord | |
parent | 62ddeaf0522a00d1040d19d616238fa47177ba64 (diff) | |
download | rails-1542886e40e89905d75f53af8fd5ecaf42135d55.tar.gz rails-1542886e40e89905d75f53af8fd5ecaf42135d55.tar.bz2 rails-1542886e40e89905d75f53af8fd5ecaf42135d55.zip |
Document the validates class method. Closes #10216 [Farley Knight]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8345 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rwxr-xr-x | activerecord/lib/active_record/validations.rb | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 79e009a413..1845f14116 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -297,7 +297,33 @@ module ActiveRecord :equal_to => '==', :less_than => '<', :less_than_or_equal_to => '<=', :odd => 'odd?', :even => 'even?' }.freeze - + # Adds a validation method or block to the class. This is useful when + # overriding the #validate instance method becomes too unwieldly and + # you're looking for more descriptive declaration of your validations. + # + # This can be done with a symbol pointing to a method: + # + # class Comment < ActiveRecord::Base + # validate :must_be_friends + # + # def must_be_friends + # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee) + # end + # end + # + # Or with a block which is passed the current record to be validated: + # + # class Comment < ActiveRecord::Base + # validate do |comment| + # comment.must_be_friends + # end + # + # def must_be_friends + # errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee) + # end + # end + # + # This usage applies to #validate_on_create and #validate_on_update as well. def validate(*methods, &block) methods << block if block_given? write_inheritable_set(:validate, methods) |