2004-11-23 20:04:44 -05:00
|
|
|
module ActiveRecord
|
2008-07-16 08:00:36 -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.
|
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
|
2005-11-03 13:54:16 -05:00
|
|
|
attr_reader :record
|
2006-02-05 12:39:54 -05:00
|
|
|
def initialize(record)
|
2005-11-03 13:54:16 -05:00
|
|
|
@record = record
|
2009-08-26 13:22:56 -04:00
|
|
|
errors = @record.errors.full_messages.join(I18n.t('support.array.words_connector', :default => ', '))
|
2010-01-06 19:04:25 -05:00
|
|
|
super(I18n.t('errors.messages.record_invalid', :errors => errors))
|
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
|
|
|
|
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
|
|
|
|
2009-05-11 22:23:47 -04:00
|
|
|
included do
|
|
|
|
alias_method_chain :save, :validation
|
|
|
|
alias_method_chain :save!, :validation
|
2004-12-09 08:37:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2005-11-03 13:54:16 -05:00
|
|
|
# Creates an object just like Base.create but calls save! instead of save
|
|
|
|
# so an exception is raised if the record is invalid.
|
2008-05-01 00:14:32 -04:00
|
|
|
def create!(attributes = nil, &block)
|
2005-11-03 13:54:16 -05:00
|
|
|
if attributes.is_a?(Array)
|
2008-05-01 00:14:32 -04:00
|
|
|
attributes.collect { |attr| create!(attr, &block) }
|
2005-11-03 13:54:16 -05:00
|
|
|
else
|
|
|
|
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
|
|
|
|
|
|
|
module InstanceMethods
|
|
|
|
# The validation process on save can be skipped by passing false. The regular Base#save method is
|
|
|
|
# replaced with this when the validations module is mixed in, which it is by default.
|
2010-01-17 08:22:27 -05:00
|
|
|
def save_with_validation(options=nil)
|
|
|
|
perform_validation = case options
|
|
|
|
when NilClass
|
|
|
|
true
|
|
|
|
when Hash
|
|
|
|
options[:validate] != false
|
|
|
|
else
|
|
|
|
ActiveSupport::Deprecation.warn "save(#{options}) is deprecated, please give save(:validate => #{options}) instead", caller
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
2009-03-20 15:12:18 -04:00
|
|
|
if perform_validation && valid? || !perform_validation
|
|
|
|
save_without_validation
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
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.
|
|
|
|
def save_with_validation!
|
|
|
|
if valid?
|
|
|
|
save_without_validation!
|
|
|
|
else
|
|
|
|
raise RecordInvalid.new(self)
|
|
|
|
end
|
2005-10-01 22:00:50 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2009-03-20 17:40:37 -04:00
|
|
|
# Runs all the specified validations and returns true if no errors were added otherwise false.
|
|
|
|
def valid?
|
|
|
|
errors.clear
|
|
|
|
|
2009-09-08 11:10:14 -04:00
|
|
|
@_on_validate = new_record? ? :create : :update
|
|
|
|
_run_validate_callbacks
|
2009-03-20 17:40:37 -04:00
|
|
|
|
2009-09-08 11:10:14 -04:00
|
|
|
deprecated_callback_method(:validate)
|
2009-03-20 17:40:37 -04:00
|
|
|
|
|
|
|
if new_record?
|
2009-09-08 11:10:14 -04:00
|
|
|
deprecated_callback_method(:validate_on_create)
|
2009-03-20 17:40:37 -04:00
|
|
|
else
|
2009-09-08 11:10:14 -04:00
|
|
|
deprecated_callback_method(:validate_on_update)
|
2009-03-20 17:40:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
errors.empty?
|
|
|
|
end
|
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
|
|
|
|
|
|
|
Dir[File.dirname(__FILE__) + "/validations/*.rb"].sort.each do |path|
|
|
|
|
filename = File.basename(path)
|
|
|
|
require "active_record/validations/#{filename}"
|
|
|
|
end
|