josevalim-inherited_resources 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/CHANGELOG +4 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +362 -0
  4. data/Rakefile +19 -0
  5. data/init.rb +1 -0
  6. data/lib/inherited_resources.rb +4 -0
  7. data/lib/inherited_resources/base.rb +272 -0
  8. data/lib/inherited_resources/base_helpers.rb +199 -0
  9. data/lib/inherited_resources/belongs_to.rb +227 -0
  10. data/lib/inherited_resources/belongs_to_helpers.rb +89 -0
  11. data/lib/inherited_resources/class_methods.rb +155 -0
  12. data/lib/inherited_resources/polymorphic_helpers.rb +19 -0
  13. data/lib/inherited_resources/respond_to.rb +324 -0
  14. data/lib/inherited_resources/singleton_helpers.rb +53 -0
  15. data/lib/inherited_resources/url_helpers.rb +147 -0
  16. data/test/aliases_test.rb +71 -0
  17. data/test/base_helpers_test.rb +130 -0
  18. data/test/base_test.rb +219 -0
  19. data/test/belongs_to_base_test.rb +268 -0
  20. data/test/belongs_to_test.rb +109 -0
  21. data/test/class_methods_test.rb +73 -0
  22. data/test/fixtures/en.yml +9 -0
  23. data/test/nested_belongs_to_test.rb +138 -0
  24. data/test/polymorphic_base_test.rb +282 -0
  25. data/test/respond_to_test.rb +282 -0
  26. data/test/singleton_base_test.rb +226 -0
  27. data/test/test_helper.rb +37 -0
  28. data/test/url_helpers_test.rb +284 -0
  29. data/test/views/cities/edit.html.erb +1 -0
  30. data/test/views/cities/index.html.erb +1 -0
  31. data/test/views/cities/new.html.erb +1 -0
  32. data/test/views/cities/show.html.erb +1 -0
  33. data/test/views/comments/edit.html.erb +1 -0
  34. data/test/views/comments/index.html.erb +1 -0
  35. data/test/views/comments/new.html.erb +1 -0
  36. data/test/views/comments/show.html.erb +1 -0
  37. data/test/views/employees/edit.html.erb +1 -0
  38. data/test/views/employees/index.html.erb +1 -0
  39. data/test/views/employees/new.html.erb +1 -0
  40. data/test/views/employees/show.html.erb +1 -0
  41. data/test/views/managers/edit.html.erb +1 -0
  42. data/test/views/managers/new.html.erb +1 -0
  43. data/test/views/managers/show.html.erb +1 -0
  44. data/test/views/pets/edit.html.erb +1 -0
  45. data/test/views/professors/edit.html.erb +1 -0
  46. data/test/views/professors/index.html.erb +1 -0
  47. data/test/views/professors/new.html.erb +1 -0
  48. data/test/views/professors/show.html.erb +1 -0
  49. data/test/views/projects/index.html.erb +1 -0
  50. data/test/views/projects/respond_to_with_resource.html.erb +1 -0
  51. data/test/views/students/edit.html.erb +1 -0
  52. data/test/views/students/new.html.erb +1 -0
  53. data/test/views/users/edit.html.erb +1 -0
  54. data/test/views/users/index.html.erb +1 -0
  55. data/test/views/users/new.html.erb +1 -0
  56. data/test/views/users/show.html.erb +1 -0
  57. metadata +108 -0
