diff options
author | José Valim <jose.valim@gmail.com> | 2010-05-10 12:28:38 +0300 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-05-10 12:28:38 +0300 |
commit | 5c245b91d2dbc0b300e8193310b3f950d0cf6c4b (patch) | |
tree | bb5c50da70417ff7951d905cab2a70f14e21ebf6 /activerecord/lib/active_record/validations.rb | |
parent | ce5827ea4791e8b8143919ecceb0231e36e8932e (diff) | |
download | rails-5c245b91d2dbc0b300e8193310b3f950d0cf6c4b.tar.gz rails-5c245b91d2dbc0b300e8193310b3f950d0cf6c4b.tar.bz2 rails-5c245b91d2dbc0b300e8193310b3f950d0cf6c4b.zip |
Make sure valid? preceives the context as in ActiveModel API (ht: Carlos Antonio)
Diffstat (limited to 'activerecord/lib/active_record/validations.rb')
-rw-r--r-- | activerecord/lib/active_record/validations.rb | 47 |
1 files changed, 22 insertions, 25 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 55c4236874..58391168a9 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -36,8 +36,8 @@ module ActiveRecord # 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) + def save(options={}) + return super if perform_validations(options) false end @@ -47,38 +47,35 @@ 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 = nil) - return super if valid?(options) + def save!(options={}) + return super if perform_validations(options) raise RecordInvalid.new(self) end # 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 - ActiveSupport::Deprecation.warn "save(#{options}) is deprecated, please give save(:validate => #{options}) instead", caller - options - end + def valid?(context = nil) + context ||= (new_record? ? :create : :update) + super(context) - if perform_validation - errors.clear + deprecated_callback_method(:validate) + deprecated_callback_method(:"validate_on_#{context}") - self.validation_context = new_record? ? :create : :update - _run_validate_callbacks + errors.empty? + end - deprecated_callback_method(:validate) + protected - if new_record? - deprecated_callback_method(:validate_on_create) - else - deprecated_callback_method(:validate_on_update) - end + def perform_validations(options={}) + perform_validation = case options + when Hash + options[:validate] != false + else + ActiveSupport::Deprecation.warn "save(#{options}) is deprecated, please give save(:validate => #{options}) instead", caller + options + end - errors.empty? + if perform_validation + valid?(options.is_a?(Hash) ? options[:context] : nil) else true end |