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

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@345 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
jcarroll (author)
Thu Feb 14 17:39:03 -0800 2008
commit  432aa3e537c75fa52a742877a6623b92383ebade
tree    811963e5d9a159857f617c12789a481e5eb5e0c2
parent  21620772650ee3d94718db5faefd682be99f70d5
...
11
12
13
14
15
16
 
 
 
17
18
19
...
22
23
24
25
26
 
 
27
28
29
...
11
12
13
 
 
 
14
15
16
17
18
19
...
22
23
24
 
 
25
26
27
28
29
0
@@ -11,9 +11,9 @@ module When
0
           src = <<-END
0
             def #{callback}_with_conditions(*callbacks, &block)
0
               options = callbacks.extract_options!
0
- if block_given?
0
- callbacks << block
0
- end
0
+# if block_given?
0
+# callbacks << block
0
+# end
0
               callbacks.each do |callback|
0
                 #{callback}_without_conditions do |record|
0
                   unless (! options[:if].nil? && ! evaluate_condition(options[:if], record)) ||
0
@@ -22,8 +22,8 @@ module When
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
+# elsif callback.class == Proc || callback.class == Method
0
+# callback.call(record)
0
                     else
0
                       raise ActiveRecord::ActiveRecordError,
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."
...
9
10
11
12
13
14
 
 
 
15
16
17
...
20
21
22
23
24
 
 
25
26
27
...
9
10
11
 
 
 
12
13
14
15
16
17
...
20
21
22
 
 
23
24
25
26
27
0
@@ -9,9 +9,9 @@ module When
0
           src = <<-END
0
             def #{filter}_with_conditions(*filters, &block)
0
               options = filters.extract_options!
0
- if block_given?
0
- options << block
0
- end
0
+# if block_given?
0
+# options << block
0
+# end
0
               filters.each do |filter|
0
                 #{filter}_without_conditions(options) do |controller|
0
                   unless (! options[:if].nil? && ! ActiveRecord::Base.evaluate_condition(options[:if], controller)) ||
0
@@ -20,8 +20,8 @@ module When
0
                       controller.send filter
0
                     elsif filter.class == String
0
                       eval(filter, binding)
0
- elsif filter.class == Proc || filter.class == Method
0
- filter.call(controller)
0
+# elsif filter.class == Proc || filter.class == Method
0
+# filter.call(controller)
0
                     else
0
                       raise ActionController::ActionControllerError,
0
                         "Filters 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."