@@ -0,0 +1,9 @@
1
+ en:
2
+ flash:
3
+ addresses:
4
+ create:
5
+ notice: "You created a new address close to <b>{{reference}}</b>."
6
+ update:
7
+ notice: "Nice! {{resource}} was updated with success!"
8
+ error: "Oh no! We could not update your address!"
9
+
@@ -0,0 +1,138 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Country
4
+ end
5
+
6
+ class State
7
+ end
8
+
9
+ class City
10
+ def self.human_name; 'City'; end
11
+ end
12
+
13
+ class CitiesController < InheritedResources::Base
14
+ belongs_to :country, :state
15
+ end
16
+
17
+ # Create a TestHelper module with some helpers
18
+ class NestedBelongsToTest < Test::Unit::TestCase
19
+
20
+ def setup
21
+ @controller = CitiesController.new
22
+ @controller.request = @request = ActionController::TestRequest.new
23
+ @controller.response = @response = ActionController::TestResponse.new
24
+ end
25
+
26
+ def test_assigns_country_and_state_and_city_on_create
27
+ Country.expects(:find).with('13').returns(mock_country)
28
+ mock_country.expects(:states).returns(State)
29
+ State.expects(:find).with('37').returns(mock_state)
30
+ mock_state.expects(:cities).returns(City)
31
+ City.expects(:find).with(:all).returns([mock_city])
32
+
33
+ get :index, :state_id => '37', :country_id => '13'
34
+
35
+ assert_equal mock_country, assigns(:country)
36
+ assert_equal mock_state, assigns(:state)
37
+ assert_equal [mock_city], assigns(:cities)
38
+ end
39
+
40
+ def test_assigns_country_and_state_and_city_on_show
41
+ Country.expects(:find).with('13').returns(mock_country)
42
+ mock_country.expects(:states).returns(State)
43
+ State.expects(:find).with('37').returns(mock_state)
44
+ mock_state.expects(:cities).returns(City)
45
+ City.expects(:find).with('42').returns(mock_city)
46
+
47
+ get :show, :id => '42', :state_id => '37', :country_id => '13'
48
+
49
+ assert_equal mock_country, assigns(:country)
50
+ assert_equal mock_state, assigns(:state)
51
+ assert_equal mock_city, assigns(:city)
52
+ end
53
+
54
+ def test_assigns_country_and_state_and_city_on_new
55
+ Country.expects(:find).with('13').returns(mock_country)
56
+ mock_country.expects(:states).returns(State)
57
+ State.expects(:find).with('37').returns(mock_state)
58
+ mock_state.expects(:cities).returns(City)
59
+ City.expects(:build).returns(mock_city)
60
+
61
+ get :new, :state_id => '37', :country_id => '13'
62
+
63
+ assert_equal mock_country, assigns(:country)
64
+ assert_equal mock_state, assigns(:state)
65
+ assert_equal mock_city, assigns(:city)
66
+ end
67
+
68
+ def test_assigns_country_and_state_and_city_on_edit
69
+ Country.expects(:find).with('13').returns(mock_country)
70
+ mock_country.expects(:states).returns(State)
71
+ State.expects(:find).with('37').returns(mock_state)
72
+ mock_state.expects(:cities).returns(City)
73
+ City.expects(:find).with('42').returns(mock_city)
74
+
75
+ get :edit, :id => '42', :state_id => '37', :country_id => '13'
76
+
77
+ assert_equal mock_country, assigns(:country)
78
+ assert_equal mock_state, assigns(:state)
79
+ assert_equal mock_city, assigns(:city)
80
+ end
81
+
82
+ def test_assigns_country_and_state_and_city_on_create
83
+ Country.expects(:find).with('13').returns(mock_country)
84
+ mock_country.expects(:states).returns(State)
85
+ State.expects(:find).with('37').returns(mock_state)
86
+ mock_state.expects(:cities).returns(City)
87
+ City.expects(:build).with({'these' => 'params'}).returns(mock_city(:save => true))
88
+
89
+ post :create, :state_id => '37', :country_id => '13', :city => {:these => 'params'}
90
+
91
+ assert_equal mock_country, assigns(:country)
92
+ assert_equal mock_state, assigns(:state)
93
+ assert_equal mock_city, assigns(:city)
94
+ end
95
+
96
+ def test_assigns_country_and_state_and_city_on_update
97
+ Country.expects(:find).with('13').returns(mock_country)
98
+ mock_country.expects(:states).returns(State)
99
+ State.expects(:find).with('37').returns(mock_state)
100
+ mock_state.expects(:cities).returns(City)
101
+ City.expects(:find).with('42').returns(mock_city)
102
+ mock_city.expects(:update_attributes).with({'these' => 'params'}).returns(true)
103
+
104
+ put :update, :id => '42', :state_id => '37', :country_id => '13', :city => {:these => 'params'}
105
+
106
+ assert_equal mock_country, assigns(:country)
107
+ assert_equal mock_state, assigns(:state)
108
+ assert_equal mock_city, assigns(:city)
109
+ end
110
+
111
+ def test_assigns_country_and_state_and_city_on_destroy
112
+ Country.expects(:find).with('13').returns(mock_country)
113
+ mock_country.expects(:states).returns(State)
114
+ State.expects(:find).with('37').returns(mock_state)
115
+ mock_state.expects(:cities).returns(City)
116
+ City.expects(:find).with('42').returns(mock_city)
117
+ mock_city.expects(:destroy)
118
+
119
+ delete :destroy, :id => '42', :state_id => '37', :country_id => '13'
120
+
121
+ assert_equal mock_country, assigns(:country)
122
+ assert_equal mock_state, assigns(:state)
123
+ assert_equal mock_city, assigns(:city)
124
+ end
125
+
126
+ protected
127
+ def mock_country(stubs={})
128
+ @mock_country ||= mock(stubs)
129
+ end
130
+
131
+ def mock_state(stubs={})
132
+ @mock_state ||= mock(stubs)
133
+ end
134
+
135
+ def mock_city(stubs={})
136
+ @mock_city ||= mock(stubs)
137
+ end
138
+ end
@@ -0,0 +1,282 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
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
+
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])
273
+ get :index, :factory_id => '37'
274
+
275
+ assert @controller.send(:parent?)
276
+ assert_equal :factory, assigns(:parent_type)
277
+ assert_equal :factory, @controller.send(:parent_type)
278
+ assert_equal Factory, @controller.send(:parent_class)
279
+ assert_equal new_factory, assigns(:factory)
280
+ assert_equal new_factory, @controller.send(:parent_instance)
281
+ end
282
+ end
@@ -0,0 +1,282 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Project
4
+ def to_html
5
+ 'Generated HTML'
6
+ end
7
+
8
+ def to_xml
9
+ 'Generated XML'
10
+ end
11
+
12
+ [:to_json, :to_rss, :to_rjs].each do |method|
13
+ undef_method method if respond_to? method
14
+ end
15
+ end
16
+
17
+ class ProjectsController < ActionController::Base
18
+ # Inherited respond_to definition is:
19
+ # respond_to :html
20
+ # respond_to :xml, :except => :edit
21
+ respond_to :html
22
+ respond_to :rjs => :edit
23
+ respond_to :rss, :only => 'index'
24
+ respond_to :json, :except => :index
25
+
26
+ def index
27
+ respond_with(Project.new)
28
+ end
29
+
30
+ def respond_with_options
31
+ respond_with(Project.new, :to => [:xml, :json], :location => 'http://test.host/')
32
+ end
33
+
34
+ def skip_not_acceptable
35
+ respond_with(Project.new, :skip_not_acceptable => true)
36
+ render :text => 'Will not raise double render error.'
37
+ end
38
+
39
+ def respond_to_with_resource
40
+ respond_to(:with => Project.new)
41
+ end
42
+
43
+ def respond_to_with_resource_and_blocks
44
+ respond_to(:with => Project.new) do |format|
45
+ format.json { render :text => 'Render JSON' }
46
+ format.rss { render :text => 'Render RSS' }
47
+ end
48
+ end
49
+ end
50
+
51
+ class SuperProjectsController < ProjectsController
52
+ end
53
+
54
+ class RespondToUnitTest < Test::Unit::TestCase
55
+ def setup(class_controller = ProjectsController)
56
+ @controller = class_controller.new
57
+ @controller.request = @request = ActionController::TestRequest.new
58
+ @controller.response = @response = ActionController::TestResponse.new
59
+
60
+ @formats = @controller.formats_for_respond_to
61
+ @responder = ActionController::MimeResponds::Responder.new(@controller)
62
+ end
63
+
64
+ def test_respond_to_class_method_without_options
65
+ assert_nil @formats[:html][:only]
66
+ assert_nil @formats[:html][:except]
67
+ end
68
+
69
+ def test_respond_to_class_method_inheritance
70
+ assert_nil @formats[:xml][:only]
71
+ assert_equal [:edit], @formats[:xml][:except]
72
+ end
73
+
74
+ def test_respond_to_class_method_with_implicit_only
75
+ assert_equal [:edit], @formats[:rjs][:only]
76
+ assert_nil @formats[:rjs][:except]
77
+ end
78
+
79
+ def test_respond_to_class_method_with_explicit_only
80
+ assert_equal [:index], @formats[:rss][:only]
81
+ assert_nil @formats[:rss][:except]
82
+ end
83
+
84
+ def test_respond_to_class_method_with_explicit_except
85
+ assert_nil @formats[:json][:only]
86
+ assert_equal [:index], @formats[:json][:except]
87
+ end
88
+
89
+ def test_action_respond_to_format
90
+ @controller.action_name = 'index'
91
+ assert @responder.action_respond_to_format?('html') # defined
92
+ assert @responder.action_respond_to_format?('xml') # inherited
93
+ assert @responder.action_respond_to_format?('rss') # explicit only
94
+ assert !@responder.action_respond_to_format?('json') # exception
95
+
96
+ @controller.action_name = 'edit'
97
+ assert !@responder.action_respond_to_format?('xml') # inherited
98
+ assert @responder.action_respond_to_format?('rjs') # implicit only
99
+ assert @responder.action_respond_to_format?('json') # exception
100
+ end
101
+
102
+ def test_action_respond_to_format_with_additional_mimes
103
+ assert @responder.action_respond_to_format?('html', [:xml, :html, :json])
104
+ assert !@responder.action_respond_to_format?('html', [:xml, :rss, :json])
105
+
106
+ @controller.action_name = 'index'
107
+ assert @responder.action_respond_to_format?('html', [])
108
+ assert !@responder.action_respond_to_format?('json', [])
109
+ end
110
+
111
+ def test_clear_respond_to
112
+ setup(SuperProjectsController)
113
+
114
+ # Those responses are inherited from ProjectsController
115
+ @controller.action_name = 'index'
116
+ assert @responder.action_respond_to_format?('html') # defined
117
+ assert @responder.action_respond_to_format?('xml') # inherited
118
+ assert @responder.action_respond_to_format?('rss') # explicit only
119
+
120
+ # Let's clear respond_to definitions
121
+ SuperProjectsController.send(:clear_respond_to!)
122
+
123
+ assert !@responder.action_respond_to_format?('html')
124
+ assert !@responder.action_respond_to_format?('xml')
125
+ assert !@responder.action_respond_to_format?('rss')
126
+ end
127
+
128
+ def test_respond_to_block_does_not_respond_to_mime_all
129
+ prepare_responder_to_respond!
130
+
131
+ @responder.respond_to_block
132
+ assert !@performed
133
+ assert !@responder.responded?
134
+
135
+ @responder.respond
136
+ assert @performed
137
+ end
138
+
139
+ def test_respond_to_all_responds_to_mime_all
140
+ prepare_responder_to_respond!
141
+
142
+ @responder.respond_to_all
143
+ assert @performed
144
+ assert @responder.responded?
145
+ end
146
+
147
+ def test_respond_to_all_responds_only_to_all
148
+ prepare_responder_to_respond!('text/html')
149
+
150
+ @responder.respond_to_all
151
+ assert !@performed
152
+ assert !@responder.responded?
153
+ end
154
+
155
+ protected
156
+ def prepare_responder_to_respond!(content_type = '*/*')
157
+ @request.accept = content_type
158
+ @responder = ActionController::MimeResponds::Responder.new(@controller)
159
+ @performed = false
160
+
161
+ # Mock template
162
+ template = mock()
163
+ @response.stubs(:template).returns(template)
164
+ template.stubs(:template_format=).returns(true)
165
+
166
+ respond_to_declaration = proc { |format|
167
+ format.html { @performed = true }
168
+ format.xml { }
169
+ }
170
+
171
+ respond_to_declaration.call(@responder)
172
+ end
173
+ end
174
+
175
+ class RespondToFunctionalTest < Test::Unit::TestCase
176
+ def setup
177
+ @controller = ProjectsController.new
178
+ @controller.request = @request = ActionController::TestRequest.new
179
+ @controller.response = @response = ActionController::TestResponse.new
180
+ end
181
+
182
+ def test_respond_with_layout_rendering
183
+ @request.accept = 'text/html'
184
+ get :index
185
+ assert_equal 'Index HTML', @response.body.strip
186
+ end
187
+
188
+ def test_respond_with_calls_to_format_on_resource
189
+ @request.accept = 'application/xml'
190
+ get :index
191
+ assert_equal 'Generated XML', @response.body.strip
192
+ end
193
+
194
+ def test_respond_with_inherits_format
195
+ @request.accept = 'application/xml'
196
+ get :index
197
+ assert_equal 'Generated XML', @response.body.strip
198
+ end
199
+
200
+ def test_respond_with_renders_status_not_acceptable_if_mime_type_is_not_registered
201
+ @request.accept = 'application/json'
202
+ get :index
203
+ assert_equal '406 Not Acceptable', @response.status
204
+ end
205
+
206
+ def test_respond_with_renders_not_found_when_mime_type_is_valid_but_could_not_render
207
+ @request.accept = 'application/rss+xml'
208
+ get :index
209
+ assert_equal '404 Not Found', @response.status
210
+ end
211
+
212
+ def test_respond_to_all
213
+ @request.accept = '*/*'
214
+ get :index
215
+ assert_equal 'Index HTML', @response.body.strip
216
+ end
217
+
218
+ def test_respond_with_sets_content_type_properly
219
+ @request.accept = 'text/html'
220
+ get :index
221
+ assert_equal 'text/html', @response.content_type
222
+ assert_equal :html, @response.template.template_format
223
+
224
+ @request.accept = 'application/xml'
225
+ get :index
226
+ assert_equal 'application/xml', @response.content_type
227
+ assert_equal :xml, @response.template.template_format
228
+ end
229
+
230
+ def test_respond_with_when_to_is_given_as_option
231
+ @request.accept = 'text/html'
232
+ get :respond_with_options
233
+ assert_equal '406 Not Acceptable', @response.status
234
+
235
+ @request.accept = 'application/xml'
236
+ get :respond_with_options
237
+ assert_equal 'Generated XML', @response.body.strip
238
+ end
239
+
240
+ def test_respond_with_forwads_extra_options_to_render
241
+ @request.accept = 'application/xml'
242
+ get :respond_with_options
243
+ assert_equal 'Generated XML', @response.body.strip
244
+ assert_equal 'http://test.host/', @response.headers['Location']
245
+ end
246
+
247
+ def test_respond_with_skips_head_when_skip_not_acceptable_is_given
248
+ @request.accept = 'application/rss+xml'
249
+ get :skip_not_acceptable
250
+ assert_equal 'Will not raise double render error.', @response.body.strip
251
+ end
252
+
253
+ def test_respond_to_when_a_resource_is_given_as_option
254
+ @request.accept = 'text/html'
255
+ get :respond_to_with_resource
256
+ assert_equal 'RespondTo HTML', @response.body.strip
257
+
258
+ @request.accept = 'application/xml'
259
+ get :respond_to_with_resource
260
+ assert_equal 'Generated XML', @response.body.strip
261
+
262
+ @request.accept = 'application/json'
263
+ get :respond_to_with_resource
264
+ assert_equal '404 Not Found', @response.status
265
+
266
+ @request.accept = 'application/rss+xml'
267
+ get :respond_to_with_resource
268
+ assert_equal '406 Not Acceptable', @response.status
269
+ end
270
+
271
+ def test_respond_to_overwrite_class_method_definition
272
+ @request.accept = 'application/rss+xml'
273
+ get :respond_to_with_resource_and_blocks
274
+ assert_equal 'Render RSS', @response.body.strip
275
+ end
276
+
277
+ def test_respond_to_fallback_to_first_block_when_mime_type_is_all
278
+ @request.accept = '*/*'
279
+ get :respond_to_with_resource_and_blocks
280
+ assert_equal 'Render JSON', @response.body.strip
281
+ end
282
+ end