inherited_resources 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module InheritedResources
2
- VERSION = '1.2.2'.freeze
2
+ VERSION = '1.3.0'.freeze
3
3
  end
data/test/aliases_test.rb CHANGED
@@ -126,7 +126,16 @@ class AliasesTest < ActionController::TestCase
126
126
 
127
127
  def test_options_are_used_in_respond_with
128
128
  @request.accept = "application/xml"
129
- Student.stubs(:new).returns(mock_student(:save => true, :to_xml => "XML"))
129
+ mock_student = mock_student(:save => true, :to_xml => "XML")
130
+ Student.stubs(:new).returns(mock_student)
131
+
132
+ # Bug in mocha does not accept strings on respond_to
133
+ mock_student.singleton_class.class_eval do
134
+ def respond_to?(method, *)
135
+ method == "to_xml" || super
136
+ end
137
+ end
138
+
130
139
  post :create
131
140
  assert_equal "http://test.host/", @response.location
132
141
  end
data/test/base_test.rb CHANGED
@@ -104,6 +104,14 @@ class ShowActionBaseTest < ActionController::TestCase
104
104
  @request.accept = 'application/xml'
105
105
  User.expects(:find).with('42').returns(mock_user)
106
106
  mock_user.expects(:to_xml).returns("Generated XML")
107
+
108
+ # Bug in mocha does not accept strings on respond_to
109
+ mock_user.singleton_class.class_eval do
110
+ def respond_to?(method, *)
111
+ method == "to_xml" || super
112
+ end
113
+ end
114
+
107
115
  get :show, :id => '42'
108
116
  assert_response :success
109
117
  assert_equal 'Generated XML', @response.body
@@ -130,6 +138,14 @@ class NewActionBaseTest < ActionController::TestCase
130
138
  @request.accept = 'application/xml'
131
139
  User.expects(:new).returns(mock_user)
132
140
  mock_user.expects(:to_xml).returns("Generated XML")
141
+
142
+ # Bug in mocha does not accept strings on respond_to
143
+ mock_user.singleton_class.class_eval do
144
+ def respond_to?(method, *)
145
+ method == "to_xml" || super
146
+ end
147
+ end
148
+
133
149
  get :new
134
150
  assert_response :success
135
151
  assert_equal 'Generated XML', @response.body
@@ -162,6 +178,14 @@ class CreateActionBaseTest < ActionController::TestCase
162
178
  post :create, :user => {:these => 'params'}
163
179
  assert_equal mock_user, assigns(:user)
164
180
  end
181
+
182
+ def test_expose_a_newly_create_user_when_saved_with_success_and_role_setted
183
+ @controller.class.send(:with_role, :admin)
184
+ User.expects(:new).with({'these' => 'params'}, {:as => :admin}).returns(mock_user(:save => true))
185
+ post :create, :user => {:these => 'params'}
186
+ assert_equal mock_user, assigns(:user)
187
+ @controller.class.send(:with_role, nil)
188
+ end
165
189
 
166
190
  def test_redirect_to_the_created_user
167
191
  User.stubs(:new).returns(mock_user(:save => true))
@@ -205,8 +229,17 @@ class UpdateActionBaseTest < ActionController::TestCase
205
229
  put :update, :id => '42', :user => {:these => 'params'}
206
230
  assert_equal mock_user, assigns(:user)
207
231
  end
232
+
233
+ def test_update_the_requested_object_when_setted_role
234
+ @controller.class.send(:with_role, :admin)
235
+ User.expects(:find).with('42').returns(mock_user)
236
+ mock_user.expects(:update_attributes).with({'these' => 'params'}, {:as => :admin}).returns(true)
237
+ put :update, :id => '42', :user => {:these => 'params'}
238
+ assert_equal mock_user, assigns(:user)
239
+ @controller.class.send(:with_role, nil)
240
+ end
208
241
 
209
- def test_redirect_to_the_created_user
242
+ def test_redirect_to_the_updated_user
210
243
  User.stubs(:find).returns(mock_user(:update_attributes => true))
211
244
  @controller.expects(:resource_url).returns('http://test.host/')
212
245
  put :update
@@ -26,7 +26,7 @@ class CarsController < InheritedResources::Base
26
26
  end
27
27
 
28
28
  def update_resource(resource, attributes)
29
- resource.update_successfully(attributes)
29
+ resource.update_successfully(*attributes)
30
30
  end
31
31
 
32
32
  def destroy_resource(resource)
