josevalim-inherited_resources 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,4 +1,8 @@
1
- Version 0.1
1
+ # Version 0.1.1
2
+
3
+ * Added Rails 2.3.0 and changed tests to work with ActionController::TestCase.
4
+
5
+ # Version 0.1.0
2
6
 
3
7
  * Initial release. Support to I18n, singleton controllers, polymorphic
4
8
  controllers, belongs_to, nested_belongs_to and url helpers.
data/README CHANGED
@@ -1,6 +1,6 @@
1
1
  Inherited Resources
2
2
  License: MIT
3
- Version: 0.1
3
+ Version: 0.1.1
4
4
 
5
5
  You can also read this README in pretty html at the GitHub project Wiki page:
6
6
 
@@ -13,6 +13,11 @@ Inherited Resources speeds up development by making your controllers inherit
13
13
  all restful actions so you just have to focus on what is important. It makes
14
14
  your controllers more powerful and cleaner at the same time.
15
15
 
16
+ Plus, making your controllers follow a pattern, it helps you to write better
17
+ code by following fat models and skinny controllers convention.
18
+
19
+ Inherited Resources is tested and compatible with Rails 2.2.2 and Rails 2.3.0.
20
+
16
21
  keywords: resources, controller, singleton, belongs_to, polymorphic and I18n
17
22
 
18
23
  Installation
@@ -258,12 +263,11 @@ practices says that you should never nest more than two resources, but sometimes
258
263
  you have to for security reasons. So this is an example of how you can do it:
259
264
 
260
265
  class CommentsController < InheritedResources::Base
261
- nested_belongs_to :project
262
- nested_belongs_to :task
266
+ nested_belongs_to :project, :task
263
267
  end
264
268
 
265
- nested_belongs_to is actually just an alias to belongs_to. This is an
266
- alternative declaration:
269
+ nested_belongs_to is actually just an alias to belongs_to created to make clear
270
+ what is happening. You can also declare nested_belongs_to like this:
267
271
 
268
272
  class CommentsController < InheritedResources::Base
269
273
  belongs_to :project do
@@ -255,6 +255,18 @@ module ActionController #:nodoc:
255
255
  head :not_acceptable
256
256
  return false
257
257
  end
258
+
259
+ private
260
+
261
+ # Define template_exists? for Rails 2.3
262
+ unless ActionController::Base.private_instance_methods.include? 'template_exists?'
263
+ def template_exists?
264
+ self.view_paths.find_template("#{controller_name}/#{action_name}", response.template.template_format)
265
+ rescue ActionView::MissingTemplate
266
+ false
267
+ end
268
+ end
269
+
258
270
  end
259
271
 
260
272
  module MimeResponds #:nodoc:
data/test/aliases_test.rb CHANGED
@@ -19,7 +19,7 @@ class StudentsController < InheritedResources::Base
19
19
 
20
20
  end
21
21
 
22
- class AliasesBaseTest < Test::Unit::TestCase
22
+ class AliasesBaseTest < TEST_CLASS
23
23
 
24
24
  def setup
25
25
  @controller = StudentsController.new
@@ -11,7 +11,7 @@ class AddressesController < InheritedResources::Base
11
11
  end
12
12
  end
13
13
 
14
- class FlashBaseHelpersTest < Test::Unit::TestCase
14
+ class FlashBaseHelpersTest < TEST_CLASS
15
15
 
16
16
  def setup
17
17
  @controller = AddressesController.new
@@ -75,7 +75,7 @@ class PetsController < InheritedResources::Base
75
75
  end
76
76
  end
77
77
 
78
- class AssociationChainBaseHelpersTest < Test::Unit::TestCase
78
+ class AssociationChainBaseHelpersTest < TEST_CLASS
79
79
 
80
80
  def setup
81
81
  @controller = PetsController.new
data/test/base_test.rb CHANGED
@@ -24,7 +24,7 @@ module UserTestHelper
24
24
  end
25
25
  end
26
26
 
27
- class IndexActionBaseTest < Test::Unit::TestCase
27
+ class IndexActionBaseTest < TEST_CLASS
28
28
  include UserTestHelper
29
29
 
30
30
  def test_expose_all_users_as_instance_variable
@@ -50,7 +50,7 @@ class IndexActionBaseTest < Test::Unit::TestCase
50
50
  end
51
51
  end
52
52
 
53
- class ShowActionBaseTest < Test::Unit::TestCase
53
+ class ShowActionBaseTest < TEST_CLASS
54
54
  include UserTestHelper
55
55
 
56
56
  def test_expose_the_resquested_user
@@ -76,7 +76,7 @@ class ShowActionBaseTest < Test::Unit::TestCase
76
76
  end
77
77
  end
78
78
 
79
- class NewActionBaseTest < Test::Unit::TestCase
79
+ class NewActionBaseTest < TEST_CLASS
80
80
  include UserTestHelper
81
81
 
82
82
  def test_expose_a_new_user
@@ -102,7 +102,7 @@ class NewActionBaseTest < Test::Unit::TestCase
102
102
  end
103
103
  end
104
104
 
105
- class EditActionBaseTest < Test::Unit::TestCase
105
+ class EditActionBaseTest < TEST_CLASS
106
106
  include UserTestHelper
107
107
 
108
108
  def test_expose_the_resquested_user
