josevalim-inherited_resources 0.8.5 → 0.9.0

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.
@@ -136,10 +136,9 @@ module InheritedResources
136
136
  end
137
137
 
138
138
  if key.nil?
139
- raise ScriptError, "Could not find param for polymorphic association.
140
- The request params keys are #{params.keys.inspect}
141
- and the polymorphic associations are
142
- #{polymorphic_symbols.inspect}." unless polymorphic_config[:optional]
139
+ raise ScriptError, "Could not find param for polymorphic association. The request" <<
140
+ "parameters are #{params.keys.inspect} and the polymorphic " <<
141
+ "associations are #{polymorphic_config[:symbols].inspect}." unless polymorphic_config[:optional]
143
142
 
144
143
  nil
145
144
  else
data/test/aliases_test.rb CHANGED
@@ -4,8 +4,13 @@ class Student;
4
4
  def self.human_name; 'Student'; end
5
5
  end
6
6
 
7
+ class ApplicationController < ActionController::Base
8
+ include InheritedResources::DSL
9
+ end
10
+
7
11
  class StudentsController < ApplicationController
8
12
  inherit_resources
13
+ respond_to :html, :xml
9
14
 
10
15
  def edit
11
16
  edit! do |format|
@@ -18,26 +23,19 @@ class StudentsController < ApplicationController
18
23
  new!
19
24
  end
20
25
 
21
- def create
22
- create! do |success, failure|
23
- success.html { render :text => "I won't redirect!" }
24
- failure.xml { render :text => "I shouldn't be rendered" }
25
- end
26
+ create!(:location => "http://test.host/") do |success, failure|
27
+ success.html { render :text => "I won't redirect!" }
28
+ failure.xml { render :text => "I shouldn't be rendered" }
26
29
  end
27
30
 
28
- def update
29
- update! do |success, failure|
30
- success.html { redirect_to(resource_url) }
31
- failure.html { render :text => "I won't render!" }
32
- end
31
+ update! do |success, failure|
32
+ success.html { redirect_to(resource_url) }
33
+ failure.html { render :text => "I won't render!" }
33
34
  end
34
35
 
35
- def destroy
36
- destroy! do |format|
37
- format.html { render :text => "Destroyed!" }
38
- end
36
+ destroy! do |format|
37
+ format.html { render :text => "Destroyed!" }
39
38
  end
40
-
41
39
  end
42
40
 
43
41
  class AliasesTest < ActionController::TestCase
@@ -89,7 +87,7 @@ class AliasesTest < ActionController::TestCase
89
87
 
90
88
  def test_dumb_responder_quietly_receives_everything_on_failure
91
89
  @request.accept = 'text/html'
92
- Student.stubs(:new).returns(mock_student(:save => false, :errors => []))
90
+ Student.stubs(:new).returns(mock_student(:save => false, :errors => {:some => :error}))
93
91
  @controller.stubs(:resource_url).returns('http://test.host/')
94
92
  post :create
95
93
  assert_response :success
@@ -98,7 +96,7 @@ class AliasesTest < ActionController::TestCase
98
96
 
99
97
  def test_html_is_the_default_when_only_xml_is_overwriten
100
98
  @request.accept = '*/*'
101
- Student.stubs(:new).returns(mock_student(:save => false, :errors => []))
99
+ Student.stubs(:new).returns(mock_student(:save => false, :errors => {:some => :error}))
102
100
  @controller.stubs(:resource_url).returns('http://test.host/')
103
101
  post :create
104
102
  assert_response :success
@@ -106,7 +104,7 @@ class AliasesTest < ActionController::TestCase
106
104
  end
107
105
 
108
106
  def test_wont_render_edit_template_on_update_with_failure_if_failure_block_is_given
109
- Student.stubs(:find).returns(mock_student(:update_attributes => false, :errors => []))
107
+ Student.stubs(:find).returns(mock_student(:update_attributes => false))
110
108
  put :update
111
109
  assert_response :success
112
110
  assert_equal "I won't render!", @response.body