...
180
181
182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
184
185
...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
0
@@ -180,6 +180,126 @@ class CallbacksTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
+ conditions.each do |condition|
0
+ basic_callbacks.each do |callback|
0
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_bio" do
0
+ Company.send callback.to_sym, 'self.bio = "new bio"', :if => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.new :flag => true
0
+ assert company.save
0
+ assert_equal 'new bio', Company.bio
0
+ end
0
+
0
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_bio" do
0
+ Company.send callback.to_sym, 'self.bio = "new bio"', :if => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.new :flag => false
0
+ assert company.save
0
+ assert_equal 'thoughtbot', Company.bio
0
+ end
0
+
0
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_bio" do
0
+ Company.send callback.to_sym, 'self.bio = "new bio"', :unless => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.new :flag => true
0
+ assert company.save
0
+ assert_equal 'thoughtbot', Company.bio
0
+ end
0
+
0
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_bio" do
0
+ Company.send callback.to_sym, 'self.bio = "new bio"', :unless => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.new :flag => false
0
+ assert company.save
0
+ assert_equal 'new bio', Company.bio
0
+ end
0
+ end
0
+
0
+ update_callbacks.each do |callback|
0
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_bio" do
0
+ Company.send callback.to_sym, 'self.bio = "new bio"', :if => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.create :flag => true
0
+ assert company.save
0
+ assert_equal 'new bio', Company.bio
0
+ end
0
+
0
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_bio" do
0
+ Company.send callback.to_sym, 'self.bio = "new bio"', :if => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.create :flag => false
0
+ assert company.save
0
+ assert_equal 'thoughtbot', Company.bio
0
+ end
0
+
0
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_bio" do
0
+ Company.send callback.to_sym, 'self.bio = "new bio"', :unless => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.create :flag => true
0
+ assert company.save
0
+ assert_equal 'thoughtbot', Company.bio
0
+ end
0
+
0
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_bio" do
0
+ Company.send callback.to_sym, 'self.bio = "new bio"', :unless => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.create :flag => false
0
+ assert company.save
0
+ assert_equal 'new bio', Company.bio
0
+ end
0
+ end
0
+
0
+ destroy_callbacks.each do |callback|
0
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_true_should_toggle_class_flag" do
0
+ Company.send callback.to_sym, 'self.flag = ! flag; true;', :if => condition
0
+
0
+ Company.flag = true
0
+ company = Company.new :flag => true
0
+ assert company.save
0
+ assert company.destroy
0
+ assert ! Company.flag
0
+ end
0
+
0
+ define_method "test_#{callback}_with_if_condition_#{condition.class}_which_returns_false_should_not_toggle_class_flag" do
0
+ Company.send callback.to_sym, 'self.flag = ! flag', :if => condition
0
+
0
+ Company.flag = false
0
+ company = Company.new :flag => false
0
+ assert company.save
0
+ assert company.destroy
0
+ assert ! Company.flag
0
+ end
0
+
0
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_true_should_not_toggle_class_flag" do
0
+ Company.send callback.to_sym, 'self.flag = ! flag', :unless => condition
0
+
0
+ Company.flag = true
0
+ company = Company.new :flag => true
0
+ assert company.save
0
+ assert company.destroy
0
+ assert Company.flag
0
+ end
0
+
0
+ define_method "test_#{callback}_with_unless_condition_#{condition.class}_which_returns_false_should_toggle_class_flag" do
0
+ Company.send callback.to_sym, 'self.flag = ! flag', :unless => condition
0
+
0
+ Company.flag = false
0
+ company = Company.new :flag => false
0
+ assert company.save
0
+ assert company.destroy
0
+ assert Company.flag
0
+ end
0
+ end
0
+ end
0
+
0
   def teardown
0
     Object.class_eval do
0
       remove_const Company.to_s if const_defined? Company.to_s
...
195
196
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
199
200
...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
0
@@ -195,6 +195,154 @@ class FiltersTest < ActionController::TestCase
0
     end
0
   end
0
 
0
+ conditions.each do |condition|
0
+ basic_filters.each do |filter|
0
+ define_method "test_#{filter}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :if => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :index
0
+ assert_equal 'new bio', CompaniesController.bio
0
+ end
0
+
0
+ define_method "test_#{filter}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :if => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+ end
0
+
0
+ define_method "test_#{filter}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :unless => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+ end
0
+
0
+ define_method "test_#{filter}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :unless => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :index
0
+ assert_equal 'new bio', CompaniesController.bio
0
+ end
0
+
0
+ define_method "test_#{filter}_with_only_option_with_if_condition_#{condition.class}_which_returns_true_should_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :only => :show, :if => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+
0
+ get :show
0
+ assert_equal 'new bio', CompaniesController.bio
0
+ end
0
+
0
+ define_method "test_#{filter}_with_only_option_if_condition_#{condition.class}_which_returns_false_should_not_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :only => :show, :if => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+ end
0
+
0
+ define_method "test_#{filter}_with_only_option_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :only => :show, :unless => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+ end
0
+
0
+ define_method "test_#{filter}_with_only_option_unless_condition_#{condition.class}_which_returns_false_should_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :only => :show, :unless => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+
0
+ get :show
0
+ assert_equal 'new bio', CompaniesController.bio
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_with_if_condition_#{condition.class}_which_returns_true_should_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :except => :show, :if => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+
0
+ get :index
0
+ assert_equal 'new bio', CompaniesController.bio
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_if_condition_#{condition.class}_which_returns_false_should_not_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :except => :show, :if => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :except => :show, :unless => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_unless_condition_#{condition.class}_which_returns_false_should_change_company_bio" do
0
+ CompaniesController.send filter.to_sym, 'self.bio = "new bio"', :except => :show, :unless => condition
0
+
0
+ CompaniesController.bio = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', CompaniesController.bio
0
+
0
+ get :index
0
+ assert_equal 'new bio', CompaniesController.bio
0
+ end
0
+ end
0
+ end
0
+
0
   def teardown
0
     Object.class_eval do
0
       remove_const CompaniesController.to_s if const_defined? CompaniesController.to_s