@@ -120,7 +120,7 @@ class EditActionBaseTest < Test::Unit::TestCase
120
120
  end
121
121
  end
122
122
 
123
- class CreateActionBaseTest < Test::Unit::TestCase
123
+ class CreateActionBaseTest < TEST_CLASS
124
124
  include UserTestHelper
125
125
 
126
126
  def test_expose_a_newly_create_user_when_saved_with_success
@@ -146,7 +146,7 @@ class CreateActionBaseTest < Test::Unit::TestCase
146
146
  User.stubs(:new).returns(mock_user(:save => false, :errors => []))
147
147
  post :create
148
148
  assert_response :success
149
- assert_template 'new'
149
+ assert_template :new
150
150
  end
151
151
 
152
152
  def test_dont_show_flash_message_when_user_cannot_be_saved
@@ -156,7 +156,7 @@ class CreateActionBaseTest < Test::Unit::TestCase
156
156
  end
157
157
  end
158
158
 
159
- class UpdateActionBaseTest < Test::Unit::TestCase
159
+ class UpdateActionBaseTest < TEST_CLASS
160
160
  include UserTestHelper
161
161
 
162
162
  def test_update_the_requested_object
@@ -183,7 +183,7 @@ class UpdateActionBaseTest < Test::Unit::TestCase
183
183
  User.stubs(:find).returns(mock_user(:update_attributes => false, :errors => []))
184
184
  put :update
185
185
  assert_response :success
186
- assert_template 'edit'
186
+ assert_template :edit
187
187
  end
188
188
 
189
189
  def test_dont_show_flash_message_when_user_cannot_be_saved
@@ -193,7 +193,7 @@ class UpdateActionBaseTest < Test::Unit::TestCase
193
193
  end
194
194
  end
195
195
 
196
- class DestroyActionBaseTest < Test::Unit::TestCase
196
+ class DestroyActionBaseTest < TEST_CLASS
197
197
  include UserTestHelper
198
198
 
199
199
  def test_the_resquested_user_is_destroyed
@@ -32,7 +32,7 @@ module CommentTestHelper
32
32
  end
33
33
  end
34
34
 
35
- class IndexActionBelongsToTest < Test::Unit::TestCase
35
+ class IndexActionBelongsToTest < TEST_CLASS
36
36
  include CommentTestHelper
37
37
 
38
38
  def test_expose_all_comments_as_instance_variable
@@ -64,7 +64,7 @@ class IndexActionBelongsToTest < Test::Unit::TestCase
64
64
  end
65
65
  end
66
66
 
67
- class ShowActionBelongsToTest < Test::Unit::TestCase
67
+ class ShowActionBelongsToTest < TEST_CLASS
68
68
  include CommentTestHelper
69
69
 
70
70
  def test_expose_the_resquested_comment
@@ -96,7 +96,7 @@ class ShowActionBelongsToTest < Test::Unit::TestCase
96
96
  end
97
97
  end
98
98
 
99
- class NewActionBelongsToTest < Test::Unit::TestCase
99
+ class NewActionBelongsToTest < TEST_CLASS
100
100
  include CommentTestHelper
101
101
 
102
102
  def test_expose_a_new_comment
@@ -128,7 +128,7 @@ class NewActionBelongsToTest < Test::Unit::TestCase
128
128
  end
129
129
  end
130
130
 
131
- class EditActionBelongsToTest < Test::Unit::TestCase
131
+ class EditActionBelongsToTest < TEST_CLASS
132
132
  include CommentTestHelper
133
133
 
134
134
  def test_expose_the_resquested_comment
@@ -150,7 +150,7 @@ class EditActionBelongsToTest < Test::Unit::TestCase
150
150
  end
151
151
  end
152
152
 
153
- class CreateActionBelongsToTest < Test::Unit::TestCase
153
+ class CreateActionBelongsToTest < TEST_CLASS
154
154
  include CommentTestHelper
155
155
 
156
156
  def test_expose_a_newly_create_comment_when_saved_with_success
@@ -182,7 +182,7 @@ class CreateActionBelongsToTest < Test::Unit::TestCase
182
182
  Comment.stubs(:build).returns(mock_comment(:save => false, :errors => []))
183
183
  post :create
184
184
  assert_response :success
185
- assert_template 'new'
185
+ assert_template :new
186
186
  end
187
187
 
188
188
  def test_dont_show_flash_message_when_comment_cannot_be_saved
@@ -193,7 +193,7 @@ class CreateActionBelongsToTest < Test::Unit::TestCase
193
193
  end
194
194
  end
195
195
 
196
- class UpdateActionBelongsToTest < Test::Unit::TestCase
196
+ class UpdateActionBelongsToTest < TEST_CLASS
197
197
  include CommentTestHelper
198
198
 
199
199
  def test_update_the_requested_object
@@ -226,7 +226,7 @@ class UpdateActionBelongsToTest < Test::Unit::TestCase
226
226
  Comment.stubs(:find).returns(mock_comment(:update_attributes => false, :errors => []))
227
227
  put :update
228
228
  assert_response :success
229
- assert_template 'edit'
229
+ assert_template :edit
230
230
  end
231
231
 
232
232
  def test_dont_show_flash_message_when_comment_cannot_be_saved
@@ -237,7 +237,7 @@ class UpdateActionBelongsToTest < Test::Unit::TestCase
237
237
  end
