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
|
2014-11-26 09:05:34 -05:00
|
|
|
# complex_operation_that_internally_calls_save!
|
2005-11-03 13:54:16 -05:00
|
|
|
# rescue ActiveRecord::RecordInvalid => invalid
|
|
|
|
# puts invalid.record.errors
|
|
|
|
# end
|
2008-02-09 21:49:11 -05:00
|
|
|
class RecordInvalid < ActiveRecordError
|
2014-11-26 09:05:34 -05:00
|
|
|
attr_reader :record
|
|
|
|
|
|
|
|
def initialize(record)
|
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
|
|
|
|
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={})
|
2014-06-23 09:09:40 -04:00
|
|
|
perform_validations(options) ? super : raise_record_invalid
|
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
|
|
|
#
|
2014-03-23 07:09:55 -04:00
|
|
|
# Aliased as validate.
|
|
|
|
#
|
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
|
|
|
|
2014-03-23 07:09:55 -04:00
|
|
|
alias_method :validate, :valid?
|
|
|
|
|
2014-06-23 09:09:40 -04:00
|
|
|
# Runs all the validations within the specified context. Returns +true+ if
|
|
|
|
# no errors are found, raises +RecordInvalid+ 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
def validate!(context = nil)
|
|
|
|
valid?(context) || raise_record_invalid
|
|
|
|
end
|
|
|
|
|
2010-05-10 05:28:38 -04:00
|
|
|
protected
|
2009-03-20 17:40:37 -04:00
|
|
|
|
2014-06-23 09:09:40 -04:00
|
|
|
def raise_record_invalid
|
|
|
|
raise(RecordInvalid.new(self))
|
|
|
|
end
|
|
|
|
|
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"
|
2013-03-25 10:58:23 -04:00
|
|
|
require "active_record/validations/length"
|