Permalink
Browse files

update AR::Validations documentation [ci skip]

  • Loading branch information...
1 parent b21012b commit d13cd2a7a9d766f450e9a8a7e01f5df54b3dc7b0 Francesco Rodriguez committed Sep 22, 2012
@@ -1,4 +1,4 @@
-require 'active_support/core_ext/hash/indifferent_access'
+ require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/object/duplicable'
require 'thread'
@@ -10,8 +10,8 @@ module ActiveRecord
# puts invalid.record.errors
# end
class RecordInvalid < ActiveRecordError
- attr_reader :record
- def initialize(record)
+ attr_reader :record # :nodoc:
+ def initialize(record) # :nodoc:
@record = record
errors = @record.errors.full_messages.join(", ")
super(I18n.t(:"#{@record.class.i18n_scope}.errors.messages.record_invalid", :errors => errors, :default => :"errors.messages.record_invalid"))
@@ -44,23 +44,24 @@ def create!(attributes = nil, &block)
end
end
- # The validation process on save can be skipped by passing <tt>:validate => false</tt>. The regular Base#save method is
- # replaced with this when the validations module is mixed in, which it is by default.
+ # The validation process on save can be skipped by passing <tt>validate: false</tt>.
+ # The regular Base#save method is replaced with this when the validations
+ # module is mixed in, which it is by default.
def save(options={})
perform_validations(options) ? super : false
end
- # Attempts to save the record just like Base#save but will raise a +RecordInvalid+ exception instead of returning false
- # if the record is not valid.
+ # Attempts to save the record just like Base#save but will raise a +RecordInvalid+
+ # exception instead of returning +false+ if the record is not valid.
def save!(options={})
perform_validations(options) ? super : raise(RecordInvalid.new(self))
end
- # Runs all the validations within the specified context. Returns true if no errors are found,
- # false otherwise.
+ # Runs all the validations within the specified context. Returns +true+ if
+ # no errors are found, +false+ otherwise.
#
- # If the argument is false (default is +nil+), the context is set to <tt>:create</tt> if
- # <tt>new_record?</tt> is true, and to <tt>:update</tt> if it is not.
+ # If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
+ # <tt>new_record?</tt> is +true+, and to <tt>:update</tt> if it is not.
#
# Validations with no <tt>:on</tt> option will run no matter the context. Validations with
# some <tt>:on</tt> option will only run in the specified context.
@@ -72,7 +73,7 @@ def valid?(context = nil)
protected
- def perform_validations(options={})
+ def perform_validations(options={}) # :nodoc:
perform_validation = options[:validate] != false
perform_validation ? valid?(options[:context]) : true
end
@@ -38,7 +38,7 @@ module ClassMethods
# 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
+ # 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)
@@ -1,6 +1,6 @@
module ActiveRecord
module Validations
- class PresenceValidator < ActiveModel::Validations::PresenceValidator
+ class PresenceValidator < ActiveModel::Validations::PresenceValidator # :nodoc:
def validate(record)
super
attributes.each do |attribute|
@@ -29,7 +29,7 @@ module ClassMethods
#
# If you want to validate the presence of a boolean field (where the real values
# are true and false), you will want to use
- # <tt>validates_inclusion_of :field_name, :in => [true, false]</tt>.
+ # <tt>validates_inclusion_of :field_name, in: [true, false]</tt>.
#
# This is due to the way Object#blank? handles boolean values:
# <tt>false.blank? # => true</tt>.
@@ -46,16 +46,15 @@ module ClassMethods
# validation contexts by default (+nil+), other options are <tt>:create</tt>
# and <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.
+ # 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.
+ # 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.
# * <tt>:strict</tt> - Specifies whether validation should be strict.
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
- #
def validates_presence_of(*attr_names)
validates_with PresenceValidator, _merge_attributes(attr_names)
end
@@ -2,7 +2,7 @@
module ActiveRecord
module Validations
- class UniquenessValidator < ActiveModel::EachValidator #:nodoc:
+ class UniquenessValidator < ActiveModel::EachValidator # :nodoc:
def initialize(options)
super(options.reverse_merge(:case_sensitive => true))
end
@@ -199,7 +199,7 @@ module ClassMethods
# can catch it and restart the transaction (e.g. by telling the user
# that the title already exists, and asking him to re-enter the title).
# This technique is also known as optimistic concurrency control:
- # http://en.wikipedia.org/wiki/Optimistic_concurrency_control
+ # http://en.wikipedia.org/wiki/Optimistic_concurrency_control.
#
# The bundled ActiveRecord::ConnectionAdapters distinguish unique index
# constraint errors from other types of database errors by throwing an
@@ -209,10 +209,10 @@ module ClassMethods
#
# The following bundled adapters throw the ActiveRecord::RecordNotUnique exception:
#
- # * ActiveRecord::ConnectionAdapters::MysqlAdapter
- # * ActiveRecord::ConnectionAdapters::Mysql2Adapter
- # * ActiveRecord::ConnectionAdapters::SQLite3Adapter
- # * ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
+ # * ActiveRecord::ConnectionAdapters::MysqlAdapter.
+ # * ActiveRecord::ConnectionAdapters::Mysql2Adapter.
+ # * ActiveRecord::ConnectionAdapters::SQLite3Adapter.
+ # * ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.
def validates_uniqueness_of(*attr_names)
validates_with UniquenessValidator, _merge_attributes(attr_names)
end

0 comments on commit d13cd2a

Please sign in to comment.