238
238
  end
239
239
 
240
- class DestroyActionBelongsToTest < Test::Unit::TestCase
240
+ class DestroyActionBelongsToTest < TEST_CLASS
241
241
  include CommentTestHelper
242
242
 
243
243
  def test_the_resquested_comment_is_destroyed
@@ -19,7 +19,7 @@ class ProfessorsController < InheritedResources::Base
19
19
  end
20
20
 
21
21
  # Create a TestHelper module with some helpers
22
- class BelongsToTest < Test::Unit::TestCase
22
+ class BelongsToTest < TEST_CLASS
23
23
  def setup
24
24
  @controller = ProfessorsController.new
25
25
  @controller.request = @request = ActionController::TestRequest.new
@@ -1,41 +1,41 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
3
  class Book; end
4
4
  class Folder; end
5
-
6
- class BooksController < InheritedResources::Base
5
+
6
+ class BooksController < InheritedResources::Base
7
7
  actions :index, :show
8
- end
9
-
10
- class ReadersController < InheritedResources::Base
11
- actions :all, :except => [ :edit, :update ]
8
+ end
9
+
10
+ class ReadersController < InheritedResources::Base
11
+ actions :all, :except => [ :edit, :update ]
12
12
  end
13
13
 
14
14
  class FoldersController < InheritedResources::Base
15
- end
16
-
17
- class ActionsTest < Test::Unit::TestCase
18
- def test_actions_are_undefined_when_only_option_is_given
19
- action_methods = BooksController.send(:action_methods)
20
- assert_equal 2, action_methods.size
21
-
22
- ['index', 'show'].each do |action|
23
- assert action_methods.include? action
24
- end
25
- end
26
-
27
- def test_actions_are_undefined_when_except_option_is_given
28
- action_methods = ReadersController.send(:action_methods)
29
- assert_equal 5, action_methods.size
30
-
31
- ['index', 'new', 'show', 'create', 'destroy'].each do |action|
32
- assert action_methods.include? action
33
- end
15
+ end
16
+
17
+ class ActionsTest < ActiveSupport::TestCase
18
+ def test_actions_are_undefined_when_only_option_is_given
19
+ action_methods = BooksController.send(:action_methods)
20
+ assert_equal 2, action_methods.size
21
+
22
+ ['index', 'show'].each do |action|
23
+ assert action_methods.include? action
24
+ end
25
+ end
26
+
27
+ def test_actions_are_undefined_when_except_option_is_given
28
+ action_methods = ReadersController.send(:action_methods)
29
+ assert_equal 5, action_methods.size
30
+
31
+ ['index', 'new', 'show', 'create', 'destroy'].each do |action|
32
+ assert action_methods.include? action
33
+ end
34
34
  end
35
35
  end
36
36
 
37
- class DefaultsTest < Test::Unit::TestCase
38
-
39
37
  def test_resource_class_is_set_to_nil_when_resource_model_cannot_be_found
38
+ class DefaultsTest < ActiveSupport::TestCase
39
+ def test_resource_class_is_set_to_nil_when_resource_model_cannot_be_found
40
40
  assert_nil ReadersController.send(:resource_class)
41
41
  end
42
42
 
@@ -15,7 +15,7 @@ class CitiesController < InheritedResources::Base
15
15
  end
16
16
 
17
17
  # Create a TestHelper module with some helpers
18
- class NestedBelongsToTest < Test::Unit::TestCase
18
+ class NestedBelongsToTest < TEST_CLASS
19
19
 
20
20
  def setup
21
21
  @controller = CitiesController.new
@@ -1,282 +1,282 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
3
  class Factory; end