@@ -126,6 +124,13 @@ class AliasesTest < ActionController::TestCase
126
124
  assert_equal "Destroyed!", @response.body
127
125
  end
128
126
 
127
+ def test_options_are_used_in_respond_with
128
+ @request.accept = "application/xml"
129
+ Student.stubs(:new).returns(mock_student(:save => true, :to_xml => "XML"))
130
+ post :create
131
+ assert_equal "http://test.host/", @response.location
132
+ end
133
+
129
134
  protected
130
135
  def mock_student(stubs={})
131
136
  @mock_student ||= mock(stubs)
@@ -61,6 +61,13 @@ class AssociationChainBaseHelpersTest < ActionController::TestCase
61
61
  assert_equal 'new pet', assigns(:pet)
62
62
  end
63
63
 
64
+ def test_model_is_not_initialized_with_nil
65
+ @controller.current_user.expects(:pets).returns(Pet)
66
+ Pet.expects(:build).with({}).returns(mock_pet)
67
+ get :new
68
+ assert mock_pet, assigns(:pet)
69
+ end
70
+
64
71
  protected
65
72
  def mock_pet(stubs={})
66
73
  @mock_pet ||= mock(stubs)
data/test/base_test.rb CHANGED
@@ -131,7 +131,7 @@ class CreateActionBaseTest < ActionController::TestCase
131
131
 
132
132
  def test_redirect_to_the_created_user
133
133
  User.stubs(:new).returns(mock_user(:save => true))
134
- @controller.expects(:resource_url).returns('http://test.host/').times(2)
134
+ @controller.expects(:resource_url).returns('http://test.host/')
135
135
  post :create
136
136
  assert_redirected_to 'http://test.host/'
137
137
  end
@@ -143,14 +143,14 @@ class CreateActionBaseTest < ActionController::TestCase
143
143
  end
144
144
 
145
145
  def test_render_new_template_when_user_cannot_be_saved
146
- User.stubs(:new).returns(mock_user(:save => false, :errors => []))
146
+ User.stubs(:new).returns(mock_user(:save => false, :errors => {:some => :error}))
147
147
  post :create
148
148
  assert_response :success
149
149
  assert_template :new
150
150
  end
151
151
 
152
152
  def test_dont_show_flash_message_when_user_cannot_be_saved
153
- User.stubs(:new).returns(mock_user(:save => false, :errors => []))
153
+ User.stubs(:new).returns(mock_user(:save => false, :errors => {:some => :error}))
154
154
  post :create
155
155
  assert flash.empty?
156
156
  end
@@ -180,14 +180,14 @@ class UpdateActionBaseTest < ActionController::TestCase
180
180
  end
181
181
 
182
182
  def test_render_edit_template_when_user_cannot_be_saved
183
- User.stubs(:find).returns(mock_user(:update_attributes => false, :errors => []))
183
+ User.stubs(:find).returns(mock_user(:update_attributes => false, :errors => {:some => :error}))
184
184
  put :update
185
185
  assert_response :success
186
186
  assert_template :edit
187
187
  end
188
188
 
189
189
  def test_dont_show_flash_message_when_user_cannot_be_saved
190
- User.stubs(:find).returns(mock_user(:update_attributes => false, :errors => []))
190
+ User.stubs(:find).returns(mock_user(:update_attributes => false, :errors => {:some => :error}))
191
191
  put :update
192
192
  assert flash.empty?
193
193
  end
data/test/flash_test.rb CHANGED
@@ -5,6 +5,7 @@ class Address
5
5
  end
6
6
 
7
7
  class AddressesController < InheritedResources::Base
8
+ respond_to :xml
8
9
  protected
9
10
  def interpolation_options
10
11
  { :reference => 'Ocean Avenue' }
@@ -13,6 +14,7 @@ end
13
14
 
14
15
  module Admin; end
15
16
  class Admin::AddressesController < InheritedResources::Base
17
+ respond_to :xml
16
18
  protected
17
19
  def interpolation_options
18
20
  { :reference => 'Ocean Avenue' }
@@ -23,34 +25,37 @@ class FlashBaseHelpersTest < ActionController::TestCase
23
25
  tests AddressesController
