From 51739d3228d12907d60fb1b0a2b1ef96c55f66a3 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 16 Jun 2010 11:30:37 -0400 Subject: moving before_validation and after_validation functionality from ActiveRecord to ActiveModel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [#4653 state:resolved] Signed-off-by: José Valim --- activemodel/lib/active_model/validations.rb | 14 +---- .../lib/active_model/validations/callbacks.rb | 64 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 activemodel/lib/active_model/validations/callbacks.rb (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 57487cf75a..31516dc8a9 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -3,6 +3,7 @@ require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/hash/keys' require 'active_model/errors' +require 'active_model/validations/callbacks' module ActiveModel @@ -45,6 +46,7 @@ module ActiveModel module Validations extend ActiveSupport::Concern include ActiveSupport::Callbacks + include ActiveModel::Validations::Callbacks included do extend ActiveModel::Translation @@ -158,18 +160,6 @@ 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_validate_callbacks - errors.empty? - ensure - self.validation_context = current_context - end - # Performs the opposite of valid?. Returns true if errors were added, # false otherwise. def invalid?(context = nil) diff --git a/activemodel/lib/active_model/validations/callbacks.rb b/activemodel/lib/active_model/validations/callbacks.rb new file mode 100644 index 0000000000..2c8798bcdd --- /dev/null +++ b/activemodel/lib/active_model/validations/callbacks.rb @@ -0,0 +1,64 @@ +require 'active_support/callbacks' + +module ActiveModel + module Validations + module Callbacks + # == Active Model Validation callbacks + # + # Provides an interface for any class to have before_validation and + # after_validation callbacks. + # + # First, extend ActiveModel::Callbacks from the class you are creating: + # + # class MyModel + # include ActiveModel::Validations::Callbacks + # + # before_validation :do_stuff_before_validation + # after_validation :do_tuff_after_validation + # end + # + # Like other before_* callbacks if before_validation returns false + # then valid? will not be called. + extend ActiveSupport::Concern + + included do + include ActiveSupport::Callbacks + define_callbacks :validation, :terminator => "result == false", :scope => [:kind, :name] + end + + module ClassMethods + def before_validation(*args, &block) + options = args.last + if options.is_a?(Hash) && options[:on] + options[:if] = Array.wrap(options[:if]) + options[:if] << "self.validation_context == :#{options[:on]}" + end + set_callback(:validation, :before, *args, &block) + end + + def after_validation(*args, &block) + options = args.extract_options! + options[:prepend] = true + options[:if] = Array.wrap(options[:if]) + options[:if] << "!halted && value != false" + options[:if] << "self.validation_context == :#{options[:on]}" if options[:on] + set_callback(:validation, :after, *(args << options), &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 + + end + end +end -- cgit v1.2.3 From 0247995d05b3cd3ff5fe32d5fbd8fdd866646909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 19 Jun 2010 18:18:45 +0200 Subject: ActiveModel::Validations::Callbacks should not be required by default. --- activemodel/lib/active_model/validations.rb | 19 ++++++++++++++++++- activemodel/lib/active_model/validations/callbacks.rb | 17 +++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) (limited to 'activemodel/lib') 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 valid?. 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 -- cgit v1.2.3