diff options
author | Xavier Noria <fxn@hashref.com> | 2010-05-09 11:46:45 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-05-09 11:46:45 +0200 |
commit | e1a0d86fe0355ddff8c86db0f42f3824ffe14c02 (patch) | |
tree | 5833022ca41905f243c15c8a5ffdf864de69bfa6 /activerecord/lib/active_record/validations.rb | |
parent | 1ff3d951e620ddeeb97e87e2024391470e886467 (diff) | |
parent | df508bd97062b871fe25eda8d1bb61cd43d79bc4 (diff) | |
download | rails-e1a0d86fe0355ddff8c86db0f42f3824ffe14c02.tar.gz rails-e1a0d86fe0355ddff8c86db0f42f3824ffe14c02.tar.bz2 rails-e1a0d86fe0355ddff8c86db0f42f3824ffe14c02.zip |
Merge remote branch 'rails/master'
Diffstat (limited to 'activerecord/lib/active_record/validations.rb')
-rw-r--r-- | activerecord/lib/active_record/validations.rb | 69 |
1 files changed, 31 insertions, 38 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 8b266be638..55c4236874 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -19,11 +19,6 @@ module ActiveRecord extend ActiveSupport::Concern include ActiveModel::Validations - included do - alias_method_chain :save, :validation - alias_method_chain :save!, :validation - end - module ClassMethods # Creates an object just like Base.create but calls save! instead of save # so an exception is raised if the record is invalid. @@ -39,42 +34,40 @@ module ActiveRecord end end - 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. - 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 + # 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(options=nil) + return super if valid?(options) + false + end - if perform_validation && valid? || !perform_validation - save_without_validation - else - false - end - end + def save_without_validation! + save!(:validate => false) + 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!(options = nil) + return super if valid?(options) + raise RecordInvalid.new(self) + 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! + # Runs all the specified validations and returns true if no errors were added otherwise false. + def valid?(options = nil) + perform_validation = case options + when NilClass + true + when Hash + options[:validate] != false else - raise RecordInvalid.new(self) - end + ActiveSupport::Deprecation.warn "save(#{options}) is deprecated, please give save(:validate => #{options}) instead", caller + options end - # Runs all the specified validations and returns true if no errors were added otherwise false. - def valid? + if perform_validation errors.clear - @_on_validate = new_record? ? :create : :update + self.validation_context = new_record? ? :create : :update _run_validate_callbacks deprecated_callback_method(:validate) @@ -86,12 +79,12 @@ module ActiveRecord end errors.empty? + else + true end end end end -Dir[File.dirname(__FILE__) + "/validations/*.rb"].sort.each do |path| - filename = File.basename(path) - require "active_record/validations/#{filename}" -end +require "active_record/validations/associated" +require "active_record/validations/uniqueness" |