4
- class Company; end
5
-
6
- class Employee
7
- def self.human_name; 'Employee'; end
8
- end
9
-
10
- class EmployeesController < InheritedResources::Base
11
- belongs_to :factory, :company, :polymorphic => true
12
- end
13
-
14
- # Create a TestHelper module with some helpers
15
- module EmployeeTestHelper
16
- def setup
17
- @controller = EmployeesController.new
18
- @controller.request = @request = ActionController::TestRequest.new
19
- @controller.response = @response = ActionController::TestResponse.new
20
- end
21
-
22
- protected
23
- def mock_factory(stubs={})
24
- @mock_factory ||= mock(stubs)
25
- end
26
-
27
- def mock_employee(stubs={})
28
- @mock_employee ||= mock(stubs)
29
- end
30
- end
31
-
32
- class IndexActionPolymorphicTest < Test::Unit::TestCase
33
- include EmployeeTestHelper
34
-
35
- def test_expose_all_employees_as_instance_variable
36
- Factory.expects(:find).with('37').returns(mock_factory)
37
- mock_factory.expects(:employees).returns(Employee)
38
- Employee.expects(:find).with(:all).returns([mock_employee])
39
- get :index, :factory_id => '37'
40
- assert_equal mock_factory, assigns(:factory)
41
- assert_equal [mock_employee], assigns(:employees)
42
- end
43
-
44
- def test_controller_should_render_index
45
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
46
- Employee.stubs(:find).returns([mock_employee])
47
- get :index, :factory_id => '37'
48
- assert_response :success
49
- assert_equal 'Index HTML', @response.body.strip
50
- end
51
-
52
- def test_render_all_employees_as_xml_when_mime_type_is_xml
53
- @request.accept = 'application/xml'
54
- Factory.expects(:find).with('37').returns(mock_factory)
55
- mock_factory.expects(:employees).returns(Employee)
56
- Employee.expects(:find).with(:all).returns(mock_employee)
57
- mock_employee.expects(:to_xml).returns('Generated XML')
58
- get :index, :factory_id => '37'
59
- assert_response :success
60
- assert_equal 'Generated XML', @response.body
61
- end
62
- end
63
-
64
- class ShowActionPolymorphicTest < Test::Unit::TestCase
65
- include EmployeeTestHelper
66
-
67
- def test_expose_the_resquested_employee
68
- Factory.expects(:find).with('37').returns(mock_factory)
69
- mock_factory.expects(:employees).returns(Employee)
70
- Employee.expects(:find).with('42').returns(mock_employee)
71
- get :show, :id => '42', :factory_id => '37'
72
- assert_equal mock_factory, assigns(:factory)
73
- assert_equal mock_employee, assigns(:employee)
74
- end
75
-
76
- def test_controller_should_render_show
77
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
78
- Employee.stubs(:find).returns(mock_employee)
79
- get :show, :factory_id => '37'
80
- assert_response :success
81
- assert_equal 'Show HTML', @response.body.strip
82
- end
83
-
84
- def test_render_exposed_employee_as_xml_when_mime_type_is_xml
85
- @request.accept = 'application/xml'
86
- Factory.expects(:find).with('37').returns(mock_factory)
87
- mock_factory.expects(:employees).returns(Employee)
88
- Employee.expects(:find).with('42').returns(mock_employee)
89
- mock_employee.expects(:to_xml).returns("Generated XML")
90
- get :show, :id => '42', :factory_id => '37'
91
- assert_response :success
92
- assert_equal 'Generated XML', @response.body
93
- end
94
- end
95
-
96
- class NewActionPolymorphicTest < Test::Unit::TestCase
97
- include EmployeeTestHelper
98
-
99
- def test_expose_a_new_employee
100
- Factory.expects(:find).with('37').returns(mock_factory)
101
- mock_factory.expects(:employees).returns(Employee)
102
- Employee.expects(:build).returns(mock_employee)
103
- get :new, :factory_id => '37'
104
- assert_equal mock_factory, assigns(:factory)
105
- assert_equal mock_employee, assigns(:employee)
106
- end
107
-
108
- def test_controller_should_render_new
109
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
110
- Employee.stubs(:build).returns(mock_employee)
111
- get :new, :factory_id => '37'
112
- assert_response :success
113
- assert_equal 'New HTML', @response.body.strip
114
- end
115
-
116
- def test_render_exposed_a_new_employee_as_xml_when_mime_type_is_xml
117
- @request.accept = 'application/xml'
118
- Factory.expects(:find).with('37').returns(mock_factory)
119
- mock_factory.expects(:employees).returns(Employee)
120
- Employee.expects(:build).returns(mock_employee)
121
- mock_employee.expects(:to_xml).returns("Generated XML")
122
- get :new, :factory_id => '37'
123
- assert_equal 'Generated XML', @response.body
124
- assert_response :success
125
- end
126
- end
127
-
128
- class EditActionPolymorphicTest < Test::Unit::TestCase
129
- include EmployeeTestHelper
130
-
131
- def test_expose_the_resquested_employee
132
- Factory.expects(:find).with('37').returns(mock_factory)
133
- mock_factory.expects(:employees).returns(Employee)
134
- Employee.expects(:find).with('42').returns(mock_employee)
135
- get :edit, :id => '42', :factory_id => '37'
136
- assert_equal mock_factory, assigns(:factory)
137
- assert_equal mock_employee, assigns(:employee)
138
- assert_response :success
139
- end
140
-
141
- def test_controller_should_render_edit
142
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
143
- Employee.stubs(:find).returns(mock_employee)
144
- get :edit, :factory_id => '37'
145
- assert_response :success
146
- assert_equal 'Edit HTML', @response.body.strip
147
- end
148
- end
149
-
150
- class CreateActionPolymorphicTest < Test::Unit::TestCase
151
- include EmployeeTestHelper
152
-
153
- def test_expose_a_newly_create_employee_when_saved_with_success
154
- Factory.expects(:find).with('37').returns(mock_factory)
155
- mock_factory.expects(:employees).returns(Employee)
156
- Employee.expects(:build).with({'these' => 'params'}).returns(mock_employee(:save => true))
157
- post :create, :factory_id => '37', :employee => {:these => 'params'}
158
- assert_equal mock_factory, assigns(:factory)
159
- assert_equal mock_employee, assigns(:employee)
160
- end
161
-
162
- def test_redirect_to_the_created_employee
163
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
164
- Employee.stubs(:build).returns(mock_employee(:save => true))
165
- @controller.expects(:resource_url).returns('http://test.host/').times(2)
166
- post :create, :factory_id => '37'
167
- assert_redirected_to 'http://test.host/'
168
- end
169
-
170
- def test_show_flash_message_when_success
171
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
172
- Employee.stubs(:build).returns(mock_employee(:save => true))
173
- post :create, :factory_id => '37'
174
- assert_equal flash[:notice], 'Employee was successfully created.'
175
- end
176
-
177
- def test_render_new_template_when_employee_cannot_be_saved
178
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
179
- Employee.stubs(:build).returns(mock_employee(:save => false, :errors => []))
180
- post :create, :factory_id => '37'
181
- assert_response :success
182
- assert_template 'new'
183
- end
184
-
185
- def test_dont_show_flash_message_when_employee_cannot_be_saved
186
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
187
- Employee.stubs(:build).returns(mock_employee(:save => false, :errors => []))
188
- post :create, :factory_id => '37'
189
- assert flash.empty?
190
- end
191
- end
192
-
193
- class UpdateActionPolymorphicTest < Test::Unit::TestCase
194
- include EmployeeTestHelper
195
-
196
- def test_update_the_requested_object
197
- Factory.expects(:find).with('37').returns(mock_factory)
198
- mock_factory.expects(:employees).returns(Employee)
199
- Employee.expects(:find).with('42').returns(mock_employee)
200
- mock_employee.expects(:update_attributes).with({'these' => 'params'}).returns(true)
201
- put :update, :id => '42', :factory_id => '37', :employee => {:these => 'params'}
202
- assert_equal mock_factory, assigns(:factory)
203
- assert_equal mock_employee, assigns(:employee)
204
- end
205
-
206
- def test_redirect_to_the_created_employee
207
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
208
- Employee.stubs(:find).returns(mock_employee(:update_attributes => true))
209
- @controller.expects(:resource_url).returns('http://test.host/')
210
- put :update, :factory_id => '37'
211
- assert_redirected_to 'http://test.host/'
212
- end
213
-
214
- def test_show_flash_message_when_success
215
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
216
- Employee.stubs(:find).returns(mock_employee(:update_attributes => true))
217
- put :update, :factory_id => '37'
218
- assert_equal flash[:notice], 'Employee was successfully updated.'
219
- end
220
-
221
- def test_render_edit_template_when_employee_cannot_be_saved
222
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
223
- Employee.stubs(:find).returns(mock_employee(:update_attributes => false, :errors => []))
224
- put :update, :factory_id => '37'
225
- assert_response :success
226
- assert_template 'edit'
227
- end
228
-
229
- def test_dont_show_flash_message_when_employee_cannot_be_saved
230
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
231
- Employee.stubs(:find).returns(mock_employee(:update_attributes => false, :errors => []))
232
- put :update, :factory_id => '37'
233
- assert flash.empty?
234
- end
235
- end
236
-
237
- class DestroyActionPolymorphicTest < Test::Unit::TestCase
238
- include EmployeeTestHelper
239
-
240
- def test_the_resquested_employee_is_destroyed
241
- Factory.expects(:find).with('37').returns(mock_factory)
242
- mock_factory.expects(:employees).returns(Employee)
243
- Employee.expects(:find).with('42').returns(mock_employee)
244
- mock_employee.expects(:destroy)
245
- delete :destroy, :id => '42', :factory_id => '37'
246
- assert_equal mock_factory, assigns(:factory)
247
- assert_equal mock_employee, assigns(:employee)
248
- end
249
-
250
- def test_show_flash_message
251
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
252
- Employee.stubs(:find).returns(mock_employee(:destroy => true))
253
- delete :destroy, :factory_id => '37'
254
- assert_equal flash[:notice], 'Employee was successfully destroyed.'
255
- end
256
-
257
- def test_redirects_to_employees_list
258
- Factory.stubs(:find).returns(mock_factory(:employees => Employee))
259
- Employee.stubs(:find).returns(mock_employee(:destroy => true))
260
- @controller.expects(:collection_url).returns('http://test.host/')
261
- delete :destroy, :factory_id => '37'
262
- assert_redirected_to 'http://test.host/'
263
- end
264
- end
265
-
266
- include EmployeeTestHelper
267
-
4
+ class Company; end
5
+
6
+ class Employee
7
+ def self.human_name; 'Employee'; end
8
+ end
9
+
10
+ class EmployeesController < InheritedResources::Base
11
+ belongs_to :factory, :company, :polymorphic => true
12
+ end
13
+
14
+ # Create a TestHelper module with some helpers
15
+ module EmployeeTestHelper
16
+ def setup
17
+ @controller = EmployeesController.new
18
+ @controller.request = @request = ActionController::TestRequest.new
19
+ @controller.response = @response = ActionController::TestResponse.new
20
+ end
21
+
22
+ protected
23
+ def mock_factory(stubs={})
24
+ @mock_factory ||= mock(stubs)
25
+ end
26
+
27
+ def mock_employee(stubs={})
28
+ @mock_employee ||= mock(stubs)
29
+ end
30
+ end
31
+
32
+ class IndexActionPolymorphicTest < TEST_CLASS
33
+ include EmployeeTestHelper
34
+
35
+ def test_expose_all_employees_as_instance_variable
36
+ Factory.expects(:find).with('37').returns(mock_factory)
37
+ mock_factory.expects(:employees).returns(Employee)
38
+ Employee.expects(:find).with(:all).returns([mock_employee])
39
+ get :index, :factory_id => '37'
40
+ assert_equal mock_factory, assigns(:factory)
41
+ assert_equal [mock_employee], assigns(:employees)
42
+ end
43
+
44
+ def test_controller_should_render_index
45
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
46
+ Employee.stubs(:find).returns([mock_employee])
47
+ get :index, :factory_id => '37'
48
+ assert_response :success
49
+ assert_equal 'Index HTML', @response.body.strip
50
+ end
51
+
52
+ def test_render_all_employees_as_xml_when_mime_type_is_xml
53
+ @request.accept = 'application/xml'
54
+ Factory.expects(:find).with('37').returns(mock_factory)
55
+ mock_factory.expects(:employees).returns(Employee)
56
+ Employee.expects(:find).with(:all).returns(mock_employee)
57
+ mock_employee.expects(:to_xml).returns('Generated XML')
58
+ get :index, :factory_id => '37'
59
+ assert_response :success
60
+ assert_equal 'Generated XML', @response.body
61
+ end
62
+ end
63
+
64
+ class ShowActionPolymorphicTest < TEST_CLASS
65
+ include EmployeeTestHelper
66
+
67
+ def test_expose_the_resquested_employee
68
+ Factory.expects(:find).with('37').returns(mock_factory)
69
+ mock_factory.expects(:employees).returns(Employee)
70
+ Employee.expects(:find).with('42').returns(mock_employee)
71
+ get :show, :id => '42', :factory_id => '37'
72
+ assert_equal mock_factory, assigns(:factory)
73
+ assert_equal mock_employee, assigns(:employee)
74
+ end
75
+
76
+ def test_controller_should_render_show
77
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
78
+ Employee.stubs(:find).returns(mock_employee)
79
+ get :show, :factory_id => '37'
80
+ assert_response :success
81
+ assert_equal 'Show HTML', @response.body.strip
82
+ end
83
+
84
+ def test_render_exposed_employee_as_xml_when_mime_type_is_xml
85
+ @request.accept = 'application/xml'
86
+ Factory.expects(:find).with('37').returns(mock_factory)
87
+ mock_factory.expects(:employees).returns(Employee)
88
+ Employee.expects(:find).with('42').returns(mock_employee)
89
+ mock_employee.expects(:to_xml).returns("Generated XML")
90
+ get :show, :id => '42', :factory_id => '37'
91
+ assert_response :success
92
+ assert_equal 'Generated XML', @response.body
93
+ end
94
+ end
95
+
96
+ class NewActionPolymorphicTest < TEST_CLASS
97
+ include EmployeeTestHelper
98
+
99
+ def test_expose_a_new_employee
100
+ Factory.expects(:find).with('37').returns(mock_factory)
101
+ mock_factory.expects(:employees).returns(Employee)
102
+ Employee.expects(:build).returns(mock_employee)
103
+ get :new, :factory_id => '37'
104
+ assert_equal mock_factory, assigns(:factory)
105
+ assert_equal mock_employee, assigns(:employee)
106
+ end
107
+
108
+ def test_controller_should_render_new
109
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
110
+ Employee.stubs(:build).returns(mock_employee)
111
+ get :new, :factory_id => '37'
112
+ assert_response :success
113
+ assert_equal 'New HTML', @response.body.strip
114
+ end
115
+
116
+ def test_render_exposed_a_new_employee_as_xml_when_mime_type_is_xml
117
+ @request.accept = 'application/xml'
118
+ Factory.expects(:find).with('37').returns(mock_factory)
119
+ mock_factory.expects(:employees).returns(Employee)
120
+ Employee.expects(:build).returns(mock_employee)
121
+ mock_employee.expects(:to_xml).returns("Generated XML")
122
+ get :new, :factory_id => '37'
123
+ assert_equal 'Generated XML', @response.body
124
+ assert_response :success
125
+ end
126
+ end
127
+
128
+ class EditActionPolymorphicTest < TEST_CLASS
129
+ include EmployeeTestHelper
130
+
131
+ def test_expose_the_resquested_employee
132
+ Factory.expects(:find).with('37').returns(mock_factory)
133
+ mock_factory.expects(:employees).returns(Employee)
134
+ Employee.expects(:find).with('42').returns(mock_employee)
135
+ get :edit, :id => '42', :factory_id => '37'
136
+ assert_equal mock_factory, assigns(:factory)
137
+ assert_equal mock_employee, assigns(:employee)
138
+ assert_response :success
139
+ end
140
+
141
+ def test_controller_should_render_edit
142
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
143
+ Employee.stubs(:find).returns(mock_employee)
144
+ get :edit, :factory_id => '37'
145
+ assert_response :success
146
+ assert_equal 'Edit HTML', @response.body.strip
147
+ end
148
+ end
149
+
150
+ class CreateActionPolymorphicTest < TEST_CLASS
151
+ include EmployeeTestHelper
152
+
153
+ def test_expose_a_newly_create_employee_when_saved_with_success
154
+ Factory.expects(:find).with('37').returns(mock_factory)
155
+ mock_factory.expects(:employees).returns(Employee)
156
+ Employee.expects(:build).with({'these' => 'params'}).returns(mock_employee(:save => true))
157
+ post :create, :factory_id => '37', :employee => {:these => 'params'}
158
+ assert_equal mock_factory, assigns(:factory)
159
+ assert_equal mock_employee, assigns(:employee)
160
+ end
161
+
162
+ def test_redirect_to_the_created_employee
163
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
164
+ Employee.stubs(:build).returns(mock_employee(:save => true))
165
+ @controller.expects(:resource_url).returns('http://test.host/').times(2)
166
+ post :create, :factory_id => '37'
167
+ assert_redirected_to 'http://test.host/'
168
+ end
169
+
170
+ def test_show_flash_message_when_success
171
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
172
+ Employee.stubs(:build).returns(mock_employee(:save => true))
173
+ post :create, :factory_id => '37'
174
+ assert_equal flash[:notice], 'Employee was successfully created.'
175
+ end
176
+
177
+ def test_render_new_template_when_employee_cannot_be_saved
178
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
179
+ Employee.stubs(:build).returns(mock_employee(:save => false, :errors => []))
180
+ post :create, :factory_id => '37'
181
+ assert_response :success
182
+ assert_template :new
183
+ end
184
+
185
+ def test_dont_show_flash_message_when_employee_cannot_be_saved
186
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
187
+ Employee.stubs(:build).returns(mock_employee(:save => false, :errors => []))
188
+ post :create, :factory_id => '37'
189
+ assert flash.empty?
190
+ end
191
+ end
192
+
193
+ class UpdateActionPolymorphicTest < TEST_CLASS
194
+ include EmployeeTestHelper
195
+
196
+ def test_update_the_requested_object
197
+ Factory.expects(:find).with('37').returns(mock_factory)
198
+ mock_factory.expects(:employees).returns(Employee)
199
+ Employee.expects(:find).with('42').returns(mock_employee)
200
+ mock_employee.expects(:update_attributes).with({'these' => 'params'}).returns(true)
201
+ put :update, :id => '42', :factory_id => '37', :employee => {:these => 'params'}
202
+ assert_equal mock_factory, assigns(:factory)
203
+ assert_equal mock_employee, assigns(:employee)
204
+ end
205
+
206
+ def test_redirect_to_the_created_employee
207
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
208
+ Employee.stubs(:find).returns(mock_employee(:update_attributes => true))
209
+ @controller.expects(:resource_url).returns('http://test.host/')
210
+ put :update, :factory_id => '37'
211
+ assert_redirected_to 'http://test.host/'
212
+ end
213
+
214
+ def test_show_flash_message_when_success
215
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
216
+ Employee.stubs(:find).returns(mock_employee(:update_attributes => true))
217
+ put :update, :factory_id => '37'
218
+ assert_equal flash[:notice], 'Employee was successfully updated.'
219
+ end
220
+
221
+ def test_render_edit_template_when_employee_cannot_be_saved
222
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
223
+ Employee.stubs(:find).returns(mock_employee(:update_attributes => false, :errors => []))
224
+ put :update, :factory_id => '37'
225
+ assert_response :success
226
+ assert_template :edit
227
+ end
228
+
229
+ def test_dont_show_flash_message_when_employee_cannot_be_saved
230
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
231
+ Employee.stubs(:find).returns(mock_employee(:update_attributes => false, :errors => []))
232
+ put :update, :factory_id => '37'
233
+ assert flash.empty?
234
+ end
235
+ end
236
+
237
+ class DestroyActionPolymorphicTest < TEST_CLASS
238
+ include EmployeeTestHelper
239
+
240
+ def test_the_resquested_employee_is_destroyed
241
+ Factory.expects(:find).with('37').returns(mock_factory)
242
+ mock_factory.expects(:employees).returns(Employee)
243
+ Employee.expects(:find).with('42').returns(mock_employee)
244
+ mock_employee.expects(:destroy)
245
+ delete :destroy, :id => '42', :factory_id => '37'
246
+ assert_equal mock_factory, assigns(:factory)
247
+ assert_equal mock_employee, assigns(:employee)
248
+ end
249
+
250
+ def test_show_flash_message
251
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
252
+ Employee.stubs(:find).returns(mock_employee(:destroy => true))
253
+ delete :destroy, :factory_id => '37'
254
+ assert_equal flash[:notice], 'Employee was successfully destroyed.'
255
+ end
256
+
257
+ def test_redirects_to_employees_list
258
+ Factory.stubs(:find).returns(mock_factory(:employees => Employee))
259
+ Employee.stubs(:find).returns(mock_employee(:destroy => true))
260
+ @controller.expects(:collection_url).returns('http://test.host/')
261
+ delete :destroy, :factory_id => '37'
262
+ assert_redirected_to 'http://test.host/'
263
+ end
264
+ end
265
+ class PolymorphicHelpersTest < TEST_CLASS
266
+ include EmployeeTestHelper
267
+
268
268
  def test_polymorphic_helpers
