This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

update_callbacks

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@310 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
1 parent 25414c5 commit bbbd4475c6f16fe3fd272299e70662809d4c2533 dcroak committed Feb 12, 2008
Showing with 44 additions and 3 deletions.
  1. +44 −3 test/callbacks_test.rb
View
@@ -11,11 +11,12 @@ def setup
'after_validation_on_create', 'after_validation',
'before_save', 'after_save' ]
+ update_callbacks = ['before_update', 'after_update',
+ 'before_validation_on_update', 'after_validation_on_update']
+
destroy_callbacks = ['before_destroy', 'after_destroy']
- unique_callbacks = ['after_find', 'after_initialize',
- 'before_update', 'after_update',
- 'before_validation_on_update', 'after_validation_on_update']
+ unique_callbacks = ['after_find', 'after_initialize']
conditions = ['lambda {|company| company.callback_flag == true}']
@@ -62,6 +63,46 @@ def test_#{callback}_with_unless_condition_which_returns_false_should_change_com
END
end
+ update_callbacks.each do |callback|
+ src << <<-END;
+ def test_#{callback}_with_if_condition_which_returns_true_should_change_company_name
+ Company.class_eval do
+ #{callback} :change_name, :if => #{condition}
+ end
+ company = Company.create :name => 'thoughtbot', :callback_flag => true
+ assert company.save
+ assert_equal 'new name', company.name
+ end
+
+ def test_#{callback}_with_if_condition_which_returns_false_should_not_change_company_name
+ Company.class_eval do
+ #{callback} :change_name, :if => #{condition}
+ end
+ company = Company.create :name => 'thoughtbot', :callback_flag => false
+ assert company.save
+ assert_equal 'thoughtbot', company.name
+ end
+
+ def test_#{callback}_with_unless_condition_which_returns_true_should_not_change_company_name
+ Company.class_eval do
+ #{callback} :change_name, :unless => #{condition}
+ end
+ company = Company.create :name => 'thoughtbot', :callback_flag => true
+ assert company.save
+ assert_equal 'thoughtbot', company.name
+ end
+
+ def test_#{callback}_with_unless_condition_which_returns_false_should_change_company_name
+ Company.class_eval do
+ #{callback} :change_name, :unless => #{condition}
+ end
+ company = Company.create :name => 'thoughtbot', :callback_flag => false
+ assert company.save
+ assert_equal 'new name', company.name
+ end
+ END
+ end
+
destroy_callbacks.each do |callback|
src << <<-END;
def test_#{callback}_with_if_condition_which_returns_true_should_change_company_name

0 comments on commit bbbd447

Please sign in to comment.