Skip to content
Browse files

added support for String, Proc, Method

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@341 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
1 parent 6ad7a04 commit cfdaefa7cb69b8e82bf1989591980b598d24f858 dcroak committed Feb 15, 2008
Showing with 25 additions and 21 deletions.
  1. +5 −1 lib/callbacks.rb
  2. +12 −12 test/callbacks_test.rb
  3. +8 −8 test/validations_test.rb
View
6 lib/callbacks.rb
@@ -17,9 +17,13 @@ def #{callback}_with_conditions(*callbacks)
(! options[:unless].nil? && evaluate_condition(options[:unless], record))
if callback.class == Symbol
record.send callback
+ elsif callback.class == String
+ eval(callback, binding)
+ elsif callback.class == Proc || callback.class == Method
+ callback.call(record)
else
raise ActiveRecord::ActiveRecordError,
- 'When only supports Symbol callbacks, refactor to use a Symbol or remove When'
+ "Callbacks must be a symbol denoting the method to call, a string to be evaluated, a block to be invoked, or an object responding to the callback method."
end
end
end
View
24 test/callbacks_test.rb
@@ -50,17 +50,17 @@ def setup
assert_equal 'new name', company.name
end
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
- Company.send callback.to_sym, "'puts #{callback}'", :if => condition
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_supported_type" do
+ Company.send callback.to_sym, [], :if => condition
company = Company.new :name => 'thoughtbot', :flag => true
assert_raises(ActiveRecord::ActiveRecordError) do
assert company.save
end
end
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
- Company.send callback.to_sym, "'puts #{callback}'", :unless => condition
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_supported_type" do
+ Company.send callback.to_sym, [], :unless => condition
company = Company.new :name => 'thoughtbot', :flag => false
assert_raises(ActiveRecord::ActiveRecordError) do
@@ -102,17 +102,17 @@ def setup
assert_equal 'new name', company.name
end
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
- Company.send callback.to_sym, "'puts #{callback}'", :if => condition
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_supported_type" do
+ Company.send callback.to_sym, [], :if => condition
company = Company.create :name => 'thoughtbot', :flag => true
assert_raises(ActiveRecord::ActiveRecordError) do
assert company.save
end
end
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
- Company.send callback.to_sym, "'puts #{callback}'", :unless => condition
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_supported_type" do
+ Company.send callback.to_sym, [], :unless => condition
company = Company.create :name => 'thoughtbot', :flag => false
assert_raises(ActiveRecord::ActiveRecordError) do
@@ -158,8 +158,8 @@ def setup
assert company.flag
end
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
- Company.send callback.to_sym, "'puts #{callback}'", :if => condition
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_supported_type" do
+ Company.send callback.to_sym, [], :if => condition
company = Company.new :name => 'thoughtbot', :flag => true
assert company.save
@@ -168,8 +168,8 @@ def setup
end
end
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
- Company.send callback.to_sym, "'puts #{callback}'", :unless => condition
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_supported_type" do
+ Company.send callback.to_sym, [], :unless => condition
company = Company.new :name => 'thoughtbot', :flag => false
assert company.save
View
16 test/validations_test.rb
@@ -45,17 +45,17 @@ def setup
assert_equal 'new name', company.name
end
- define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
- Company.send validation.to_sym, "'puts #{validation}'", :if => condition
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_supported_type" do
+ Company.send validation.to_sym, [], :if => condition
company = Company.new :name => 'thoughtbot', :flag => true
assert_raises(ActiveRecord::ActiveRecordError) do
assert company.save
end
end
- define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
- Company.send validation.to_sym, "'puts #{validation}'", :unless => condition
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_supported_type" do
+ Company.send validation.to_sym, [], :unless => condition
company = Company.new :name => 'thoughtbot', :flag => false
assert_raises(ActiveRecord::ActiveRecordError) do
@@ -97,17 +97,17 @@ def setup
assert_equal 'new name', company.name
end
- define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
- Company.send validation.to_sym, "'puts #{validation}'", :if => condition
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_supported_type" do
+ Company.send validation.to_sym, [], :if => condition
company = Company.create :name => 'thoughtbot', :flag => true
assert_raises(ActiveRecord::ActiveRecordError) do
assert company.save
end
end
- define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
- Company.send validation.to_sym, "'puts #{validation}'", :unless => condition
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_supported_type" do
+ Company.send validation.to_sym, [], :unless => condition
company = Company.create :name => 'thoughtbot', :flag => false
assert_raises(ActiveRecord::ActiveRecordError) do

0 comments on commit cfdaefa

Please sign in to comment.
Something went wrong with that request. Please try again.