public
Description: Conditional checks on Rails filters
Clone URL: git://github.com/thoughtbot/when.git
fixed callbacks String condition

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@349 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
jcarroll (author)
Thu Feb 14 21:36:09 -0800 2008
commit  161cc080e7141aeddecfcdb75bb3c3d1ecf3d27e
tree    a2b03d27cb31946dc3bc631349b64cf0f21e82db
parent  d8487bb35e0f65b57174dd237e439c1ee6c4c937
...
21
22
23
24
 
25
26
27
...
21
22
23
 
24
25
26
27
0
@@ -21,7 +21,7 @@ module When
0
                     if callback.class == Symbol
0
                       record.send callback
0
                     elsif callback.class == String
0
- eval(callback, binding)
0
+ eval(callback, record.send(:binding))
0
                     elsif callback.class == Proc || callback.class == Method
0
                       callback.call(record)
0
                     else
...
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
...
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
0
@@ -182,120 +182,108 @@ class CallbacksTest < Test::Unit::TestCase
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
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ Company.send callback.to_sym, 'change_name', :if => condition
0
 
0
- Company.bio = 'thoughtbot'
0
- company = Company.new :flag => true
0
+ company = Company.new :name => 'thoughtbot', :flag => true
0
         assert company.save
0
- assert_equal 'new bio', Company.bio
0
+ assert_equal 'new name', company.name
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
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
+ Company.send callback.to_sym, 'change_name', :if => condition
0
 
0
- Company.bio = 'thoughtbot'
0
- company = Company.new :flag => false
0
+ company = Company.new :name => 'thoughtbot', :flag => false
0
         assert company.save
0
- assert_equal 'thoughtbot', Company.bio
0
+ assert_equal 'thoughtbot', company.name
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
+ define_method "test_#{callback}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
+ Company.send callback.to_sym, 'change_name', :unless => condition
0
 
0
- Company.bio = 'thoughtbot'
0
- company = Company.new :flag => true
0
+ company = Company.new :name => 'thoughtbot', :flag => true
0
         assert company.save
0
- assert_equal 'thoughtbot', Company.bio
0
+ assert_equal 'thoughtbot', company.name
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
+ define_method "test_#{callback}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ Company.send callback.to_sym, 'change_name', :unless => condition
0
 
0
- Company.bio = 'thoughtbot'
0
- company = Company.new :flag => false
0
+ company = Company.new :name => 'thoughtbot', :flag => false
0
         assert company.save
0
- assert_equal 'new bio', Company.bio
0
+ assert_equal 'new name', company.name
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
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ Company.send callback.to_sym, 'change_name', :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
+ assert_equal 'new name', company.name
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
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
+ Company.send callback.to_sym, 'change_name', :if => condition
0
 
0
- Company.bio = 'thoughtbot'
0
- company = Company.create :flag => false
0
+ company = Company.create :name => 'thoughtbot', :flag => false
0
         assert company.save
0
- assert_equal 'thoughtbot', Company.bio
0
+ assert_equal 'thoughtbot', company.name
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
+ define_method "test_#{callback}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
+ Company.send callback.to_sym, 'change_name', :unless => condition
0
 
0
- Company.bio = 'thoughtbot'
0
- company = Company.create :flag => true
0
+ company = Company.create :name => 'thoughtbot', :flag => true
0
         assert company.save
0
- assert_equal 'thoughtbot', Company.bio
0
+ assert_equal 'thoughtbot', company.name
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
+ define_method "test_#{callback}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ Company.send callback.to_sym, 'change_name', :unless => condition
0
 
0
- Company.bio = 'thoughtbot'
0
- company = Company.create :flag => false
0
+ company = Company.create :name => 'thoughtbot', :flag => false
0
         assert company.save