269
- new_factory = Factory.new
270
- Factory.expects(:find).with('37').returns(new_factory)
271
- new_factory.expects(:employees).returns(Employee)
272
- Employee.expects(:find).with(:all).returns([mock_employee])
269
+ new_factory = Factory.new
270
+ Factory.expects(:find).with('37').returns(new_factory)
271
+ new_factory.expects(:employees).returns(Employee)
272
+ Employee.expects(:find).with(:all).returns([mock_employee])
273
273
  get :index, :factory_id => '37'
274
-
274
+
275
275
  assert @controller.send(:parent?)
276
276
  assert_equal :factory, assigns(:parent_type)
277
277
  assert_equal :factory, @controller.send(:parent_type)
278
278
  assert_equal Factory, @controller.send(:parent_class)
279
279
  assert_equal new_factory, assigns(:factory)
280
- assert_equal new_factory, @controller.send(:parent_instance)
280
+ assert_equal new_factory, @controller.send(:parent_instance)
281
281
  end
282
282
  end
@@ -51,7 +51,7 @@ end
51
51
  class SuperProjectsController < ProjectsController
52
52
  end
53
53
 
54
- class RespondToUnitTest < Test::Unit::TestCase
54
+ class RespondToUnitTest < TEST_CLASS
55
55
  def setup(class_controller = ProjectsController)
