authorized_rails_scaffolds 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/authorized_rails_scaffolds/helper.rb +6 -0
- data/lib/authorized_rails_scaffolds/version.rb +1 -1
- data/lib/generators/authorized_rails_scaffolds/install_macros/templates/devise_can_can/USAGE +1 -1
- data/lib/generators/authorized_rails_scaffolds/install_macros/templates/devise_can_can/controller_macros.rb +34 -15
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/controller.rb +2 -2
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/controller_spec.rb +292 -246
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/edit_spec.rb +52 -43
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/index_spec.rb +101 -95
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/new_spec.rb +51 -43
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/show_spec.rb +49 -41
- metadata +8 -8
@@ -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,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
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
14
|
-
|
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
|
data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/controller.rb
CHANGED
@@ -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
|
-
|
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
|
|
data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/controller_spec.rb
CHANGED
@@ -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
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
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
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
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
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
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
|
140
|
-
|
141
|
-
|
142
|
-
|
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
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
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
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
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
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
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
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
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
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
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
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
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
|
-
|
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
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
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
|
-
|
255
|
-
|
256
|
-
|
257
|
-
<%= local_class_name
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
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
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
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
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
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
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
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
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
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
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
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
|
-
|
358
|
-
|
359
|
-
|
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
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
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
|