Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ActiveModel::Validations::Callbacks should not be required by default.

  • Loading branch information...
commit 0247995d05b3cd3ff5fe32d5fbd8fdd866646909 1 parent 51739d3
@josevalim josevalim authored
View
19 activemodel/lib/active_model/validations.rb
@@ -46,7 +46,6 @@ module ActiveModel
module Validations
extend ActiveSupport::Concern
include ActiveSupport::Callbacks
- include ActiveModel::Validations::Callbacks
included do
extend ActiveModel::Translation
@@ -160,6 +159,17 @@ def errors
@errors ||= Errors.new(self)
end
+ # Runs all the specified validations and returns true if no errors were added
+ # otherwise false. Context can optionally be supplied to define which callbacks
+ # to test against (the context is defined on the validations using :on).
+ def valid?(context = nil)
+ current_context, self.validation_context = validation_context, context
+ errors.clear
+ run_validations!
+ ensure
+ self.validation_context = current_context
+ end
+
# Performs the opposite of <tt>valid?</tt>. Returns true if errors were added,
# false otherwise.
def invalid?(context = nil)
@@ -184,6 +194,13 @@ def invalid?(context = nil)
# end
#
alias :read_attribute_for_validation :send
+
+ protected
+
+ def run_validations!
+ _run_validate_callbacks
+ errors.empty?
+ end
end
end
View
17 activemodel/lib/active_model/validations/callbacks.rb
@@ -46,19 +46,12 @@ def after_validation(*args, &block)
end
end
- # Runs all the specified validations and returns true if no errors were added
- # otherwise false. Context can optionally be supplied to define which callbacks
- # to test against (the context is defined on the validations using :on).
- def valid?(context = nil)
- current_context, self.validation_context = validation_context, context
- errors.clear
- @validate_callback_result = nil
- validation_callback_result = _run_validation_callbacks { @validate_callback_result = _run_validate_callbacks }
- (validation_callback_result && @validate_callback_result) ? errors.empty? : false
- ensure
- self.validation_context = current_context
- end
+ protected
+ # Overwrite run validations to include callbacks.
+ def run_validations!
+ _run_validation_callbacks { super }
+ end
end
end
end
View
1  activemodel/test/cases/validations/callbacks_test.rb
@@ -3,6 +3,7 @@
class Dog
include ActiveModel::Validations
+ include ActiveModel::Validations::Callbacks
attr_accessor :name, :history
View
1  activerecord/lib/active_record/base.rb
@@ -1874,7 +1874,6 @@ def object_from_yaml(string)
extend ActiveSupport::DescendantsTracker
include ActiveModel::Conversion
- include ActiveModel::Validations::Callbacks
include Validations
extend CounterCache
include Locking::Optimistic, Locking::Pessimistic
View
10 activerecord/lib/active_record/callbacks.rb
@@ -234,8 +234,7 @@ module Callbacks
included do
extend ActiveModel::Callbacks
-
- attr_accessor :validation_context
+ include ActiveModel::Validations::Callbacks
define_model_callbacks :initialize, :find, :only => :after
define_model_callbacks :save, :create, :update, :destroy
@@ -249,12 +248,6 @@ def method_added(meth)
send(meth.to_sym, meth.to_sym)
end
end
-
- end
-
- def valid?(*) #:nodoc:
- self.validation_context = new_record? ? :create : :update
- super
end
def destroy #:nodoc:
@@ -269,6 +262,7 @@ def deprecated_callback_method(symbol) #:nodoc:
end
private
+
def create_or_update #:nodoc:
_run_save_callbacks { super }
end
Please sign in to comment.
Something went wrong with that request. Please try again.