56
56
  @controller = class_controller.new
57
57
  @controller.request = @request = ActionController::TestRequest.new
@@ -172,7 +172,7 @@ class RespondToUnitTest < Test::Unit::TestCase
172
172
  end
173
173
  end
174
174
 
175
- class RespondToFunctionalTest < Test::Unit::TestCase
175
+ class RespondToFunctionalTest < TEST_CLASS
176
176
  def setup
177
177
  @controller = ProjectsController.new
178
178
  @controller.request = @request = ActionController::TestRequest.new
@@ -32,7 +32,7 @@ module ManagerTestHelper
32
32
  end
33
33
  end
34
34
 
35
- class ShowActionSingletonTest < Test::Unit::TestCase
35
+ class ShowActionSingletonTest < TEST_CLASS
36
36
  include ManagerTestHelper
37
37
 
38
38
  def test_expose_the_resquested_manager
@@ -61,7 +61,7 @@ class ShowActionSingletonTest < Test::Unit::TestCase
61
61
  end
62
62
  end
63
63
 
64
- class NewActionSingletonTest < Test::Unit::TestCase
64
+ class NewActionSingletonTest < TEST_CLASS
65
65
  include ManagerTestHelper
66
66
 
67
67
  def test_expose_a_new_manager
@@ -91,7 +91,7 @@ class NewActionSingletonTest < Test::Unit::TestCase
91
91
  end
