authorized_rails_scaffolds 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,7 @@ module AuthorizedRailsScaffolds
8
8
  @plural_var_name = options[:plural_var_name] || @var_name.pluralize # Pluralized non-namespaced variable name
9
9
  # Determine namespace prefix i.e awesome
10
10
  @namespace_prefix = options[:namespace_prefix] || options[:singular_table_name][0..-(@var_name.length + 2)]
11
+ @controller_prefix = options[:controller_prefix] || options[:class_name].split('::')[0..-2].join('::')
11
12
 
12
13
  # Determine Parent Prefix i.e. user_company
13
14
  parent_prefix = AuthorizedRailsScaffolds.parent_models.collect{ |x| x.underscore }.join('_')
@@ -24,6 +25,11 @@ module AuthorizedRailsScaffolds
24
25
  @single_path_prefix = "#{@route_prefix}#{var_name}"
25
26
  end
26
27
 
28
+ # Prefix for Controllers (i.e. Admin::)
29
+ def ns_controller_prefix
30
+ "#{@controller_prefix}::" unless @controller_prefix.blank?
31
+ end
32
+
27
33
  # Non-namespaced class name (i.e. FooBar)
28
34
  def local_class_name
29
35
  @local_class_name
@@ -1,3 +1,3 @@
1
1
  module AuthorizedRailsScaffolds
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -3,5 +3,5 @@
3
3
  #
4
4
  RSpec.configure do |config|
5
5
  config.include Devise::TestHelpers, :type => :controller
6
- config.extend DeviseCanCanControllerMacros, :type => :controller
6
+ config.include DeviseCanCanControllerMacros, :type => :controller
7
7
  end
@@ -1,25 +1,44 @@
1
1
  module DeviseCanCanControllerMacros
2
- def login_unauthorized_user
3
- before(:each) do
4
- @ability = Object.new
5
- @ability.extend(CanCan::Ability)
6
- @controller.stubs(:current_ability).returns(@ability)
7
2
 
8
- @request.env["devise.mapping"] = Devise.mappings[:user]
9
- @logged_in_user = FactoryGirl.create(:user)
10
- sign_in @logged_in_user
3
+ module ClassMethods
4
+ def grant_ability(action, subject)
5
+ before(:each) do
6
+ stub_ability.can action, subject
7
+ end
8
+ end
9
+
10
+ def login_unauthorized_user
11
+ before(:each) do
12
+ stub_ability
13
+
14
+ @request.env["devise.mapping"] = Devise.mappings[:user]
15
+ @logged_in_user = FactoryGirl.create(:user)
16
+ sign_in @logged_in_user
17
+ end
18
+ end
19
+
20
+ def login_user_with_ability(action, subject)
21
+ before(:each) do
22
+ stub_ability.can action, subject
23
+
24
+ @request.env["devise.mapping"] = Devise.mappings[:user]
25
+ @logged_in_user = FactoryGirl.create(:user)
26
+ sign_in @logged_in_user
27
+ end
11
28
  end
12
29
  end
13
- def login_user_with_ability(action, subject)
14
- before(:each) do
30
+
31
+ def self.included(controller_spec)
32
+ controller_spec.extend(ClassMethods)
33
+ end
34
+
35
+ def stub_ability
36
+ unless @ability
15
37
  @ability = Object.new
16
38
  @ability.extend(CanCan::Ability)
17
- @ability.can action, subject
18
39
  @controller.stubs(:current_ability).returns(@ability)
19
-
20
- @request.env["devise.mapping"] = Devise.mappings[:user]
21
- @logged_in_user = FactoryGirl.create(:user)
22
- sign_in @logged_in_user
23
40
  end
41
+ @ability
24
42
  end
43
+
25
44
  end
@@ -19,9 +19,9 @@ orm_instance = Rails::Generators::ActiveModel.new var_name
19
19
 
20
20
  -%>
21
21
  <% module_namespacing do -%>
22
- class <%= controller_class_name %>Controller < ApplicationController
22
+ class <%= controller_class_name %>Controller < <%= t_helper.ns_controller_prefix %>ApplicationController
23
23
  <%- AuthorizedRailsScaffolds.parent_models.each_with_index do |model, model_index| -%>
