diff options
Diffstat (limited to 'activerecord/lib/active_record/validations.rb')
-rw-r--r-- | activerecord/lib/active_record/validations.rb | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 7f7d49cdb4..e227212827 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -5,13 +5,14 @@ module ActiveRecord # +record+ method to retrieve the record which did not validate. # # begin - # complex_operation_that_calls_save!_internally + # complex_operation_that_internally_calls_save! # rescue ActiveRecord::RecordInvalid => invalid # puts invalid.record.errors # end class RecordInvalid < ActiveRecordError - attr_reader :record # :nodoc: - def initialize(record) # :nodoc: + attr_reader :record + + def initialize(record) @record = record errors = @record.errors.full_messages.join(", ") super(I18n.t(:"#{@record.class.i18n_scope}.errors.messages.record_invalid", :errors => errors, :default => :"errors.messages.record_invalid")) @@ -39,7 +40,7 @@ module ActiveRecord # 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!(options={}) - perform_validations(options) ? super : raise_record_invalid + perform_validations(options) ? super : raise_validation_error end # Runs all the validations within the specified context. Returns +true+ if @@ -60,21 +61,9 @@ module ActiveRecord alias_method :validate, :valid? - # 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 - protected - def raise_record_invalid + def raise_validation_error raise(RecordInvalid.new(self)) end @@ -87,3 +76,4 @@ end require "active_record/validations/associated" require "active_record/validations/uniqueness" require "active_record/validations/presence" +require "active_record/validations/length" |