2004-11-24 01:04:44 +00:00
|
|
|
module ActiveRecord
|
2008-07-16 12:00:36 +00:00
|
|
|
# Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the
|
2008-02-10 02:49:11 +00:00
|
|
|
# +record+ method to retrieve the record which did not validate.
|
2005-11-03 18:54:16 +00:00
|
|
|
# begin
|
|
|
|
# complex_operation_that_calls_save!_internally
|
|
|
|
# rescue ActiveRecord::RecordInvalid => invalid
|
|
|
|
# puts invalid.record.errors
|
|
|
|
# end
|
2008-02-10 02:49:11 +00:00
|
|
|
class RecordInvalid < ActiveRecordError
|
2005-11-03 18:54:16 +00:00
|
|
|
attr_reader :record
|
2006-02-05 17:39:54 +00:00
|
|
|
def initialize(record)
|
2005-11-03 18:54:16 +00:00
|
|
|
@record = record
|
2006-02-05 17:39:54 +00:00
|
|
|
super("Validation failed: #{@record.errors.full_messages.join(", ")}")
|
2005-11-03 18:54:16 +00:00
|
|
|
end
|
2005-04-18 19:19:23 +00:00
|
|
|
end
|
2005-11-03 18:54:16 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
module Validations
|
2006-04-29 18:10:14 +00:00
|
|
|
def self.included(base) # :nodoc:
|
2005-02-18 17:27:26 +00:00
|
|
|
base.extend ClassMethods
|
2009-03-19 23:28:59 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
base.class_eval do
|
2006-04-29 20:20:22 +00:00
|
|
|
alias_method_chain :save, :validation
|
2006-06-21 20:40:14 +00:00
|
|
|
alias_method_chain :save!, :validation
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2004-12-09 13:37:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2005-11-03 18:54:16 +00: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 04:14:32 +00:00
|
|
|
def create!(attributes = nil, &block)
|
2005-11-03 18:54:16 +00:00
|
|
|
if attributes.is_a?(Array)
|
2008-05-01 04:14:32 +00:00
|
|
|
attributes.collect { |attr| create!(attr, &block) }
|
2005-11-03 18:54:16 +00:00
|
|
|
else
|
|
|
|
object = new(attributes)
|
2008-05-01 04:14:32 +00:00
|
|
|
yield(object) if block_given?
|
2005-11-03 18:54:16 +00:00
|
|
|
object.save!
|
|
|
|
object
|
|
|
|
end
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
2009-03-19 23:28:59 +00:00
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
# 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.
|
|
|
|
def save_with_validation(perform_validation = true)
|
2005-10-02 02:00:50 +00:00
|
|
|
if perform_validation && valid? || !perform_validation
|
|
|
|
save_without_validation
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
|
2005-11-03 18:54:16 +00:00
|
|
|
# Attempts to save the record just like Base#save but will raise a RecordInvalid exception instead of returning false
|
2005-04-18 19:19:23 +00:00
|
|
|
# if the record is not valid.
|
2006-02-28 20:37:21 +00:00
|
|
|
def save_with_validation!
|
2005-11-03 18:54:16 +00:00
|
|
|
if valid?
|
2006-02-28 20:37:21 +00:00
|
|
|
save_without_validation!
|
2005-11-03 18:54:16 +00:00
|
|
|
else
|
|
|
|
raise RecordInvalid.new(self)
|
|
|
|
end
|
2005-04-18 19:19:23 +00:00
|
|
|
end
|
|
|
|
|
2004-11-24 01:04:44 +00:00
|
|
|
# Returns the Errors object that holds all information about attribute error messages.
|
|
|
|
def errors
|
2005-10-02 02:00:50 +00:00
|
|
|
@errors ||= Errors.new(self)
|
2004-11-24 01:04:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|