aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/validations.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/validations.rb')
-rw-r--r--activerecord/lib/active_record/validations.rb69
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"