24
26
 
25
27
  def setup
28
+ super
26
29
  @request.accept = 'application/xml'
30
+ @controller.stubs(:resource_url).returns("http://test.host/")
31
+ @controller.stubs(:collection_url).returns("http://test.host/")
27
32
  end
28
33
 
29
34
  def test_success_flash_message_on_create_with_yml
30
35
  Address.stubs(:new).returns(mock_address(:save => true))
31
- @controller.stubs(:address_url)
32
36
  post :create
33
37
  assert_equal 'You created a new address close to <b>Ocean Avenue</b>.', flash[:notice]
34
38
  end
35
39
 
36
40
  def test_success_flash_message_on_create_with_namespaced_controller
37
41
  @controller = Admin::AddressesController.new
42
+ @controller.stubs(:resource_url).returns("http://test.host/")
38
43
  Address.stubs(:new).returns(mock_address(:save => true))
39
- @controller.stubs(:address_url)
40
44
  post :create
41
45
  assert_equal 'Admin, you created a new address close to <b>Ocean Avenue</b>.', flash[:notice]
42
46
  end
43
47
 
44
48
  def test_failure_flash_message_on_create_with_namespaced_controller_actions
45
49
  @controller = Admin::AddressesController.new
50
+ @controller.stubs(:resource_url).returns("http://test.host/")
46
51
  Address.stubs(:new).returns(mock_address(:save => false))
47
- @controller.stubs(:address_url)
48
52
  post :create
49
53
  assert_equal 'Admin error message.', flash[:error]
50
54
  end
51
55
 
52
56
  def test_inherited_success_flash_message_on_update_on_namespaced_controllers
53
57
  @controller = Admin::AddressesController.new
58
+ @controller.stubs(:resource_url).returns("http://test.host/")
54
59
  Address.stubs(:find).returns(mock_address(:update_attributes => true))
55
60
  put :update
56
61
  assert_response :success
@@ -65,7 +70,7 @@ class FlashBaseHelpersTest < ActionController::TestCase
65
70
  end
66
71
 
67
72
  def test_failure_flash_message_on_update
68
- Address.stubs(:find).returns(mock_address(:update_attributes => false, :errors => []))
73
+ Address.stubs(:find).returns(mock_address(:update_attributes => false, :errors => {:some => :error}))
69
74
  put :update
70
75
  assert_equal 'Oh no! We could not update your address!', flash[:error]
71
76
  end
@@ -78,6 +83,6 @@ class FlashBaseHelpersTest < ActionController::TestCase
78
83
 
79
84
  protected
80
85
  def mock_address(stubs={})
81
- @mock_address ||= mock(stubs)
86
+ @mock_address ||= stub(stubs.merge(:to_xml => "xml"))
82
87
  end
83
88
  end
@@ -28,21 +28,18 @@ class RedirectToWithBlockTest < ActionController::TestCase
28
28
 
29
29
  def test_redirect_to_the_given_url_on_create
30
30
  Machine.stubs(:new).returns(mock_machine(:save => true))
31
- @controller.expects(:resource_url).times(0)
32
31
  post :create
33
32
  assert_redirected_to 'http://test.host/create'
34
33
  end
35
34
 
36
35
  def test_redirect_to_the_given_url_on_update
37
36
  Machine.stubs(:find).returns(mock_machine(:update_attributes => true))
38
- @controller.expects(:resource_url).times(0)
39
37
  put :update
40
38
  assert_redirected_to 'http://test.host/update'
41
39
  end
42
40
 
43
41
  def test_redirect_to_the_given_url_on_destroy
44
42
  Machine.stubs(:find).returns(mock_machine(:destroy => true))
45
- @controller.expects(:collection_url).times(0)
46
43
  delete :destroy
47
44
  assert_redirected_to 'http://test.host/destroy'
48
45
  end
@@ -52,26 +49,3 @@ class RedirectToWithBlockTest < ActionController::TestCase
52
49
  @mock_machine ||= mock(stubs)
53
50
  end
54
51
  end