@@ -0,0 +1,369 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ class Student; end
4
+ class Manager; end
5
+ class Employee; end
6
+
7
+ class Project
8
+ def self.human_name; "Project"; end
9
+ end
10
+
11
+ class ProjectsController < InheritedResources::Base
12
+ belongs_to :student, :manager, :employee, :optional => true
13
+ end
14
+
15
+ class MultipleNestedOptionalTest < ActionController::TestCase
16
+ tests ProjectsController
17
+
18
+ def setup
19
+ @controller.stubs(:resource_url).returns('/')
20
+ @controller.stubs(:collection_url).returns('/')
21
+ end
22
+
23
+ # INDEX
24
+ def test_expose_all_projects_as_instance_variable_with_student
25
+ Student.expects(:find).with('37').returns(mock_student)
26
+ mock_student.expects(:projects).returns(Project)
27
+ Project.expects(:scoped).returns([mock_project])
28
+ get :index, :student_id => '37'
29
+ assert_equal mock_student, assigns(:student)
30
+ assert_equal [mock_project], assigns(:projects)
31
+ end
32
+
33
+ def test_expose_all_projects_as_instance_variable_with_manager
34
+ Manager.expects(:find).with('38').returns(mock_manager)
35
+ mock_manager.expects(:projects).returns(Project)
36
+ Project.expects(:scoped).returns([mock_project])
37
+ get :index, :manager_id => '38'
38
+ assert_equal mock_manager, assigns(:manager)
39
+ assert_equal [mock_project], assigns(:projects)
40
+ end
41
+
42
+ def test_expose_all_projects_as_instance_variable_with_employee
43
+ Employee.expects(:find).with('666').returns(mock_employee)
44
+ mock_employee.expects(:projects).returns(Project)
45
+ Project.expects(:scoped).returns([mock_project])
46
+ get :index, :employee_id => '666'
47
+ assert_equal mock_employee, assigns(:employee)
48
+ assert_equal [mock_project], assigns(:projects)
49
+ end
50
+
51
+ def test_expose_all_projects_as_instance_variable_with_manager_and_employee
52
+ Manager.expects(:find).with('37').returns(mock_manager)
53
+ mock_manager.expects(:employees).returns(Employee)
54
+ Employee.expects(:find).with('42').returns(mock_employee)
55
+ mock_employee.expects(:projects).returns(Project)
56
+ Project.expects(:scoped).returns([mock_project])
57
+ get :index, :manager_id => '37', :employee_id => '42'
58
+ assert_equal mock_manager, assigns(:manager)
59
+ assert_equal mock_employee, assigns(:employee)
60
+ assert_equal [mock_project], assigns(:projects)
61
+ end
62
+
63
+ def test_expose_all_projects_as_instance_variable_without_parents
64
+ Project.expects(:scoped).returns([mock_project])
65
+ get :index
66
+ assert_equal [mock_project], assigns(:projects)
67
+ end
68
+
69
+ # SHOW
70
+ def test_expose_the_requested_project_with_student
71
+ Student.expects(:find).with('37').returns(mock_student)
72
+ mock_student.expects(:projects).returns(Project)
73
+ Project.expects(:find).with('42').returns(mock_project)
74
+ get :show, :id => '42', :student_id => '37'
75
+ assert_equal mock_student, assigns(:student)
76
+ assert_equal mock_project, assigns(:project)
77
+ end
78
+
79
+ def test_expose_the_requested_project_with_manager
80
+ Manager.expects(:find).with('37').returns(mock_manager)
81
+ mock_manager.expects(:projects).returns(Project)
82
+ Project.expects(:find).with('42').returns(mock_project)
83
+ get :show, :id => '42', :manager_id => '37'
84
+ assert_equal mock_manager, assigns(:manager)
85
+ assert_equal mock_project, assigns(:project)
86
+ end
87
+
88
+ def test_expose_the_requested_project_with_employee
89
+ Employee.expects(:find).with('37').returns(mock_employee)
90
+ mock_employee.expects(:projects).returns(Project)
91
+ Project.expects(:find).with('42').returns(mock_project)
92
+ get :show, :id => '42', :employee_id => '37'
93
+ assert_equal mock_employee, assigns(:employee)
94
+ assert_equal mock_project, assigns(:project)
95
+ end
96
+
97
+ def test_expose_the_requested_project_with_manager_and_employee
98
+ Manager.expects(:find).with('37').returns(mock_manager)
99
+ mock_manager.expects(:employees).returns(Employee)
100
+ Employee.expects(:find).with('42').returns(mock_employee)
101
+ mock_employee.expects(:projects).returns(Project)
102
+ Project.expects(:find).with('13').returns(mock_project)
103
+ get :show, :id => '13', :manager_id => '37', :employee_id => '42'
104
+ assert_equal mock_manager, assigns(:manager)
105
+ assert_equal mock_employee, assigns(:employee)
106
+ assert_equal mock_project, assigns(:project)
107
+ end
108
+
109
+ def test_expose_the_requested_project_without_parents
110
+ Project.expects(:find).with('13').returns(mock_project)
111
+ get :show, :id => '13'
112
+ assert_equal mock_project, assigns(:project)
113
+ end
114
+
115
+ # NEW
116
+ def test_expose_a_new_project_with_student
117
+ Student.expects(:find).with('37').returns(mock_student)
118
+ mock_student.expects(:projects).returns(Project)
119
+ Project.expects(:build).returns(mock_project)
120
+ get :new, :student_id => '37'
121
+ assert_equal mock_student, assigns(:student)
122
+ assert_equal mock_project, assigns(:project)
123
+ end
124
+
125
+ def test_expose_a_new_project_with_manager
126
+ Manager.expects(:find).with('37').returns(mock_manager)
127
+ mock_manager.expects(:projects).returns(Project)
128
+ Project.expects(:build).returns(mock_project)
129
+ get :new, :manager_id => '37'
130
+ assert_equal mock_manager, assigns(:manager)
131
+ assert_equal mock_project, assigns(:project)
132
+ end
133
+
134
+ def test_expose_a_new_project_with_employee
135
+ Employee.expects(:find).with('37').returns(mock_employee)
136
+ mock_employee.expects(:projects).returns(Project)
137
+ Project.expects(:build).returns(mock_project)
138
+ get :new, :employee_id => '37'
139
+ assert_equal mock_employee, assigns(:employee)
140
+ assert_equal mock_project, assigns(:project)
141
+ end
142
+
143
+ def test_expose_a_new_project_with_manager_and_employee
144
+ Manager.expects(:find).with('37').returns(mock_manager)
145
+ mock_manager.expects(:employees).returns(Employee)
146
+ Employee.expects(:find).with('42').returns(mock_employee)
147
+ mock_employee.expects(:projects).returns(Project)
148
+ Project.expects(:build).returns(mock_project)
149
+ get :new, :manager_id => '37', :employee_id => '42'
150
+ assert_equal mock_manager, assigns(:manager)
151
+ assert_equal mock_employee, assigns(:employee)
152
+ assert_equal mock_project, assigns(:project)
153
+ end
154
+
155
+ def test_expose_a_new_project_without_parents
156
+ Project.expects(:new).returns(mock_project)
157
+ get :new
158
+ assert_equal mock_project, assigns(:project)
159
+ end
160
+
161
+ # EDIT
162
+ def test_expose_the_requested_project_for_edition_with_student
163
+ Student.expects(:find).with('37').returns(mock_student)
164
+ mock_student.expects(:projects).returns(Project)
165
+ Project.expects(:find).with('42').returns(mock_project)
166
+ get :edit, :id => '42', :student_id => '37'
167
+ assert_equal mock_student, assigns(:student)
168
+ assert_equal mock_project, assigns(:project)
169
+ end
170
+
171
+ def test_expose_the_requested_project_for_edition_with_manager
172
+ Manager.expects(:find).with('37').returns(mock_manager)
173
+ mock_manager.expects(:projects).returns(Project)
174
+ Project.expects(:find).with('42').returns(mock_project)
175
+ get :edit, :id => '42', :manager_id => '37'
176
+ assert_equal mock_manager, assigns(:manager)
177
+ assert_equal mock_project, assigns(:project)
178
+ end
179
+
180
+ def test_expose_the_requested_project_for_edition_with_employee
181
+ Employee.expects(:find).with('37').returns(mock_employee)
182
+ mock_employee.expects(:projects).returns(Project)
183
+ Project.expects(:find).with('42').returns(mock_project)
184
+ get :edit, :id => '42', :employee_id => '37'
185
+ assert_equal mock_employee, assigns(:employee)
186
+ assert_equal mock_project, assigns(:project)
187
+ end
188
+
189
+ def test_expose_the_requested_project_for_edition_with_manager_and_employee
190
+ Manager.expects(:find).with('37').returns(mock_manager)
191
+ mock_manager.expects(:employees).returns(Employee)
192
+ Employee.expects(:find).with('42').returns(mock_employee)
193
+ mock_employee.expects(:projects).returns(Project)
194
+ Project.expects(:find).with('13').returns(mock_project)
195
+ get :edit, :id => '13', :manager_id => '37', :employee_id => '42'
196
+ assert_equal mock_manager, assigns(:manager)
197
+ assert_equal mock_employee, assigns(:employee)
198
+ assert_equal mock_project, assigns(:project)
199
+ end
200
+
201
+ def test_expose_the_requested_project_for_edition_without_parents
202
+ Project.expects(:find).with('13').returns(mock_project)
203
+ get :edit, :id => '13'
204
+ assert_equal mock_project, assigns(:project)
205
+ end
206
+
207
+ # CREATE
208
+ def test_expose_a_newly_created_project_with_student
209
+ Student.expects(:find).with('37').returns(mock_student)
210
+ mock_student.expects(:projects).returns(Project)
211
+ Project.expects(:build).with({ 'these' => 'params' }).returns(mock_project(:save => true))
212
+ post :create, :student_id => '37', :project => { :these => 'params' }
213
+ assert_equal mock_student, assigns(:student)
214
+ assert_equal mock_project, assigns(:project)
215
+ end
216
+
217
+ def test_expose_a_newly_created_project_with_manager
218
+ Manager.expects(:find).with('37').returns(mock_manager)
219
+ mock_manager.expects(:projects).returns(Project)
220
+ Project.expects(:build).with({ 'these' => 'params' }).returns(mock_project(:save => true))
221
+ post :create, :manager_id => '37', :project => { :these => 'params' }
222
+ assert_equal mock_manager, assigns(:manager)
223
+ assert_equal mock_project, assigns(:project)
224
+ end
225
+
226
+ def test_expose_a_newly_created_project_with_employee
227
+ Employee.expects(:find).with('37').returns(mock_employee)
228
+ mock_employee.expects(:projects).returns(Project)
229
+ Project.expects(:build).with({ 'these' => 'params' }).returns(mock_project(:save => true))
230
+ post :create, :employee_id => '37', :project => { :these => 'params' }
231
+ assert_equal mock_employee, assigns(:employee)
232
+ assert_equal mock_project, assigns(:project)
233
+ end
234
+
235
+ def test_expose_a_newly_created_project_with_manager_and_employee
236
+ Manager.expects(:find).with('37').returns(mock_manager)
237
+ mock_manager.expects(:employees).returns(Employee)
238
+ Employee.expects(:find).with('42').returns(mock_employee)
239
+ mock_employee.expects(:projects).returns(Project)
240
+ Project.expects(:build).with({ 'these' => 'params' }).returns(mock_project(:save => true))
241
+ post :create, :manager_id => '37', :employee_id => '42', :project => { :these => 'params' }
242
+ assert_equal mock_manager, assigns(:manager)
243
+ assert_equal mock_employee, assigns(:employee)
244
+ assert_equal mock_project, assigns(:project)
245
+ end
246
+
247
+ def test_expose_a_newly_created_project_without_parents
248
+ Project.expects(:new).with({ 'these' => 'params' }).returns(mock_project(:save => true))
249
+ post :create, :project => { :these => 'params' }
250
+ assert_equal mock_project, assigns(:project)
251
+ end
252
+
253
+ # UPDATE
254
+ def test_update_the_requested_project_with_student
255
+ Student.expects(:find).with('37').returns(mock_student)
256
+ mock_student.expects(:projects).returns(Project)
257
+ Project.expects(:find).with('42').returns(mock_project)
258
+ mock_project.expects(:update_attributes).with({ 'these' => 'params' }).returns(true)
259
+ put :update, :id => '42', :student_id => '37', :project => { :these => 'params' }
260
+ assert_equal mock_student, assigns(:student)
261
+ assert_equal mock_project, assigns(:project)
262
+ end
263
+
264
+ def test_update_the_requested_project_with_manager
265
+ Manager.expects(:find).with('37').returns(mock_manager)
266
+ mock_manager.expects(:projects).returns(Project)
267
+ Project.expects(:find).with('42').returns(mock_project)
268
+ mock_project.expects(:update_attributes).with({ 'these' => 'params' }).returns(true)
269
+ put :update, :id => '42', :manager_id => '37', :project => { :these => 'params' }
270
+ assert_equal mock_manager, assigns(:manager)
271
+ assert_equal mock_project, assigns(:project)
272
+ end
273
+
274
+ def test_update_the_requested_project_with_employee
275
+ Employee.expects(:find).with('37').returns(mock_employee)
276
+ mock_employee.expects(:projects).returns(Project)
277
+ Project.expects(:find).with('42').returns(mock_project)
278
+ mock_project.expects(:update_attributes).with({ 'these' => 'params' }).returns(true)
279
+ put :update, :id => '42', :employee_id => '37', :project => { :these => 'params' }
280
+ assert_equal mock_employee, assigns(:employee)
281
+ assert_equal mock_project, assigns(:project)
282
+ end
283
+
284
+ def test_update_the_requested_project_with_manager_and_employee
285
+ Manager.expects(:find).with('37').returns(mock_manager)
286
+ mock_manager.expects(:employees).returns(Employee)
287
+ Employee.expects(:find).with('13').returns(mock_employee)
288
+ mock_employee.expects(:projects).returns(Project)
289
+ Project.expects(:find).with('42').returns(mock_project)
290
+ mock_project.expects(:update_attributes).with({ 'these' => 'params' }).returns(true)
291
+ put :update, :id => '42', :manager_id => '37', :employee_id => '13', :project => { :these => 'params' }
292
+ assert_equal mock_manager, assigns(:manager)
293
+ assert_equal mock_employee, assigns(:employee)
294
+ assert_equal mock_project, assigns(:project)
295
+ end
296
+
297
+ # DESTROY
298
+ def test_the_requested_project_is_destroyed_with_student
299
+ Student.expects(:find).with('37').returns(mock_student)
300
+ mock_student.expects(:projects).returns(Project)
301
+ Project.expects(:find).with('42').returns(mock_project)
302
+ mock_project.expects(:destroy).returns(true)
303
+
304
+ delete :destroy, :id => '42', :student_id => '37'
305
+ assert_equal mock_student, assigns(:student)
306
+ assert_equal mock_project, assigns(:project)
307
+ end
308
+
309
+ def test_the_requested_project_is_destroyed_with_manager
310
+ Manager.expects(:find).with('37').returns(mock_manager)
311
+ mock_manager.expects(:projects).returns(Project)
312
+ Project.expects(:find).with('42').returns(mock_project)
313
+ mock_project.expects(:destroy).returns(true)
314
+
315
+ delete :destroy, :id => '42', :manager_id => '37'
316
+ assert_equal mock_manager, assigns(:manager)
317
+ assert_equal mock_project, assigns(:project)
318
+ end
319
+
320
+ def test_the_requested_project_is_destroyed_with_employee
321
+ Employee.expects(:find).with('37').returns(mock_employee)
322
+ mock_employee.expects(:projects).returns(Project)
323
+ Project.expects(:find).with('42').returns(mock_project)
324
+ mock_project.expects(:destroy).returns(true)
325
+
326
+ delete :destroy, :id => '42', :employee_id => '37'
327
+ assert_equal mock_employee, assigns(:employee)
328
+ assert_equal mock_project, assigns(:project)
329
+ end
330
+
331
+ def test_the_requested_project_is_destroyed_with_manager_and_employee
332
+ Manager.expects(:find).with('37').returns(mock_manager)
333
+ mock_manager.expects(:employees).returns(Employee)
334
+ Employee.expects(:find).with('13').returns(mock_employee)
335
+ mock_employee.expects(:projects).returns(Project)
336
+ Project.expects(:find).with('42').returns(mock_project)
337
+ mock_project.expects(:destroy).returns(true)
338
+
339
+ delete :destroy, :id => '42', :manager_id => '37', :employee_id => '13'
340
+ assert_equal mock_manager, assigns(:manager)
341
+ assert_equal mock_employee, assigns(:employee)
342
+ assert_equal mock_project, assigns(:project)
343
+ end
344
+
345
+ def test_the_requested_project_is_destroyed_without_parents
346
+ Project.expects(:find).with('42').returns(mock_project)
347
+ mock_project.expects(:destroy).returns(true)
348
+
349
+ delete :destroy, :id => '42'
350
+ assert_equal mock_project, assigns(:project)
351
+ end
352
+
353
+ protected
354
+ def mock_manager(stubs={})
355
+ @mock_manager ||= mock(stubs)
356
+ end
357
+
358
+ def mock_employee(stubs={})
359
+ @mock_employee ||= mock(stubs)
360
+ end
361
+
362
+ def mock_student(stubs={})
363
+ @mock_student ||= mock(stubs)
364
+ end
365
+
366
+ def mock_project(stubs={})
367
+ @mock_project ||= mock(stubs)
368
+ end
369
+ end