2004-11-23 20:04:44 -05:00
|
|
|
module ActiveRecord
|
2011-02-18 21:51:56 -05:00
|
|
|
# = Active Record RecordInvalid
|
2010-06-16 13:55:15 -04:00
|
|
|
#
|
2011-05-23 19:58:25 -04:00
|
|
|
# Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the
|
2008-02-09 21:49:11 -05:00
|
|
|
# +record+ method to retrieve the record which did not validate.
|
2010-06-16 13:55:15 -04:00
|
|
|
#
|
2005-11-03 13:54:16 -05:00
|
|
|
# begin
|
|
|
|
# complex_operation_that_calls_save!_internally
|
|
|
|
# rescue ActiveRecord::RecordInvalid => invalid
|
|
|
|
# puts invalid.record.errors
|
|
|
|
# end
|
2008-02-09 21:49:11 -05:00
|
|
|
class RecordInvalid < ActiveRecordError
|
2012-09-28 14:26:33 -04:00
|
|
|
attr_reader :record # :nodoc:
|
2012-09-22 19:26:00 -04:00
|
|
|
def initialize(record) # :nodoc:
|
2005-11-03 13:54:16 -05:00
|
|
|
@record = record
|
2010-01-30 07:07:48 -05:00
|
|
|
errors = @record.errors.full_messages.join(", ")
|
2012-03-04 18:17:47 -05:00
|
|
|
super(I18n.t(:"#{@record.class.i18n_scope}.errors.messages.record_invalid", :errors => errors, :default => :"errors.messages.record_invalid"))
|
2005-11-03 13:54:16 -05:00
|
|
|
end
|
2005-04-18 15:19:23 -04:00
|
|
|
end
|
2005-11-03 13:54:16 -05:00
|
|
|
|
2011-02-18 21:51:56 -05:00
|
|
|
# = Active Record Validations
|
|
|
|
#
|
2011-02-21 05:37:08 -05:00
|
|
|
# Active Record includes the majority of its validations from <tt>ActiveModel::Validations</tt>
|
2011-02-18 21:51:56 -05:00
|
|
|
# all of which accept the <tt>:on</tt> argument to define the context where the
|
|
|
|
# validations are active. Active Record will always supply either the context of
|
|
|
|
# <tt>:create</tt> or <tt>:update</tt> dependent on whether the model is a
|
|
|
|
# <tt>new_record?</tt>.
|
2004-11-23 20:04:44 -05:00
|
|
|
module Validations
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-05-29 18:03:23 -04:00
|
|
|
include ActiveModel::Validations
|
2009-03-20 17:40:37 -04:00
|
|
|
|
2004-12-09 08:37:11 -05:00
|
|
|
module ClassMethods
|
2011-04-24 18:44:12 -04:00
|
|
|
# Creates an object just like Base.create but calls <tt>save!</tt> instead of +save+
|
2005-11-03 13:54:16 -05:00
|
|
|
# so an exception is raised if the record is invalid.
|
2012-09-12 12:35:05 -04:00
|
|
|
def create!(attributes = nil, &block)
|
2005-11-03 13:54:16 -05:00
|
|
|
if attributes.is_a?(Array)
|
2012-09-12 12:35:05 -04:00
|
|
|
attributes.collect { |attr| create!(attr, &block) }
|
2005-11-03 13:54:16 -05:00
|
|
|
else
|
2012-09-12 12:35:05 -04:00
|
|
|
object = new(attributes)
|
2008-05-01 00:14:32 -04:00
|
|
|
yield(object) if block_given?
|
2005-11-03 13:54:16 -05:00
|
|
|
object.save!
|
|
|
|
object
|
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2009-03-20 15:12:18 -04:00
|
|
|
|
2012-09-22 19:26:00 -04:00
|
|
|
# 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.
|
2010-05-10 05:28:38 -04:00
|
|
|
def save(options={})
|
2010-05-11 08:45:57 -04:00
|
|
|
perform_validations(options) ? super : false
|
2010-05-08 19:06:05 -04:00
|
|
|
end
|
2010-01-17 08:22:27 -05:00
|
|
|
|
2012-09-22 19:26:00 -04:00
|
|
|
# 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.
|
2010-05-10 05:28:38 -04:00
|
|
|
def save!(options={})
|
2010-05-11 08:45:57 -04:00
|
|
|
perform_validations(options) ? super : raise(RecordInvalid.new(self))
|
2010-05-08 19:06:05 -04:00
|
|
|
end
|
2009-03-20 15:12:18 -04:00
|
|
|
|
2012-09-22 19:26:00 -04:00
|
|
|
# Runs all the validations within the specified context. Returns +true+ if
|
|
|
|
# no errors are found, +false+ otherwise.
|
2011-02-18 21:51:56 -05:00
|
|
|
#
|
2012-09-22 19:26:00 -04:00
|
|
|
# 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.
|
2011-02-18 21:51:56 -05:00
|
|
|
#
|
2011-02-21 05:37:08 -05:00
|
|
|
# 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.
|
2010-05-10 05:28:38 -04:00
|
|
|
def valid?(context = nil)
|
2010-11-28 10:55:48 -05:00
|
|
|
context ||= (new_record? ? :create : :update)
|
2010-06-16 11:30:37 -04:00
|
|
|
output = super(context)
|
|
|
|
errors.empty? && output
|
2010-05-10 05:28:38 -04:00
|
|
|
end
|
2009-03-20 17:40:37 -04:00
|
|
|
|
2010-05-10 05:28:38 -04:00
|
|
|
protected
|
2009-03-20 17:40:37 -04:00
|
|
|
|
2012-09-22 19:26:00 -04:00
|
|
|
def perform_validations(options={}) # :nodoc:
|
2013-03-27 01:39:40 -04:00
|
|
|
options[:validate] == false || valid?(options[:context])
|
2005-04-18 15:19:23 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
2009-03-19 19:36:08 -04:00
|
|
|
|
2010-05-08 19:06:05 -04:00
|
|
|
require "active_record/validations/associated"
|
|
|
|
require "active_record/validations/uniqueness"
|
2012-06-22 12:09:48 -04:00
|
|
|
require "active_record/validations/presence"
|