24
- load_resource :<%= model.underscore %><% if model_index > 0 %>, :through => :<%= AuthorizedRailsScaffolds.parent_models[model_index - 1].underscore %><% end %>
24
+ load_and_authorize_resource :<%= model.underscore %><% if model_index > 0 %>, :through => :<%= AuthorizedRailsScaffolds.parent_models[model_index - 1].underscore %><% end %>
25
25
  <%- end -%>
26
26
  load_and_authorize_resource :<%= var_name%><% if AuthorizedRailsScaffolds.parent_models.any? %>, :through => :<%= AuthorizedRailsScaffolds.parent_models.last.underscore %><% end %>
27
27
 
@@ -57,39 +57,45 @@ describe <%= controller_class_name %>Controller do
57
57
  <%- end -%>
58
58
  <% unless options[:singleton] -%>
59
59
  describe "GET index" do
60
- context 'without a user' do
61
- describe 'with valid request' do
62
- before(:each) do
63
- @<%= var_name %> = <%= t_helper.create_factory_model %>
64
- get :index, {<%= t_helper.index_action_params_prefix %>}
60
+ context do # Within default nesting
61
+ <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
62
+ grant_ability :read, <%= model.classify %>
63
+ <%- end -%>
64
+
65
+ context 'without a user' do
66
+ describe 'with valid request' do
67
+ before(:each) do
68
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
69
+ get :index, {<%= t_helper.index_action_params_prefix %>}
70
+ end
71
+ it { should redirect_to(new_user_session_path) }
72
+ it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
65
73
  end
66
- it { should redirect_to(new_user_session_path) }
67
- it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
68
74
  end
69
- end
70
- context 'as an unauthorized user' do
71
- login_unauthorized_user
72
- describe 'with valid request' do
73
- before(:each) do
74
- @<%= var_name %> = <%= t_helper.create_factory_model %>
75
- get :index, {<%= t_helper.index_action_params_prefix %>}
75
+ context 'as an unauthorized user' do
76
+ login_unauthorized_user
77
+ describe 'with valid request' do
78
+ before(:each) do
79
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
80
+ get :index, {<%= t_helper.index_action_params_prefix %>}
81
+ end
82
+ it { should redirect_to(root_url) }
83
+ it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
76
84
  end
77
- it { should redirect_to(root_url) }
78
- it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
79
85
  end
80
- end
81
- context 'as user with read ability' do
82
- login_user_with_ability :read, <%= local_class_name %>
83
- describe 'with valid request' do
84
- before(:each) do
85
- @<%= var_name %> = <%= t_helper.create_factory_model %>
86
- get :index, {<%= t_helper.index_action_params_prefix %>}
87
- end
88
- it { should respond_with(:success) }
89
- it { should render_template(:index) }
90
- it { should render_with_layout(:application) }
91
- it "assigns all <%= plural_var_name %> as @<%= plural_var_name %>" do
92
- assigns(:<%= plural_var_name %>).should eq([@<%= var_name %>])
86
+ context 'as user with read ability' do
87
+ login_user_with_ability :read, <%= local_class_name %>
88
+ describe 'with valid request' do
89
+ before(:each) do
90
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
91
+ get :index, {<%= t_helper.index_action_params_prefix %>}
92
+ end
93
+ it { should respond_with(:success) }
94
+ it { should render_template(:index) }
95
+ it { should render_with_layout(:application) }
96
+ it "assigns all <%= plural_var_name %> as @<%= plural_var_name %>" do
97
+ assigns(:<%= plural_var_name %>).should eq([@<%= var_name %>])
98
+ end
93
99
  end
94
100
  end
95
101
  end
@@ -97,279 +103,319 @@ describe <%= controller_class_name %>Controller do
97
103
 
98
104
  <% end -%>
99
105
  describe "GET show" do
100
- context 'without a user' do
101
- describe 'with valid request' do
102
- before(:each) do
103
- @<%= var_name %> = <%= t_helper.create_factory_model %>
104
- get :show, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
106
+ context do # Within default nesting
107
+ <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
108
+ grant_ability :read, <%= model.classify %>
109
+ <%- end -%>
110
+
111
+ context 'without a user' do
112
+ <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
113
+ grant_ability :read, <%= model.classify %>
114
+ <%- end -%>
115
+
116
+ describe 'with valid request' do
117
+ before(:each) do
118
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
119
+ get :show, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
120
+ end
121
+ it { should redirect_to(new_user_session_path) }
122
+ it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
105
123
  end
