public
Description: Conditional checks on Rails filters
Clone URL: git://github.com/thoughtbot/when.git
added support for String, Proc, Method

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@341 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
dcroak (author)
Thu Feb 14 16:35:54 -0800 2008
commit  cfdaefa7cb69b8e82bf1989591980b598d24f858
tree    774e5c81c3d2d67fa1f05308d43ba826a3a52e8c
parent  6ad7a04e01b0f58280faa5236a10ade24fece6a9
...
17
18
19
 
 
 
 
20
21
22
 
23
24
25
...
17
18
19
20
21
22
23
24
25
 
26
27
28
29
0
@@ -17,9 +17,13 @@ module When
0
                       (! options[:unless].nil? && evaluate_condition(options[:unless], record))
0
                     if callback.class == Symbol
0
                       record.send callback
0
+ elsif callback.class == String
0
+ eval(callback, binding)
0
+ elsif callback.class == Proc || callback.class == Method
0
+ callback.call(record)
0
                     else
0
                       raise ActiveRecord::ActiveRecordError,
0
- 'When only supports Symbol callbacks, refactor to use a Symbol or remove When'
0
+ "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."
0
                     end
0
                   end
0
                 end
...
50
51
52
53
54
 
 
55
56
57
...
59
60
61
62
63
 
 
64
65
66
...
102
103
104
105
106
 
 
107
108
109
...
111
112
113
114
115
 
 
116
117
118
...
158
159
160
161
162
 
 
163
164
165
...
168
169
170
171
172
 
 
173
174
175
...
50
51
52
 
 
53
54
55
56
57
...
59
60
61
 
 
62
63
64
65
66
...
102
103
104
 
 
105
106
107
108
109
...
111
112
113
 
 
114
115
116
117
118
...
158
159
160
 
 
161
162
163
164
165
...
168
169
170
 
 
171
172
173
174
175
0
@@ -50,8 +50,8 @@ class CallbacksTest < Test::Unit::TestCase
0
         assert_equal 'new name', company.name
0
       end
0
 
0
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
0
- Company.send callback.to_sym, "'puts #{callback}'", :if => condition
0
+ 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
0
+ Company.send callback.to_sym, [], :if => condition
0
 
0
         company = Company.new :name => 'thoughtbot', :flag => true
0
         assert_raises(ActiveRecord::ActiveRecordError) do
0
@@ -59,8 +59,8 @@ class CallbacksTest < Test::Unit::TestCase
0
         end
0
       end
0
 
0
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
0
- Company.send callback.to_sym, "'puts #{callback}'", :unless => condition
0
+ 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
0
+ Company.send callback.to_sym, [], :unless => condition
0
 
0
         company = Company.new :name => 'thoughtbot', :flag => false
0
         assert_raises(ActiveRecord::ActiveRecordError) do
0
@@ -102,8 +102,8 @@ class CallbacksTest < Test::Unit::TestCase
0
         assert_equal 'new name', company.name
0
       end
0
 
0
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
0
- Company.send callback.to_sym, "'puts #{callback}'", :if => condition
0
+ 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
0
+ Company.send callback.to_sym, [], :if => condition
0
 
0
         company = Company.create :name => 'thoughtbot', :flag => true
0
         assert_raises(ActiveRecord::ActiveRecordError) do
0
@@ -111,8 +111,8 @@ class CallbacksTest < Test::Unit::TestCase
0
         end
0
       end
0
 
0
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
0
- Company.send callback.to_sym, "'puts #{callback}'", :unless => condition
0
+ 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
0
+ Company.send callback.to_sym, [], :unless => condition
0
 
0
         company = Company.create :name => 'thoughtbot', :flag => false
0
         assert_raises(ActiveRecord::ActiveRecordError) do
0
@@ -158,8 +158,8 @@ class CallbacksTest < Test::Unit::TestCase
0
         assert company.flag
0
       end
0
 
0
- define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
0
- Company.send callback.to_sym, "'puts #{callback}'", :if => condition
0
+ 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
0
+ Company.send callback.to_sym, [], :if => condition
0
 
0
         company = Company.new :name => 'thoughtbot', :flag => true
0
         assert company.save
0
@@ -168,8 +168,8 @@ class CallbacksTest < Test::Unit::TestCase
0
         end
0
       end
0
 
0
- define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
0
- Company.send callback.to_sym, "'puts #{callback}'", :unless => condition
0
+ 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
0
+ Company.send callback.to_sym, [], :unless => condition
0
 
0
         company = Company.new :name => 'thoughtbot', :flag => false
0
         assert company.save
...
45
46
47
48
49
 
 
50
51
52
...
54
55
56
57
58
 
 
59
60
61
...
97
98
99
100
101
 
 
102
103
104
...
106
107
108
109
110
 
 
111
112
113
...
45
46
47
 
 
48
49
50
51
52
...
54
55
56
 
 
57
58
59
60
61
...
97
98
99
 
 
100
101
102
103
104
...
106
107
108
 
 
109
110
111
112
113
0
@@ -45,8 +45,8 @@ class ValidationsTest < Test::Unit::TestCase
0
         assert_equal 'new name', company.name
0
       end
0
 
0
- define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
0
- Company.send validation.to_sym, "'puts #{validation}'", :if => condition
0
+ 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
0
+ Company.send validation.to_sym, [], :if => condition
0
 
0
         company = Company.new :name => 'thoughtbot', :flag => true
0
         assert_raises(ActiveRecord::ActiveRecordError) do
0
@@ -54,8 +54,8 @@ class ValidationsTest < Test::Unit::TestCase
0
         end
0
       end
0
 
0
- define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
0
- Company.send validation.to_sym, "'puts #{validation}'", :unless => condition
0
+ 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
0
+ Company.send validation.to_sym, [], :unless => condition
0
 
0
         company = Company.new :name => 'thoughtbot', :flag => false
0
         assert_raises(ActiveRecord::ActiveRecordError) do
0
@@ -97,8 +97,8 @@ class ValidationsTest < Test::Unit::TestCase
0
         assert_equal 'new name', company.name
0
       end
0
 
0
- define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
0
- Company.send validation.to_sym, "'puts #{validation}'", :if => condition
0
+ 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
0
+ Company.send validation.to_sym, [], :if => condition
0
 
0
         company = Company.create :name => 'thoughtbot', :flag => true
0
         assert_raises(ActiveRecord::ActiveRecordError) do
0
@@ -106,8 +106,8 @@ class ValidationsTest < Test::Unit::TestCase
0
         end
0
       end
0
 
0
- define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
0
- Company.send validation.to_sym, "'puts #{validation}'", :unless => condition
0
+ 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
0
+ Company.send validation.to_sym, [], :unless => condition
0
 
0
         company = Company.create :name => 'thoughtbot', :flag => false
0
         assert_raises(ActiveRecord::ActiveRecordError) do

Comments

    No one has commented yet.