Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

generic callback support

  • Loading branch information...
commit 121fa9c91683c6ce40cfb1f04861c0c6a7570671 1 parent b47fe31
ggironda authored
View
2  lib/whyvalidationssuckin96/rails/active_record/base_validation_overrides.rb
@@ -86,7 +86,7 @@ def validations_for_current_lifecycle
def valid_with_lifecycle_checking?
validations_for_current_lifecycle.collect do |validation|
# Checks manually because a 'nil' return is considered a skipped validation, not a failed one.
- (validation.validates? == false) ? false : true
+ !(validation.validates? == false)
end.all?
end
View
34 lib/whyvalidationssuckin96/validation_support.rb
@@ -14,15 +14,36 @@ def self.included(klass_or_mod)
# Instance methods added to any class or module that mixes in ValidationSupport
module InstanceMethods
+ def self.included(klass)
+ klass.module_eval do
+
+ end
+ end
+
# Is this object invalid?
# @return [true, false]
def invalid?
!valid?
end
+ def valid?
+ self.class.run_with_generic_callbacks? ? valid_with_generic_callbacks? : valid_without_generic_callbacks?
+ end
+
# Is this object valid?
+ # Also runs any callbacks if the class has callback support as defined by the
+ # instance method run_callbacks being present.
# @return [true, false]
- def valid?
+ def valid_with_generic_callbacks?
+ run_callbacks(:before_validation)
+ valid_without_generic_callbacks?
+ ensure
+ run_callbacks(:after_validation)
+ end
+
+ # Is this object valid?
+ # @return [true, false]
+ def valid_without_generic_callbacks?
all_validations.collect do |validation|
# Checks manually because a 'nil' return is considered a skipped validation, not a failed one.
(validation.validates? == false) ? false : true
@@ -54,6 +75,17 @@ def all_validations
module ClassMethods
+ # If the class or module has a public 'run_callbacks' instance method, we run validations and fire
+ # appropriate callbacks
+ # @return [true, false]
+ def run_with_generic_callbacks?
+ if defined?(@run_with_generic_callbacks)
+ @run_with_generic_callbacks
+ else
+ @run_with_generic_callbacks = public_instance_methods.include?('run_callbacks')
+ end
+ end
+
# An array of arrays, the first element of each being the validation subclass that will be instantiated
# when validation is performed, the last element being the options the validation will be instantiated
# with.
View
35 test/validation_support_test.rb
@@ -1,6 +1,41 @@
require 'teststrap'
context "validation_support" do
+
+ context "when mixed into a class supporting callbacks" do
+ setup do
+ Class.new do
+ attr_reader :callbacks_run
+
+ def initialize
+ @callbacks_run = []
+ end
+
+ def run_callbacks(callback_name)
+ @callbacks_run << callback_name
+ end
+
+ include WhyValidationsSuckIn96::ValidationSupport
+ setup_validations do
+ validate(:foo) { pass }
+ end
+ end.new
+ end
+
+ should "run the before_validation callback when calling valid?" do
+ topic.callbacks_run.clear
+ topic.valid?
+ topic.callbacks_run
+ end.includes(:before_validation)
+
+ should "run the after_validation callback when calling valid?" do
+ topic.callbacks_run.clear
+ topic.valid?
+ topic.callbacks_run
+ end.includes(:after_validation)
+
+ end # when mixed into a class supporting callbacks
+
context "when mixed into a class" do
setup do
Class.new { include WhyValidationsSuckIn96::ValidationSupport }
Please sign in to comment.
Something went wrong with that request. Please try again.