106
- it { should redirect_to(new_user_session_path) }
107
- it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
108
124
  end
109
- end
110
- context 'as an unauthorized user' do
111
- login_unauthorized_user
112
- describe 'with valid request' do
113
- before(:each) do
114
- @<%= var_name %> = <%= t_helper.create_factory_model %>
115
- get :show, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
125
+ context 'as an unauthorized user' do
126
+ login_unauthorized_user
127
+ describe 'with valid request' do
128
+ before(:each) do
129
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
130
+ get :show, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
131
+ end
132
+ it { should redirect_to(<%= t_helper.controller_index_route %>) }
133
+ it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
116
134
  end
117
- it { should redirect_to(<%= t_helper.controller_index_route %>) }
118
- it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
119
135
  end
120
- end
121
- context 'as user with read ability' do
122
- login_user_with_ability :read, <%= local_class_name %>
123
- describe 'with valid request' do
124
- before(:each) do
125
- @<%= var_name %> = <%= t_helper.create_factory_model %>
126
- get :show, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
127
- end
128
- it { should respond_with(:success) }
129
- it { should render_template(:show) }
130
- it { should render_with_layout(:application) }
131
- it "assigns the requested <%= var_name %> as @<%= var_name %>" do
132
- assigns(:<%= var_name %>).should eq(@<%= var_name %>)
136
+ context 'as user with read ability' do
137
+ login_user_with_ability :read, <%= local_class_name %>
138
+ describe 'with valid request' do
139
+ before(:each) do
140
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
141
+ get :show, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
142
+ end
143
+ it { should respond_with(:success) }
144
+ it { should render_template(:show) }
145
+ it { should render_with_layout(:application) }
146
+ it "assigns the requested <%= var_name %> as @<%= var_name %>" do
147
+ assigns(:<%= var_name %>).should eq(@<%= var_name %>)
148
+ end
133
149
  end
134
150
  end
135
151
  end
136
152
  end
137
153
 
138
154
  describe "GET new" do
139
- context 'without a user' do
140
- describe 'with valid request' do
141
- before(:each) do
142
- get :new, {<%= t_helper.index_action_params_prefix %>}
155
+ context do # Within default nesting
156
+ <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
157
+ grant_ability :read, <%= model.classify %>
158
+ <%- end -%>
159
+
160
+ context 'without a user' do
161
+ describe 'with valid request' do
162
+ before(:each) do
163
+ get :new, {<%= t_helper.index_action_params_prefix %>}
164
+ end
165
+ it { should redirect_to(new_user_session_path) }
166
+ it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
143
167
  end
144
- it { should redirect_to(new_user_session_path) }
145
- it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
146
168
  end
147
- end
148
- context 'as an unauthorized user' do
149
- login_unauthorized_user
150
- describe 'with valid request' do
151
- before(:each) do
152
- get :new, {<%= t_helper.index_action_params_prefix %>}
169
+ context 'as an unauthorized user' do
170
+ login_unauthorized_user
171
+ describe 'with valid request' do
172
+ before(:each) do
173
+ get :new, {<%= t_helper.index_action_params_prefix %>}
174
+ end
175
+ it { should redirect_to(<%= t_helper.controller_index_route %>) }
176
+ it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
153
177
  end
154
- it { should redirect_to(<%= t_helper.controller_index_route %>) }
155
- it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
156
178
  end
157
- end
158
- context 'as user with create ability' do
159
- login_user_with_ability :create, <%= local_class_name %>
160
- describe 'with valid request' do
161
- before(:each) do
162
- get :new, {<%= t_helper.index_action_params_prefix %>}
163
- end
164
- it { should respond_with(:success) }
165
- it { should render_template(:new) }
166
- it { should render_with_layout(:application) }
167
- it "assigns a new <%= var_name %> as @<%= var_name %>" do
168
- assigns(:<%= var_name %>).should be_a_new(<%= local_class_name %>)
179
+ context 'as user with create ability' do
180
+ login_user_with_ability :create, <%= local_class_name %>
181
+ describe 'with valid request' do
182
+ before(:each) do
183
+ get :new, {<%= t_helper.index_action_params_prefix %>}
184
+ end
185
+ it { should respond_with(:success) }
186
+ it { should render_template(:new) }
187
+ it { should render_with_layout(:application) }
188
+ it "assigns a new <%= var_name %> as @<%= var_name %>" do
189
+ assigns(:<%= var_name %>).should be_a_new(<%= local_class_name %>)
190
+ end
169
191
  end
