diff options
author | José Valim <jose.valim@gmail.com> | 2009-12-29 01:15:39 +0100 |
---|---|---|
committer | Yehuda Katz <wycats@Yehuda-Katz.local> | 2009-12-28 16:19:23 -0800 |
commit | 6d390671f639b3314437054df0d393cef10493bd (patch) | |
tree | 599e7f16cecc26395b897cd9aa0c4f3c530ff930 /activemodel/test | |
parent | 643862e3be1bbe004e2c1a00286b12c5bdc9849a (diff) | |
download | rails-6d390671f639b3314437054df0d393cef10493bd.tar.gz rails-6d390671f639b3314437054df0d393cef10493bd.tar.bz2 rails-6d390671f639b3314437054df0d393cef10493bd.zip |
Move ActiveRecord callbacks implementation to ActiveModel and make use of it.
Signed-off-by: Yehuda Katz <wycats@Yehuda-Katz.local>
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/callbacks_test.rb | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/activemodel/test/cases/callbacks_test.rb b/activemodel/test/cases/callbacks_test.rb new file mode 100644 index 0000000000..b996f51d6b --- /dev/null +++ b/activemodel/test/cases/callbacks_test.rb @@ -0,0 +1,70 @@ +require "cases/helper" + +class CallbacksTest < ActiveModel::TestCase + + class CallbackValidator + def around_create(model) + model.callbacks << :before_around_create + yield + model.callbacks << :after_around_create + end + end + + class ModelCallbacks + attr_reader :callbacks + extend ActiveModel::Callbacks + + define_model_callbacks :create + define_model_callbacks :initialize, :only => :after + + before_create :before_create + around_create CallbackValidator.new + + after_create do |model| + model.callbacks << :after_create + end + + after_create "@callbacks << :final_callback" + + def initialize(valid=true) + @callbacks, @valid = [], valid + end + + def before_create + @callbacks << :before_create + end + + def create + _run_create_callbacks do + @callbacks << :create + @valid + end + end + end + + test "complete callback chain" do + model = ModelCallbacks.new + model.create + assert_equal model.callbacks, [ :before_create, :before_around_create, :create, + :after_around_create, :after_create, :final_callback] + end + + test "after callbacks are always appended" do + model = ModelCallbacks.new + model.create + assert_equal model.callbacks.last, :final_callback + end + + test "after callbacks are not executed if the block returns false" do + model = ModelCallbacks.new(false) + model.create + assert_equal model.callbacks, [ :before_create, :before_around_create, + :create, :after_around_create] + end + + test "only selects which types of callbacks should be created" do + assert !ModelCallbacks.respond_to?(:before_initialize) + assert !ModelCallbacks.respond_to?(:around_initialize) + assert ModelCallbacks.respond_to?(:after_initialize) + end +end |