55
-
56
-
57
- # Use this to test blocks with multiple arity in the future.
58
- class SuperMachinesController < InheritedResources::Base
59
- defaults :resource_class => Machine
60
-
61
- def create
62
- create! do |arg1, arg2, arg3|
63
- # nothing
64
- end
65
- end
66
- end
67
-
68
- class RedirectToArityTest < ActionController::TestCase
69
- tests SuperMachinesController
70
-
71
- def test_redirect_to_the_given_url_on_create
72
- Machine.stubs(:new).returns(:anything)
73
- assert_raise ScriptError, /arity/ do
74
- post :create
75
- end
76
- end
77
- end
@@ -15,12 +15,10 @@ class Project
15
15
  end
16
16
 
17
17
  class ProjectsController < ActionController::Base
18
- # Inherited respond_to definition is:
19
- # respond_to :html
20
18
  respond_to :html
21
- respond_to :xml, :except => :edit
22
- respond_to :rjs => :edit
23
- respond_to :rss, :only => 'index'
19
+ respond_to :xml, :except => :edit
20
+ respond_to :rjs, :only => :edit
21
+ respond_to :rss, :only => :index
24
22
  respond_to :json, :except => :index
25
23
  respond_to :csv, :except => :index
26
24
 
@@ -28,21 +26,16 @@ class ProjectsController < ActionController::Base
28
26
  respond_with(Project.new)
29
27
  end
30
28
 
31
- def respond_with_options
32
- respond_with(Project.new, :to => [:xml, :json], :location => 'http://test.host/')
33
- end
34
-
35
- def skip_not_acceptable
36
- respond_with(Project.new, :skip_not_acceptable => true)
37
- render :text => 'Will not raise double render error.'
29
+ def respond_with_resource
30
+ respond_with(Project.new)
38
31
  end
39
32
 
40
- def respond_to_with_resource
41
- respond_to(:with => Project.new)
33
+ def respond_with_resource_and_options
34
+ respond_with(Project.new, :location => 'http://test.host/')
42
35
  end
43
36
 
44
- def respond_to_with_resource_and_blocks
45
- respond_to(:with => Project.new) do |format|
37
+ def respond_with_resource_and_blocks
38
+ respond_with(Project.new) do |format|
46
39
  format.json { render :text => 'Render JSON' }
47
40
  format.rss { render :text => 'Render RSS' }
48
41
  end
@@ -53,7 +46,7 @@ class ProjectsController < ActionController::Base
53
46
  # block. This tests exactly this case.
54
47
  #
55
48
  def respond_to_skip_default_template
56
- respond_to(:with => Project.new) do |format|
49
+ respond_with(Project.new) do |format|
57
50
  format.html { render :text => 'Render HTML' }
58
51
  end
59
52
  end
@@ -62,139 +55,6 @@ end
62
55
  class SuperProjectsController < ProjectsController
63
56
  end
64
57
 