170
192
  end
171
193
  end
172
194
  end
173
195
 
174
196
  describe "GET edit" do
175
- context 'without a user' do
176
- describe 'with valid request' do
177
- before(:each) do
178
- @<%= var_name %> = <%= t_helper.create_factory_model %>
179
- get :edit, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
197
+ context do # Within default nesting
198
+ <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
199
+ grant_ability :read, <%= model.classify %>
200
+ <%- end -%>
201
+
202
+ context 'without a user' do
203
+ describe 'with valid request' do
204
+ before(:each) do
205
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
206
+ get :edit, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
207
+ end
208
+ it { should redirect_to(new_user_session_path) }
209
+ it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
180
210
  end
181
- it { should redirect_to(new_user_session_path) }
182
- it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
183
211
  end
184
- end
185
- context 'as an unauthorized user' do
186
- login_unauthorized_user
187
- describe 'with valid request' do
188
- before(:each) do
189
- @<%= var_name %> = <%= t_helper.create_factory_model %>
190
- get :edit, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
212
+ context 'as an unauthorized user' do
213
+ login_unauthorized_user
214
+ describe 'with valid request' do
215
+ before(:each) do
216
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
217
+ get :edit, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
218
+ end
219
+ it { should redirect_to(<%= t_helper.controller_index_route %>) }
220
+ it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
191
221
  end
192
- it { should redirect_to(<%= t_helper.controller_index_route %>) }
193
- it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
194
222
  end
195
- end
196
- context 'as user with update ability' do
197
- login_user_with_ability :update, <%= local_class_name %>
198
- describe 'with valid request' do
199
- before(:each) do
200
- @<%= var_name %> = <%= t_helper.create_factory_model %>
201
- get :edit, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
202
- end
203
- it { should respond_with(:success) }
204
- it { should render_template(:edit) }
205
- it { should render_with_layout(:application) }
206
- it "assigns the requested <%= var_name %> as @<%= var_name %>" do
207
- assigns(:<%= var_name %>).should eq(@<%= var_name %>)
223
+ context 'as user with update ability' do
224
+ login_user_with_ability :update, <%= local_class_name %>
225
+ describe 'with valid request' do
226
+ before(:each) do
227
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
228
+ get :edit, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
229
+ end
230
+ it { should respond_with(:success) }
231
+ it { should render_template(:edit) }
232
+ it { should render_with_layout(:application) }
233
+ it "assigns the requested <%= var_name %> as @<%= var_name %>" do
234
+ assigns(:<%= var_name %>).should eq(@<%= var_name %>)
235
+ end
208
236
  end
209
237
  end
210
238
  end
211
239
  end
212
240
 
213
241
  describe "POST create" do
214
- context 'without a user' do
215
- describe 'with valid params' do
216
- before(:each) do
217
- post :create, {<%= t_helper.action_params_prefix %>:<%= var_name %> => valid_create_attributes}
218
- end
219
- it { should redirect_to(new_user_session_path) }
220
- it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
221
- end
222
- end
223
- context 'as an unauthorized user' do
224
- login_unauthorized_user
225
- describe "with valid params" do
226
- before(:each) do
227
- post :create, {<%= t_helper.action_params_prefix %>:<%= var_name %> => valid_create_attributes}
228
- end
229
- it { should redirect_to(<%= t_helper.controller_index_route %>) }
230
- it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
231
- end
232
- end
233
- context 'as user with create ability' do
234
- login_user_with_ability :create, <%= local_class_name %>
235
- describe "with valid params" do
236
- it "creates a new <%= local_class_name %>" do
237
- expect {
242
+ context do # Within default nesting
243
+ <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
244
+ grant_ability :read, <%= model.classify %>
245
+ <%- end -%>
246
+
247
+ context 'without a user' do
248
+ describe 'with valid params' do
249
+ before(:each) do
238
250
  post :create, {<%= t_helper.action_params_prefix %>:<%= var_name %> => valid_create_attributes}
239
- }.to change(<%= local_class_name %>, :count).by(1)
251
+ end
252
+ it { should redirect_to(new_user_session_path) }
253
+ it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
240
254
  end
