aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-06-19 18:18:45 +0200
committerJosé Valim <jose.valim@gmail.com>2010-06-19 18:20:02 +0200
commit0247995d05b3cd3ff5fe32d5fbd8fdd866646909 (patch)
tree83cf6bf49a9f6421100ac765f025c508b6995ad1
parent51739d3228d12907d60fb1b0a2b1ef96c55f66a3 (diff)
downloadrails-0247995d05b3cd3ff5fe32d5fbd8fdd866646909.tar.gz
rails-0247995d05b3cd3ff5fe32d5fbd8fdd866646909.tar.bz2
rails-0247995d05b3cd3ff5fe32d5fbd8fdd866646909.zip
ActiveModel::Validations::Callbacks should not be required by default.
-rw-r--r--activemodel/lib/active_model/validations.rb19
-rw-r--r--activemodel/lib/active_model/validations/callbacks.rb17
-rw-r--r--activemodel/test/cases/validations/callbacks_test.rb1
-rwxr-xr-xactiverecord/lib/active_record/base.rb1
-rw-r--r--activerecord/lib/active_record/callbacks.rb10
5 files changed, 26 insertions, 22 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index 31516dc8a9..fa6bd91ff7 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/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 @@ module ActiveModel
@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 @@ module ActiveModel
# end
#
alias :read_attribute_for_validation :send
+
+ protected
+
+ def run_validations!
+ _run_validate_callbacks
+ errors.empty?
+ end
end
end
diff --git a/activemodel/lib/active_model/validations/callbacks.rb b/activemodel/lib/active_model/validations/callbacks.rb
index 2c8798bcdd..afd65d3dd5 100644
--- a/activemodel/lib/active_model/validations/callbacks.rb
+++ b/activemodel/lib/active_model/validations/callbacks.rb
@@ -46,19 +46,12 @@ module ActiveModel
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
diff --git a/activemodel/test/cases/validations/callbacks_test.rb b/activemodel/test/cases/validations/callbacks_test.rb
index 08dcf254c2..67b21eb106 100644
--- a/activemodel/test/cases/validations/callbacks_test.rb
+++ b/activemodel/test/cases/validations/callbacks_test.rb
@@ -3,6 +3,7 @@ require 'cases/helper'
class Dog
include ActiveModel::Validations
+ include ActiveModel::Validations::Callbacks
attr_accessor :name, :history
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index ef8dbdb6e5..def0fdaa2f 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1874,7 +1874,6 @@ module ActiveRecord #:nodoc:
extend ActiveSupport::DescendantsTracker
include ActiveModel::Conversion
- include ActiveModel::Validations::Callbacks
include Validations
extend CounterCache
include Locking::Optimistic, Locking::Pessimistic
diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb
index 42b56a3cf8..997c85ef8c 100644
--- a/activerecord/lib/active_record/callbacks.rb
+++ b/activerecord/lib/active_record/callbacks.rb
@@ -234,8 +234,7 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
end
private
+
def create_or_update #:nodoc:
_run_save_callbacks { super }
end