65
- class RespondToUnitTest < ActionController::TestCase
66
- tests ProjectsController
67
-
68
- def setup
69
- @formats = @controller.formats_for_respond_to
70
- @responder = ActionController::MimeResponds::Responder.new(@controller)
71
- end
72
-
73
- def test_respond_to_class_method_without_options
74
- assert_nil @formats[:html][:only]
75
- assert_nil @formats[:html][:except]
76
- end
77
-
78
- def test_respond_to_class_method_inheritance
79
- assert_nil @formats[:xml][:only]
80
- assert_equal [:edit], @formats[:xml][:except]
81
- end
82
-
83
- def test_respond_to_class_method_with_implicit_only
84
- assert_equal [:edit], @formats[:rjs][:only]
85
- assert_nil @formats[:rjs][:except]
86
- end
87
-
88
- def test_respond_to_class_method_with_explicit_only
89
- assert_equal [:index], @formats[:rss][:only]
90
- assert_nil @formats[:rss][:except]
91
- end
92
-
93
- def test_respond_to_class_method_with_explicit_except
94
- assert_nil @formats[:json][:only]
95
- assert_equal [:index], @formats[:json][:except]
96
- end
97
-
98
- def test_action_respond_to_format
99
- @controller.action_name = 'index'
100
- assert @responder.action_respond_to_format?('html') # defined
101
- assert @responder.action_respond_to_format?('xml') # inherited
102
- assert @responder.action_respond_to_format?('rss') # explicit only
103
- assert !@responder.action_respond_to_format?('json') # exception
104
-
105
- @controller.action_name = 'edit'
106
- assert !@responder.action_respond_to_format?('xml') # inherited
107
- assert @responder.action_respond_to_format?('rjs') # implicit only
108
- assert @responder.action_respond_to_format?('json') # exception
109
- end
110
-
111
- def test_action_respond_to_format_with_additional_mimes
112
- assert @responder.action_respond_to_format?('html', [:xml, :html, :json])
113
- assert !@responder.action_respond_to_format?('html', [:xml, :rss, :json])
114
-
115
- @controller.action_name = 'index'
116
- assert @responder.action_respond_to_format?('html', [])
117
- assert !@responder.action_respond_to_format?('json', [])
118
- end
119
-
120
- def test_clear_respond_to
121
- @controller = SuperProjectsController.new
122
- @controller.request = ActionController::TestRequest.new
123
-
124
- @controller.action_name = 'index'
125
- @responder = ActionController::MimeResponds::Responder.new(@controller)
126
-
127
- # Those responses are inherited from ProjectsController
128
- assert @responder.action_respond_to_format?('html') # defined
129
- assert @responder.action_respond_to_format?('xml') # inherited
130
- assert @responder.action_respond_to_format?('rss') # explicit only
131
-
132
- # Let's clear respond_to definitions
133
- SuperProjectsController.send(:clear_respond_to!)
134
-
135
- assert !@responder.action_respond_to_format?('html')
136
- assert !@responder.action_respond_to_format?('xml')
137
- assert !@responder.action_respond_to_format?('rss')
138
- end
139
-
140
- def test_respond_except_any_does_not_respond_to_mime_all
141
- prepare_responder_to_respond!
142
-
143
- @responder.respond_except_any
144
- assert !@performed
145
-
146
- @responder.respond
147
- assert @performed
148
- end
149
-
150
- def test_respond_any_responds_to_mime_all
151
- prepare_responder_to_respond!
152
-
153
- @responder.respond_any
154
- assert @performed
155
- end
156
-
157
- def test_respond_any_responds_only_to_all
158
- prepare_responder_to_respond!('text/html')
159
-
160
- @responder.respond_any
161
- assert !@performed
162
- end
163
-
164
- def test_responder_prioritize
165
- prepare_responder_to_respond!
166
- assert_equal [Mime::HTML, Mime::XML], @responder.order
167
-
168
- @responder.prioritize(:xml)
169
- assert_equal [Mime::XML, Mime::HTML], @responder.order
170
-
171
- @responder.prioritize(:js)
172
- assert_equal [Mime::XML, Mime::HTML], @responder.order
173
- end
174
-
175
- protected
176
- def prepare_responder_to_respond!(content_type='*/*')
177
- @controller.request = @request = ActionController::TestRequest.new
178
- @controller.response = @response = ActionController::TestResponse.new
179
-
180
- @request.accept = content_type
181
- @responder = ActionController::MimeResponds::Responder.new(@controller)
182
- @performed = false
183
-
184
- # Mock template
185
- template = mock()
186
- @response.stubs(:template).returns(template)
187
- template.stubs(:template_format=).returns(true)
188
-
189
- respond_to_declaration = proc { |format|
190
- format.html { @performed = true }
191
- format.xml { }
192
- }
193
-
194
- respond_to_declaration.call(@responder)
195
- end
196
- end
197
-
198
58
  class RespondToFunctionalTest < ActionController::TestCase
199
59
  tests ProjectsController
200
60
 
@@ -222,10 +82,11 @@ class RespondToFunctionalTest < ActionController::TestCase
222
82
  assert_equal '406 Not Acceptable', @response.status
223
83
  end
224
84
 