241
255
  end
242
- describe 'with valid params' do
243
- before(:each) do
244
- post :create, {<%= t_helper.action_params_prefix %>:<%= var_name %> => valid_create_attributes}
245
- end
246
- it "assigns a newly created <%= var_name %> as @<%= var_name %>" do
247
- assigns(:<%= var_name %>).should be_a(<%= local_class_name %>)
248
- assigns(:<%= var_name %>).should be_persisted
249
- end
250
- it "redirects to the created <%= var_name %>" do
251
- response.should redirect_to(<%= t_helper.controller_show_route "#{local_class_name}.last" %>)
256
+ context 'as an unauthorized user' do
257
+ login_unauthorized_user
258
+ describe "with valid params" do
259
+ before(:each) do
260
+ post :create, {<%= t_helper.action_params_prefix %>:<%= var_name %> => valid_create_attributes}
261
+ end
262
+ it { should redirect_to(<%= t_helper.controller_index_route %>) }
263
+ it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
252
264
  end
253
265
  end
254
- describe "with invalid params" do
255
- before(:each) do
256
- # Trigger the behavior that occurs when invalid params are submitted
257
- <%= local_class_name %>.any_instance.stub(:save).and_return(false)
258
- post :create, {<%= t_helper.action_params_prefix %>:<%= var_name %> => <%= formatted_hash(example_invalid_attributes) %>}
259
- end
260
- it { should render_template(:new) }
261
- it { should render_with_layout(:application) }
262
- it "assigns a newly created but unsaved <%= var_name %> as @<%= var_name %>" do
263
- assigns(:<%= var_name %>).should be_a_new(<%= local_class_name %>)
266
+ context 'as user with create ability' do
267
+ login_user_with_ability :create, <%= local_class_name %>
268
+ describe "with valid params" do
269
+ it "creates a new <%= local_class_name %>" do
270
+ expect {
271
+ post :create, {<%= t_helper.action_params_prefix %>:<%= var_name %> => valid_create_attributes}
272
+ }.to change(<%= local_class_name %>, :count).by(1)
273
+ end
274
+ end
275
+ describe 'with valid params' do
276
+ before(:each) do
277
+ post :create, {<%= t_helper.action_params_prefix %>:<%= var_name %> => valid_create_attributes}
278
+ end
279
+ it "assigns a newly created <%= var_name %> as @<%= var_name %>" do
280
+ assigns(:<%= var_name %>).should be_a(<%= local_class_name %>)
281
+ assigns(:<%= var_name %>).should be_persisted
282
+ end
283
+ it "redirects to the created <%= var_name %>" do
284
+ response.should redirect_to(<%= t_helper.controller_show_route "#{local_class_name}.last" %>)
285
+ end
286
+ end
287
+ describe "with invalid params" do
288
+ before(:each) do
289
+ # Trigger the behavior that occurs when invalid params are submitted
290
+ <%= local_class_name %>.any_instance.stub(:save).and_return(false)
291
+ post :create, {<%= t_helper.action_params_prefix %>:<%= var_name %> => <%= formatted_hash(example_invalid_attributes) %>}
292
+ end
293
+ it { should render_template(:new) }
294
+ it { should render_with_layout(:application) }
295
+ it "assigns a newly created but unsaved <%= var_name %> as @<%= var_name %>" do
296
+ assigns(:<%= var_name %>).should be_a_new(<%= local_class_name %>)
297
+ end
264
298
  end
265
299
  end
266
300
  end
267
301
  end
268
302
 
269
303
  describe "PUT update" do
