2004-11-23 20:04:44 -05:00
|
|
|
module ActiveRecord
|
2010-06-16 13:55:15 -04:00
|
|
|
# = Active Record Validations
|
|
|
|
#
|
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.
|
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
|
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
|
2010-01-30 07:07:48 -05:00
|
|
|
errors = @record.errors.full_messages.join(", ")
|
|
|
|
super(I18n.t("activerecord.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
|
|
|
|
2004-12-09 08:37:11 -05:00
|
|
|
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
|
|
|
|
2010-05-08 19:06:05 -04: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.
|
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
|
|
|
|
2010-05-08 19:06:05 -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
|
|
|
|
2010-05-08 19:06:05 -04:00
|
|
|
# Runs all the specified validations and returns true if no errors were added otherwise false.
|
2010-05-10 05:28:38 -04:00
|
|
|
def valid?(context = nil)
|
2010-11-07 09:05:18 -05:00
|
|
|
context ||= (persisted? ? :update : :create)
|
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
|
|
|
|
2010-05-10 05:28:38 -04:00
|
|
|
def perform_validations(options={})
|
2010-09-05 20:20:54 -04:00
|
|
|
perform_validation = options[:validate] != false
|
2010-09-05 20:23:41 -04:00
|
|
|
perform_validation ? valid?(options[:context]) : true
|
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"
|