92
92
  end
93
93
 
94
- class EditActionSingletonTest < Test::Unit::TestCase
94
+ class EditActionSingletonTest < TEST_CLASS
95
95
  include ManagerTestHelper
96
96
 
97
97
  def test_expose_the_resquested_manager
@@ -112,7 +112,7 @@ class EditActionSingletonTest < Test::Unit::TestCase
112
112
  end
113
113
  end
114
114
 
115
- class CreateActionSingletonTest < Test::Unit::TestCase
115
+ class CreateActionSingletonTest < TEST_CLASS
116
116
  include ManagerTestHelper
117
117
 
118
118
  def test_expose_a_newly_create_manager_when_saved_with_success
@@ -143,7 +143,7 @@ class CreateActionSingletonTest < Test::Unit::TestCase
143
143
  mock_store.stubs(:build_manager).returns(mock_manager(:save => false, :errors => []))
144
144
  post :create
145
145
  assert_response :success
146
- assert_template 'new'
146
+ assert_template :new
147
147
  end
148
148
 
149
149
  def test_dont_show_flash_message_when_manager_cannot_be_saved
@@ -154,7 +154,7 @@ class CreateActionSingletonTest < Test::Unit::TestCase
154
154
  end
155
155
  end
156
156
 
157
- class UpdateActionSingletonTest < Test::Unit::TestCase
157
+ class UpdateActionSingletonTest < TEST_CLASS
158
158
  include ManagerTestHelper