270
- context 'without a user' do
271
- describe 'with valid params' do
272
- before(:each) do
273
- @<%= var_name %> = <%= t_helper.create_factory_model %>
274
- put :update, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param, :<%= var_name %> => valid_update_attributes}
275
- end
276
- it { should redirect_to(new_user_session_path) }
277
- it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
278
- end
279
- end
280
- context 'as an unauthorized user' do
281
- login_unauthorized_user
282
- describe "with valid params" do
283
- before(:each) do
284
- @<%= var_name %> = <%= t_helper.create_factory_model %>
285
- put :update, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param, :<%= var_name %> => valid_update_attributes}
286
- end
287
- it { should redirect_to(<%= t_helper.controller_index_route %>) }
288
- it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
289
- end
290
- end
291
- context 'as user with update ability' do
292
- login_user_with_ability :update, <%= local_class_name %>
293
- describe "with valid params" do
294
- it "updates the requested <%= var_name %>" do
295
- @<%= var_name %> = <%= t_helper.create_factory_model %>
296
- # Assuming there are no other <%= var_name %> in the database, this
297
- # specifies that the <%= local_class_name %> created on the previous line
298
- # receives the :update_attributes message with whatever params are
299
- # submitted in the request.
300
- <%- if Rails.version >= '4' -%>
301
- <%= local_class_name %>.any_instance.should_receive(:update).with(<%= formatted_hash(example_params_for_update) %>)
302
- <%- else -%>
303
- <%= local_class_name %>.any_instance.should_receive(:update_attributes).with(<%= formatted_hash(example_params_for_update) %>)
304
- <%- end -%>
305
- put :update, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param, :<%= var_name %> => <%= formatted_hash(example_params_for_update) %>}
304
+ context do # Within default nesting
305
+ <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
306
+ grant_ability :read, <%= model.classify %>
307
+ <%- end -%>
308
+
309
+ context 'without a user' do
310
+ describe 'with valid params' do
311
+ before(:each) do
312
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
313
+ put :update, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param, :<%= var_name %> => valid_update_attributes}
314
+ end
315
+ it { should redirect_to(new_user_session_path) }
316
+ it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
306
317
  end
307
318
  end
308
- describe "with valid params" do
309
- before(:each) do
310
- @<%= var_name %> = <%= t_helper.create_factory_model %>
311
- put :update, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param, :<%= var_name %> => valid_update_attributes}
312
- end
313
- it "assigns the requested <%= var_name %> as @<%= var_name %>" do
314
- assigns(:<%= var_name %>).should eq(@<%= var_name %>)
315
- end
316
- it "redirects to the <%= var_name %>" do
317
- response.should redirect_to(<%= t_helper.controller_show_route "@#{var_name}" %>)
319
+ context 'as an unauthorized user' do
320
+ login_unauthorized_user
321
+ describe "with valid params" do
322
+ before(:each) do
323
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
324
+ put :update, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param, :<%= var_name %> => valid_update_attributes}
325
+ end
326
+ it { should redirect_to(<%= t_helper.controller_index_route %>) }
327
+ it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
318
328
  end
319
329
  end
320
- describe "with invalid params" do
321
- before(:each) do
322
- @<%= var_name %> = <%= t_helper.create_factory_model %>
323
- # Trigger the behavior that occurs when invalid params are submitted
324
- <%= local_class_name %>.any_instance.stub(:save).and_return(false)
325
- put :update, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param, :<%= var_name %> => <%= formatted_hash(example_invalid_attributes) %>}
326
- end
327
- it { should render_template(:edit) }
328
- it { should render_with_layout(:application) }
329
- it "assigns the <%= var_name %> as @<%= var_name %>" do
330
- assigns(:<%= var_name %>).should eq(@<%= var_name %>)
330
+ context 'as user with update ability' do
331
+ login_user_with_ability :update, <%= local_class_name %>
332
+ describe "with valid params" do
333
+ it "updates the requested <%= var_name %>" do
334
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
335
+ # Assuming there are no other <%= var_name %> in the database, this
336
+ # specifies that the <%= local_class_name %> created on the previous line
337
+ # receives the :update_attributes message with whatever params are
338
+ # submitted in the request.
339
+ <%- if Rails.version >= '4' -%>
340
+ <%= local_class_name %>.any_instance.should_receive(:update).with(<%= formatted_hash(example_params_for_update) %>)
341
+ <%- else -%>
342
+ <%= local_class_name %>.any_instance.should_receive(:update_attributes).with(<%= formatted_hash(example_params_for_update) %>)
343
+ <%- end -%>
344
+ put :update, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param, :<%= var_name %> => <%= formatted_hash(example_params_for_update) %>}
345
+ end
346
+ end
347
+ describe "with valid params" do
348
+ before(:each) do
349
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
350
+ put :update, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param, :<%= var_name %> => valid_update_attributes}
351
+ end
352
+ it "assigns the requested <%= var_name %> as @<%= var_name %>" do
353
+ assigns(:<%= var_name %>).should eq(@<%= var_name %>)
354
+ end
355
+ it "redirects to the <%= var_name %>" do
356
+ response.should redirect_to(<%= t_helper.controller_show_route "@#{var_name}" %>)
357
+ end
358
+ end
359
+ describe "with invalid params" do
360
+ before(:each) do
361
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
362
+ # Trigger the behavior that occurs when invalid params are submitted
363
+ <%= local_class_name %>.any_instance.stub(:save).and_return(false)
364
+ put :update, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param, :<%= var_name %> => <%= formatted_hash(example_invalid_attributes) %>}
365
+ end
366
+ it { should render_template(:edit) }
367
+ it { should render_with_layout(:application) }
368
+ it "assigns the <%= var_name %> as @<%= var_name %>" do
369
+ assigns(:<%= var_name %>).should eq(@<%= var_name %>)
370
+ end
331
371
  end
