Skip to content

Commit

Permalink
revised some conventions in validations docs
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed May 25, 2008
1 parent 80bc36d commit 9d9c96a
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions activerecord/lib/active_record/validations.rb
@@ -1,5 +1,5 @@
module ActiveRecord
# Raised by save! and create! when the record is invalid. Use the
# Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the
# +record+ method to retrieve the record which did not validate.
# begin
# complex_operation_that_calls_save!_internally
Expand Down Expand Up @@ -52,7 +52,7 @@ def initialize(base) # :nodoc:
# Adds an error to the base object instead of any particular attribute. This is used
# to report errors that don't tie to any specific attribute, but rather to the object
# as a whole. These error messages don't get prepended with any field name when iterating
# with each_full, so they should be complete sentences.
# with +each_full+, so they should be complete sentences.
def add_to_base(msg)
add(:base, msg)
end
Expand Down Expand Up @@ -97,7 +97,7 @@ def invalid?(attribute)
!@errors[attribute.to_s].nil?
end

# Returns nil, if no errors are associated with the specified +attribute+.
# Returns +nil+, if no errors are associated with the specified +attribute+.
# Returns the error message, if one error is associated with the specified +attribute+.
# Returns an array of error messages, if more than one error is associated with the specified +attribute+.
#
Expand All @@ -118,7 +118,7 @@ def on(attribute)

alias :[] :on

# Returns errors assigned to the base object through add_to_base according to the normal rules of on(attribute).
# Returns errors assigned to the base object through +add_to_base+ according to the normal rules of <tt>on(attribute)</tt>.
def on_base
on(:base)
end
Expand All @@ -131,15 +131,15 @@ def on_base
# end
#
# company = Company.create(:address => '123 First St.')
# company.errors.each{|attr,msg| puts "#{attr} - #{msg}" } # =>
# name - is too short (minimum is 5 characters)
# name - can't be blank
# address - can't be blank
# company.errors.each{|attr,msg| puts "#{attr} - #{msg}" }
# # => name - is too short (minimum is 5 characters)
# # name - can't be blank
# # address - can't be blank
def each
@errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
end

# Yields each full error message added. So Person.errors.add("first_name", "can't be empty") will be returned
# Yields each full error message added. So <tt>Person.errors.add("first_name", "can't be empty")</tt> will be returned
# through iteration as "First name can't be empty".
#
# class Company < ActiveRecord::Base
Expand All @@ -148,10 +148,10 @@ def each
# end
#
# company = Company.create(:address => '123 First St.')
# company.errors.each_full{|msg| puts msg } # =>
# Name is too short (minimum is 5 characters)
# Name can't be blank
# Address can't be blank
# company.errors.each_full{|msg| puts msg }
# # => Name is too short (minimum is 5 characters)
# # Name can't be blank
# # Address can't be blank
def each_full
full_messages.each { |msg| yield msg }
end
Expand All @@ -164,8 +164,8 @@ def each_full
# end
#
# company = Company.create(:address => '123 First St.')
# company.errors.full_messages # =>
# ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
# company.errors.full_messages
# # => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
def full_messages
full_messages = []

Expand Down Expand Up @@ -209,13 +209,13 @@ def size
# end
#
# company = Company.create(:address => '123 First St.')
# company.errors.to_xml # =>
# <?xml version="1.0" encoding="UTF-8"?>
# <errors>
# <error>Name is too short (minimum is 5 characters)</error>
# <error>Name can't be blank</error>
# <error>Address can't be blank</error>
# </errors>
# company.errors.to_xml
# # => <?xml version="1.0" encoding="UTF-8"?>
# # <errors>
# # <error>Name is too short (minimum is 5 characters)</error>
# # <error>Name can't be blank</error>
# # <error>Address can't be blank</error>
# # </errors>
def to_xml(options={})
options[:root] ||= "errors"
options[:indent] ||= 2
Expand Down Expand Up @@ -261,7 +261,7 @@ def to_xml(options={})
# person.errors.on "phone_number" # => "has invalid format"
# person.errors.each_full { |msg| puts msg }
# # => "Last name can't be empty\n" +
# "Phone number has invalid format"
# # "Phone number has invalid format"
#
# person.attributes = { "last_name" => "Heinemeier", "phone_number" => "555-555" }
# person.save # => true (and person is now saved in the database)
Expand Down

0 comments on commit 9d9c96a

Please sign in to comment.