225
- def test_respond_with_renders_not_found_when_mime_type_is_valid_but_could_not_render
85
+ def test_respond_with_raises_error_if_could_not_respond
226
86
  @request.accept = 'application/rss+xml'
227
- get :index
228
- assert_equal '404 Not Found', @response.status
87
+ assert_raise ActionView::MissingTemplate do
88
+ get :index
89
+ end
229
90
  end
230
91
 
231
92
  def test_respond_to_all
@@ -234,13 +95,6 @@ class RespondToFunctionalTest < ActionController::TestCase
234
95
  assert_equal 'Index HTML', @response.body.strip
235
96
  end
236
97
 
237
- def test_default_template_is_not_rendered_if_template_format_is_not_accepted
238
- @controller.stubs(:default_template_format).returns(:json)
239
- @request.accept = '*/*'
240
- get :index
241
- assert_equal '406 Not Acceptable', @response.status
242
- end
243
-
244
98
  def test_respond_with_sets_content_type_properly
245
99
  @request.accept = 'text/html'
246
100
  get :index
@@ -253,57 +107,44 @@ class RespondToFunctionalTest < ActionController::TestCase
253
107
  assert_equal :xml, @response.template.template_format
254
108
  end
255
109
 
256
- def test_respond_with_when_to_is_given_as_option
257
- @request.accept = 'text/html'
258
- get :respond_with_options
259
- assert_equal '406 Not Acceptable', @response.status
260
-
261
- @request.accept = 'application/xml'
262
- get :respond_with_options
263
- assert_equal 'Generated XML', @response.body.strip
264
- end
265
-
266
110
  def test_respond_with_forwads_extra_options_to_render
267
111
  @request.accept = 'application/xml'
268
- get :respond_with_options
112
+ get :respond_with_resource_and_options
269
113
  assert_equal 'Generated XML', @response.body.strip
270
114
  assert_equal 'http://test.host/', @response.headers['Location']
271
115
  end
272
116
 
273
- def test_respond_with_skips_head_when_skip_not_acceptable_is_given
274
- @request.accept = 'application/rss+xml'
275
- get :skip_not_acceptable
276
- assert_equal 'Will not raise double render error.', @response.body.strip
277
- end
278
-
279
117
  def test_respond_to_when_a_resource_is_given_as_option
280
118
  @request.accept = 'text/html'
281
- get :respond_to_with_resource
119
+ get :respond_with_resource
282
120
  assert_equal 'RespondTo HTML', @response.body.strip
283
121
 
284
122
  @request.accept = 'application/xml'
285
- get :respond_to_with_resource
123
+ get :respond_with_resource
286
124
  assert_equal 'Generated XML', @response.body.strip
287
125
 
288
- @request.accept = 'application/json'
289
- get :respond_to_with_resource
290
- assert_equal '404 Not Found', @response.status
291
-
292
126
  @request.accept = 'application/rss+xml'
293
- get :respond_to_with_resource
127
+ get :respond_with_resource
294
128
  assert_equal '406 Not Acceptable', @response.status
129
+
130
+ @request.accept = 'application/json'
131
+ assert_raise ActionView::MissingTemplate do
132
+ get :respond_with_resource
133
+ end
295
134
  end
296
135
 
297
136
  def test_respond_to_overwrite_class_method_definition
298
137
  @request.accept = 'application/rss+xml'
299
- get :respond_to_with_resource_and_blocks
138
+ get :respond_with_resource_and_blocks
300
139
  assert_equal 'Render RSS', @response.body.strip
301
140
  end
302
141
 
303
- def test_respond_to_fallback_to_first_block_when_mime_type_is_all
142
+ def test_respond_to_first_configured_mime_in_respond_to_when_mime_type_is_all
304
143
  @request.accept = '*/*'
305
- get :respond_to_with_resource_and_blocks
306
- assert_equal 'Render JSON', @response.body.strip
144
+ assert_raise ActionView::MissingTemplate do
145
+ get :respond_with_resource_and_blocks
146
+ end
147
+ assert_equal 'text/html', @response.content_type
307
148
  end
308
149
 
309
150
  def test_respond_to_skip_default_template_when_it_is_in_block