332
372
  end
333
373
  end
334
374
  end
335
375
 
336
376
  describe "DELETE destroy" do
337
- context 'without a user' do
338
- describe 'with valid request' do
339
- before(:each) do
340
- @<%= var_name %> = <%= t_helper.create_factory_model %>
341
- delete :destroy, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
377
+ context do # Within default nesting
378
+ <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
379
+ grant_ability :read, <%= model.classify %>
380
+ <%- end -%>
381
+
382
+ context 'without a user' do
383
+ describe 'with valid request' do
384
+ before(:each) do
385
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
386
+ delete :destroy, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
387
+ end
388
+ it { should redirect_to(new_user_session_path) }
389
+ it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
342
390
  end
343
- it { should redirect_to(new_user_session_path) }
344
- it { should set_the_flash[:alert].to("You need to sign in or sign up before continuing.") }
345
391
  end
346
- end
347
- context 'as an unauthorized user' do
348
- login_unauthorized_user
349
- describe "with valid request" do
350
- before(:each) do
351
- @<%= var_name %> = <%= t_helper.create_factory_model %>
352
- delete :destroy, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
392
+ context 'as an unauthorized user' do
393
+ login_unauthorized_user
394
+ describe "with valid request" do
395
+ before(:each) do
396
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
397
+ delete :destroy, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
398
+ end
399
+ it { should redirect_to(<%= t_helper.controller_index_route %>) }
400
+ it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
353
401
  end
354
- it { should redirect_to(<%= t_helper.controller_index_route %>) }
355
- it { should set_the_flash[:alert].to("You are not authorized to access this page.") }
356
402
  end
357
- end
358
- context 'as user with destroy ability' do
359
- login_user_with_ability :destroy, <%= local_class_name %>
360
- it "destroys the requested <%= var_name %>" do
361
- @<%= var_name %> = <%= t_helper.create_factory_model %>
362
- expect {
363
- delete :destroy, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
364
- }.to change(<%= local_class_name %>, :count).by(-1)
365
- end
366
- describe 'with valid request' do
367
- before(:each) do
403
+ context 'as user with destroy ability' do
404
+ login_user_with_ability :destroy, <%= local_class_name %>
405
+ it "destroys the requested <%= var_name %>" do
368
406
  @<%= var_name %> = <%= t_helper.create_factory_model %>
369
- delete :destroy, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
370
- end
371
- it "redirects to the <%= var_name %> list" do
372
- response.should redirect_to(<%= t_helper.controller_index_route %>)
407
+ expect {
408
+ delete :destroy, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
409
+ }.to change(<%= local_class_name %>, :count).by(-1)
410
+ end
411
+ describe 'with valid request' do
412
+ before(:each) do
413
+ @<%= var_name %> = <%= t_helper.create_factory_model %>
414
+ delete :destroy, {<%= t_helper.action_params_prefix %>:id => @<%= var_name %>.to_param}
415
+ end
416
+ it "redirects to the <%= var_name %> list" do
417
+ response.should redirect_to(<%= t_helper.controller_index_route %>)
418
+ end
373
419
  end
374
420
  end
375
421
  end