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

This commit is contained in:
José Valim 2010-06-19 18:18:45 +02:00
parent 51739d3228
commit 0247995d05
5 changed files with 26 additions and 22 deletions

View File

@ -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

View File

@ -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

View File

@ -3,6 +3,7 @@ require 'cases/helper'
class Dog
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
attr_accessor :name, :history

View File

@ -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

View File

@ -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