0
- assert_equal 'new bio', Company.bio
0
+ assert_equal 'new name', company.name
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
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_true_should_toggle_flag" do
0
+ Company.send callback.to_sym, 'toggle_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
+ 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
+ define_method "test_#{callback}_with_string_callback_with_if_condition_#{condition.class}_which_returns_false_should_not_toggle_flag" do
0
+ Company.send callback.to_sym, 'toggle_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
+ 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
+ define_method "test_#{callback}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_true_should_not_toggle_flag" do
0
+ Company.send callback.to_sym, 'toggle_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
+ 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
+ define_method "test_#{callback}_with_string_callback_unless_condition_#{condition.class}_which_returns_false_should_toggle_flag" do
0
+ Company.send callback.to_sym, 'toggle_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
+ assert company.flag
0
       end
0
     end
0
   end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
1
2
 
 
 
 
 
 
 
 
 
 
3
4
5
0
@@ -1,15 +1,5 @@
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
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
0
@@ -1,25 +1,5 @@
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
...
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
...
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
0
@@ -119,78 +119,70 @@ class ValidationsTest < Test::Unit::TestCase
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
+ define_method "test_#{validation}_with_string_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ Company.send validation.to_sym, 'change_name', :if => condition
0
         
0
- Company.bio = 'thoughtbot'
0
- company = Company.new :flag => true
0
+ company = Company.new :name => 'thoughtbot', :flag => true
0
         assert company.save
0
- assert_equal 'new bio', Company.bio
0
+ assert_equal 'new name', company.name
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
+ define_method "test_#{validation}_with_string_callback_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
- Company.bio = 'thoughtbot'
0
- company = Company.new :flag => false
0
+ company = Company.new :name => 'thoughtbot', :flag => false
0
         assert company.save
0
- assert_equal 'thoughtbot', Company.bio
0
+ assert_equal 'thoughtbot', company.name
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
+ define_method "test_#{validation}_with_string_callback_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
- Company.bio = 'thoughtbot'
0
- company = Company.new :flag => true
0
+ company = Company.new :name => 'thoughtbot', :flag => true
0
         assert company.save
0
- assert_equal 'thoughtbot', Company.bio
0
+ assert_equal 'thoughtbot', company.name
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
+ define_method "test_#{validation}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ Company.send validation.to_sym, 'change_name', :unless => condition
0
         
0
- Company.bio = 'thoughtbot'
0
- company = Company.new :flag => false
0
+ company = Company.new :name => 'thoughtbot', :flag => false
0
         assert company.save
0
- assert_equal 'new bio', Company.bio
0
+ assert_equal 'new name', company.name
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
+ define_method "test_#{validation}_with_string_callback_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ Company.send validation.to_sym, 'change_name', :if => condition
0
         
0
- Company.bio = 'thoughtbot'
0
- company = Company.create :flag => true
0
+ company = Company.create :name => 'thoughtbot', :flag => true
0
         assert company.save
0
- assert_equal 'new bio', Company.bio
0
+ assert_equal 'new name', company.name
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
+ define_method "test_#{validation}_with_string_callback_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
- Company.bio = 'thoughtbot'
0
- company = Company.create :flag => false
0
+ company = Company.create :name => 'thoughtbot', :flag => false
0
         assert company.save
0
- assert_equal 'thoughtbot', Company.bio
0
+ assert_equal 'thoughtbot', company.name
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
+ define_method "test_#{validation}_with_string_callback_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
- Company.bio = 'thoughtbot'
0
- company = Company.create :flag => true
0
+ company = Company.create :name => 'thoughtbot', :flag => true
0
         assert company.save
0
- assert_equal 'thoughtbot', Company.bio
0
+ assert_equal 'thoughtbot', company.name
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
+ define_method "test_#{validation}_with_string_callback_with_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ Company.send validation.to_sym, 'change_name', :unless => condition
0
         
0
- Company.bio = 'thoughtbot'
0
- company = Company.create :flag => false
0
+ company = Company.create :name => 'thoughtbot', :flag => false
0
         assert company.save
0
- assert_equal 'new bio', Company.bio
0
+ assert_equal 'new name', company.name
0
       end
0
     end
0
   end

Comments

    No one has commented yet.