2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
module ActiveRecord
|
2015-07-08 06:16:16 -04:00
|
|
|
# = Active Record \RecordInvalid
|
2010-06-16 13:55:15 -04:00
|
|
|
#
|
2015-07-08 06:16:16 -04:00
|
|
|
# Raised by {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] and
|
|
|
|
# {ActiveRecord::Base#create!}[rdoc-ref:Persistence::ClassMethods#create!] when the record is invalid.
|
|
|
|
# Use the #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
|
|
|
|
|
2015-09-05 20:48:15 -04:00
|
|
|
def initialize(record = nil)
|
|
|
|
if record
|
|
|
|
@record = record
|
|
|
|
errors = @record.errors.full_messages.join(", ")
|
|
|
|
message = I18n.t(:"#{@record.class.i18n_scope}.errors.messages.record_invalid", errors: errors, default: :"errors.messages.record_invalid")
|
|
|
|
else
|
|
|
|
message = "Record invalid"
|
|
|
|
end
|
|
|
|
|
|
|
|
super(message)
|
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
|
|
|
|
2015-07-08 06:16:16 -04:00
|
|
|
# = Active Record \Validations
|
2011-02-18 21:51:56 -05:00
|
|
|
#
|
2015-07-08 06:16:16 -04:00
|
|
|
# Active Record includes the majority of its validations from ActiveModel::Validations
|
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
|
2015-07-08 06:16:16 -04:00
|
|
|
# {new_record?}[rdoc-ref:Persistence#new_record?].
|
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>.
|
2015-07-08 06:16:16 -04:00
|
|
|
# The regular {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] method is replaced
|
|
|
|
# with this when the validations module is mixed in, which it is by default.
|
2016-10-28 23:05:58 -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
|
|
|
|
2015-07-08 06:16:16 -04:00
|
|
|
# Attempts to save the record just like {ActiveRecord::Base#save}[rdoc-ref:Base#save] but
|
2016-02-13 06:36:16 -05:00
|
|
|
# will raise an ActiveRecord::RecordInvalid exception instead of returning +false+ if the record is not valid.
|
2016-10-28 23:05:58 -04:00
|
|
|
def save!(options = {})
|
2015-02-20 12:50:49 -05:00
|
|
|
perform_validations(options) ? super : raise_validation_error
|
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
|
|
|
#
|
2015-07-08 06:16:16 -04:00
|
|
|
# Aliased as #validate.
|
2014-03-23 07:09:55 -04: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
|
2015-07-08 06:16:16 -04:00
|
|
|
# {new_record?}[rdoc-ref:Persistence#new_record?] is +true+, and to <tt>:update</tt> if it is not.
|
2011-02-18 21:51:56 -05:00
|
|
|
#
|
2015-07-08 06:16:16 -04:00
|
|
|
# \Validations with no <tt>:on</tt> option will run no matter the context. \Validations with
|
2011-02-21 05:37:08 -05:00
|
|
|
# some <tt>:on</tt> option will only run in the specified context.
|
2010-05-10 05:28:38 -04:00
|
|
|
def valid?(context = nil)
|
2015-09-07 17:41:50 -04:00
|
|
|
context ||= default_validation_context
|
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?
|
|
|
|
|
2016-12-23 01:51:11 -05:00
|
|
|
private
|
2009-03-20 17:40:37 -04:00
|
|
|
|
2016-12-28 21:53:51 -05:00
|
|
|
def default_validation_context
|
2015-09-07 17:41:50 -04:00
|
|
|
new_record? ? :create : :update
|
|
|
|
end
|
|
|
|
|
2016-12-28 21:53:51 -05:00
|
|
|
def raise_validation_error
|
2014-06-23 09:09:40 -04:00
|
|
|
raise(RecordInvalid.new(self))
|
|
|
|
end
|
|
|
|
|
2016-12-23 01:51:11 -05:00
|
|
|
def perform_validations(options = {})
|
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
|
|
|
|
2017-10-21 09:17:03 -04:00
|
|
|
require "active_record/validations/associated"
|
|
|
|
require "active_record/validations/uniqueness"
|
|
|
|
require "active_record/validations/presence"
|
|
|
|
require "active_record/validations/absence"
|
|
|
|
require "active_record/validations/length"
|