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

git-svn-id: https://svn.thoughtbot.com/plugins/when/trunk@339 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
jcarroll (author)
Thu Feb 14 15:06:54 -0800 2008
commit  3f17be92950facd2bd11a2fa53b32e3b7f2deb8a
tree    015e738aaeb42c1d578c367ab6437bff67f64c9e
parent  751f3da9647fc094c93c6e38f9ad46879aeb2b76
...
10
11
12
13
 
14
15
16
...
10
11
12
 
13
14
15
16
0
@@ -10,7 +10,7 @@ module When
0
             def #{filter}_with_conditions(*filters)
0
               options = filters.extract_options!
0
               filters.each do |filter|
0
- #{filter}_without_conditions do |controller|
0
+ #{filter}_without_conditions(options) do |controller|
0
                   unless (! options[:if].nil? && ! ActiveRecord::Base.evaluate_condition(options[:if], controller)) ||
0
                       (! options[:unless].nil? && ActiveRecord::Base.evaluate_condition(options[:unless], controller))
0
                     if filter.class == Symbol
...
16
17
18
 
 
19
20
21
...
65
66
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
69
70
...
16
17
18
19
20
21
22
23
...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
0
@@ -16,6 +16,8 @@ class FiltersTest < ActionController::TestCase
0
     @response = ActionController::TestResponse.new
0
     ActionController::Routing::Routes.draw do |map|
0
       map.connect 'companies', :controller => 'companies'
0
+ map.connect 'companies/show', :controller => 'companies',
0
+ :action => 'show'
0
     end
0
   end
0
 
0
@@ -65,6 +67,110 @@ class FiltersTest < ActionController::TestCase
0
         assert_equal 'new name', @controller.name
0
       end
0
 
0
+ define_method "test_#{filter}_with_only_option_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :change_name, :only => :show, :if => condition
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :show
0
+ assert_equal 'new name', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_only_option_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :change_name, :only => :show, :if => condition
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_only_option_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :change_name, :only => :show, :unless => condition
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_only_option_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :change_name, :only => :show, :unless => condition
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :show
0
+ assert_equal 'new name', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_with_if_condition_#{condition.class}_which_returns_true_should_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :change_name, :except => :show, :if => condition
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :index
0
+ assert_equal 'new name', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_if_condition_#{condition.class}_which_returns_false_should_not_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :change_name, :except => :show, :if => condition
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_unless_condition_#{condition.class}_which_returns_true_should_not_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :change_name, :except => :show, :unless => condition
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = true
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :index
0
+ assert_equal 'thoughtbot', @controller.name
0
+ end
0
+
0
+ define_method "test_#{filter}_with_except_option_unless_condition_#{condition.class}_which_returns_false_should_change_company_name" do
0
+ CompaniesController.send filter.to_sym, :change_name, :except => :show, :unless => condition
0
+
0
+ @controller.name = 'thoughtbot'
0
+ @controller.flag = false
0
+
0
+ get :show
0
+ assert_equal 'thoughtbot', @controller.name
0
+
0
+ get :index
0
+ assert_equal 'new name', @controller.name
0
+ end
0
+
0
       define_method "test_#{filter}_with_if_condition_#{condition.class}_which_returns_true_should_raise_an_exception_if_its_callback_is_not_a_symbol" do
0
         CompaniesController.send filter.to_sym, CompaniesController, :if => condition
0
 
...
15
16
17
 
 
 
 
18
...
15
16
17
18
19
20
21
22
0
@@ -15,4 +15,8 @@ class CompaniesController < ActionController::Base
0
     render :nothing => true
0
   end
0
 
0
+ def show
0
+ render :nothing => true
0
+ end
0
+
0
 end

Comments

    No one has commented yet.