...
1
2
 
 
 
 
 
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -1,5 +1,15 @@
0
 class CompaniesController < ActionController::Base
0
 
0
+ @@bio = nil
0
+ class << self
0
+ def bio
0
+ @@bio
0
+ end
0
+ def bio=(bio)
0
+ @@bio = bio
0
+ end
0
+ end
0
+
0
   attr_accessor :flag,
0
     :name
0
 
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
0
@@ -1,5 +1,25 @@
0
 class Company < ActiveRecord::Base
0
 
0
+ @@bio = nil
0
+ class << self
0
+ def bio
0
+ @@bio
0
+ end
0
+ def bio=(bio)
0
+ @@bio = bio
0
+ end
0
+ end
0
+
0
+ @@flag = nil
0
+ class << self
0
+ def flag
0
+ @@flag
0
+ end
0
+ def flag=(flag)
0
+ @@flag = flag
0
+ end
0
+ end
0
+
0
   attr_accessor :flag
0
 
0
   def change_name
...
72
73
74
75
 
76
77
78
...
80
81
82
83
 
84
85
86
...
117
118
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
121
122
...
72
73
74
 
75
76
77
78
...
80
81
82
 
83
84
85
86
...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
0
@@ -72,7 +72,7 @@ class ValidationsTest < Test::Unit::TestCase
0
         assert company.save
0
         assert_equal 'new name', company.name
0
       end
0
-
0
+
0
       define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
         Company.send validation.to_sym, :change_name, :if => condition
0
         
0
@@ -80,7 +80,7 @@ class ValidationsTest < Test::Unit::TestCase
0
         assert company.save
0
         assert_equal 'thoughtbot', company.name
0
       end
0
-
0
+
0
       define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
         Company.send validation.to_sym, :change_name, :unless => condition
0
         
0
@@ -117,6 +117,84 @@ class ValidationsTest < Test::Unit::TestCase
0
     end
0
   end
0
   
0
+ conditions.each do |condition|
0
+ basic_validations.each do |validation|
0
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_bio" do
0
+ Company.send validation.to_sym, 'self.bio = "new bio"', :if => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.new :flag => true
0
+ assert company.save
0
+ assert_equal 'new bio', Company.bio
0
+ end
0
+
0
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_bio" do
0
+ Company.send validation.to_sym, 'self.bio = "new bio"', :if => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.new :flag => false
0
+ assert company.save
0
+ assert_equal 'thoughtbot', Company.bio
0
+ end
0
+
0
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_bio" do
0
+ Company.send validation.to_sym, 'self.bio = "new bio"', :unless => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.new :flag => true
0
+ assert company.save
0
+ assert_equal 'thoughtbot', Company.bio
0
+ end
0
+
0
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_bio" do
0
+ Company.send validation.to_sym, 'self.bio = "new bio"', :unless => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.new :flag => false
0
+ assert company.save
0
+ assert_equal 'new bio', Company.bio
0
+ end
0
+ end
0
+
0
+ update_validations.each do |validation|
0
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_true_should_change_company_bio" do
0
+ Company.send validation.to_sym, 'self.bio = "new bio"', :if => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.create :flag => true
0
+ assert company.save
0
+ assert_equal 'new bio', Company.bio
0
+ end
0
+
0
+ define_method "test_#{validation}_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_bio" do
0
+ Company.send validation.to_sym, 'self.bio = "new bio"', :if => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.create :flag => false
0
+ assert company.save
0
+ assert_equal 'thoughtbot', Company.bio
0
+ end
0
+
0
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_bio" do
0
+ Company.send validation.to_sym, 'self.bio = "new bio"', :unless => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.create :flag => true
0
+ assert company.save
0
+ assert_equal 'thoughtbot', Company.bio
0
+ end
0
+
0
+ define_method "test_#{validation}_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_bio" do
0
+ Company.send validation.to_sym, 'self.bio = "new bio"', :unless => condition
0
+
0
+ Company.bio = 'thoughtbot'
0
+ company = Company.create :flag => false
0
+ assert company.save
0
+ assert_equal 'new bio', Company.bio
0
+ end
0
+ end
0
+ end
0
+
0
   def teardown
0
     Object.class_eval do
0
       remove_const Company.to_s if const_defined? Company.to_s

Comments

    No one has commented yet.