159
159
 
160
160
  def test_update_the_requested_object
@@ -185,7 +185,7 @@ class UpdateActionSingletonTest < Test::Unit::TestCase
185
185
  mock_manager.stubs(:update_attributes).returns(false)
186
186
  put :update
187
187
  assert_response :success
188
- assert_template 'edit'
188
+ assert_template :edit
189
189
  end
190
190
 
191
191
  def test_dont_show_flash_message_when_manager_cannot_be_saved
@@ -196,7 +196,7 @@ class UpdateActionSingletonTest < Test::Unit::TestCase
196
196
  end
197
197
  end
198
198
 
199
- class DestroyActionSingletonTest < Test::Unit::TestCase
199
+ class DestroyActionSingletonTest < TEST_CLASS
200
200
  include ManagerTestHelper
201
201
 
202
202
  def test_the_resquested_manager_is_destroyed
data/test/test_helper.rb CHANGED
@@ -4,11 +4,18 @@ require 'mocha'
4
4
 
5
5
  ENV["RAILS_ENV"] = "test"
6
6
 
7
+ # Used to tests Rails 2.2.2 version
8
+ # gem 'activesupport', '2.2.2'
9
+ # gem 'actionpack', '2.2.2'
10
+ # TEST_CLASS = Test::Unit::TestCase
11
+
7
12
  require 'active_support'
8
13
  require 'action_controller'
9
14
  require 'action_controller/test_case'
10
15
  require 'action_controller/test_process'
11
16
 
17
+ TEST_CLASS = ActionController::TestCase
18
+
12
19
  I18n.load_path << File.join(File.dirname(__FILE__), 'fixtures', 'en.yml')
13
20
  I18n.reload!
14
21
 
@@ -45,7 +45,7 @@ class CentersController < InheritedResources::Base
45
45
  end
46
46
 
47
47
  # Create a TestHelper module with some helpers
48
- class UrlHelpersTest < Test::Unit::TestCase
48
+ class UrlHelpersTest < ActiveSupport::TestCase
49
49
 
50
50
  def test_url_helpers_on_simple_inherited_resource
51
51
  controller = HousesController.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: josevalim-inherited_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jos\xC3\xA9 Valim"