responders 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b652ad426b4ce87cb002a4b5562f79c2335c412
4
- data.tar.gz: ee67a5d29f4cd5920a228ccc6229c52bfdddfea8
3
+ metadata.gz: 1048b35bc57148e916fc54ea72aa8a0e3f9f7498
4
+ data.tar.gz: 8d4c1c0825d565528c29371185d374e8266336ff
5
5
  SHA512:
6
- metadata.gz: 102cd8209ca03e4275003de1c82814eec7da60f92f118d7dd38bbaca8ce56a1e1fef7b0278a99cecaf9a3016fc85e4381e1fee440a1ecc376c024924b3b9d2dd
7
- data.tar.gz: 6733104f4c7ce9f7250f1ce410562661f16b8d11ce0193c0054264682d4757f4b44d47ce2931df6986d0864b6bc74411388caf7a09c2e5707bf00470f29784bd
6
+ metadata.gz: 2cd86b1e17658300952e4fea7326eafc6474d9b47ee7520322521a103300fa3ad803ed41c5c7849febdf0769bb7f43085d2898fe589d0b85cc835a9017f7e07c
7
+ data.tar.gz: f390f3b7d962ede7cda0b454d45b7864d2606472c33029a24981d84374be0ba8d8f98536aa5af2ef9aa4c0f915e4d2750a2f06278cf4113f82f35e30ceb5dc6a
@@ -1,8 +1,8 @@
1
1
  ## 2.0.1
2
2
 
3
- * Require "rails/railtie" explicitly before using it
4
- * Require "action_controller" explicitly before using it
5
- * Remove unecessary and limitting `resourceful?` check that required models to implement `to_#{format}` (such checks are responsibility of the rendering layer)
3
+ * Require `rails/railtie` explicitly before using it
4
+ * Require `action_controller` explicitly before using it
5
+ * Remove unnecessary and limiting `resourceful?` check that required models to implement `to_#{format}` (such checks are responsibility of the rendering layer)
6
6
 
7
7
  ## 2.0.0
8
8
 
@@ -1,53 +1,55 @@
1
1
  <% module_namespacing do -%>
2
2
  class <%= controller_class_name %>Controller < ApplicationController
3
- <%= controller_before_filter %> :set_<%= file_name %>, only: [:show, :edit, :update, :destroy]
3
+ <%= controller_before_filter %> :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
4
+
5
+ respond_to :html
4
6
 
5
7
  <% unless options[:singleton] -%>
6
8
  def index
7
- @<%= table_name %> = <%= orm_class.all(class_name) %>
8
- respond_with(@<%= table_name %>)
9
+ @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
10
+ respond_with(@<%= plural_table_name %>)
9
11
  end
10
12
  <% end -%>
11
13
 
12
14
  def show
13
- respond_with(@<%= file_name %>)
15
+ respond_with(@<%= singular_table_name %>)
14
16
  end
15
17
 
16
18
  def new
17
- @<%= file_name %> = <%= orm_class.build(class_name) %>
18
- respond_with(@<%= file_name %>)
19
+ @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
20
+ respond_with(@<%= singular_table_name %>)
19
21
  end
20
22
 
21
23
  def edit
22
24
  end
23
25
 
24
26
  def create
25
- @<%= file_name %> = <%= orm_class.build(class_name, attributes_params) %>
27
+ @<%= singular_table_name %> = <%= orm_class.build(class_name, attributes_params) %>
26
28
  <%= "flash[:notice] = '#{class_name} was successfully created.' if " if flash? %>@<%= orm_instance.save %>
27
- respond_with(@<%= file_name %>)
29
+ respond_with(@<%= singular_table_name %>)
28
30
  end
29
31
 
30
32
  def update
31
33
  <%= "flash[:notice] = '#{class_name} was successfully updated.' if " if flash? %>@<%= orm_instance_update(attributes_params) %>
32
- respond_with(@<%= file_name %>)
34
+ respond_with(@<%= singular_table_name %>)
33
35
  end
34
36
 
35
37
  def destroy
36
38
  @<%= orm_instance.destroy %>
37
- respond_with(@<%= file_name %>)
39
+ respond_with(@<%= singular_table_name %>)
38
40
  end
39
41
 
40
42
  private
41
- def set_<%= file_name %>
42
- @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
43
+ def set_<%= singular_table_name %>
44
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
43
45
  end
44
46
  <%- if strong_parameters_defined? -%>
45
47
 
46
- def <%= "#{file_name}_params" %>
48
+ def <%= "#{singular_table_name}_params" %>
47
49
  <%- if attributes_names.empty? -%>
48
- params[:<%= file_name %>]
50
+ params[:<%= singular_table_name %>]
49
51
  <%- else -%>
50
- params.require(:<%= file_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
52
+ params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
51
53
  <%- end -%>
52
54
  end
53
55
  <%- end -%>
@@ -1,3 +1,3 @@
1
1
  module Responders
2
- VERSION = "2.0.1".freeze
2
+ VERSION = "2.0.2".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: responders
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Valim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-21 00:00:00.000000000 Z
11
+ date: 2014-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -53,24 +53,6 @@ files:
53
53
  - lib/responders/locales/en.yml
54
54
  - lib/responders/location_responder.rb
55
55
  - lib/responders/version.rb
56
- - test/action_controller/respond_with_test.rb
57
- - test/locales/en.yml
58
- - test/responders/collection_responder_test.rb
59
- - test/responders/controller_method_test.rb
60
- - test/responders/flash_responder_test.rb
61
- - test/responders/http_cache_responder_test.rb
62
- - test/test_helper.rb
63
- - test/views/addresses/create.js.erb
64
- - test/views/addresses/edit.html.erb
65
- - test/views/addresses/new.html.erb
66
- - test/views/locations/new.html.erb
67
- - test/views/respond_with/edit.html.erb
68
- - test/views/respond_with/new.html.erb
69
- - test/views/respond_with/respond_with_additional_params.html.erb
70
- - test/views/respond_with/using_invalid_resource_with_template.xml.erb
71
- - test/views/respond_with/using_options_with_template.xml.erb
72
- - test/views/respond_with/using_resource.js.erb
73
- - test/views/respond_with/using_resource_with_block.html.erb
74
56
  homepage: http://github.com/plataformatec/responders
75
57
  licenses:
76
58
  - MIT
@@ -95,22 +77,4 @@ rubygems_version: 2.2.2
95
77
  signing_key:
96
78
  specification_version: 4
97
79
  summary: A set of Rails responders to dry up your application
98
- test_files:
99
- - test/action_controller/respond_with_test.rb
100
- - test/locales/en.yml
101
- - test/responders/collection_responder_test.rb
102
- - test/responders/controller_method_test.rb
103
- - test/responders/flash_responder_test.rb
104
- - test/responders/http_cache_responder_test.rb
105
- - test/test_helper.rb
106
- - test/views/addresses/create.js.erb
107
- - test/views/addresses/edit.html.erb
108
- - test/views/addresses/new.html.erb
109
- - test/views/locations/new.html.erb
110
- - test/views/respond_with/edit.html.erb
111
- - test/views/respond_with/new.html.erb
112
- - test/views/respond_with/respond_with_additional_params.html.erb
113
- - test/views/respond_with/using_invalid_resource_with_template.xml.erb
114
- - test/views/respond_with/using_options_with_template.xml.erb
115
- - test/views/respond_with/using_resource.js.erb
116
- - test/views/respond_with/using_resource_with_block.html.erb
80
+ test_files: []
@@ -1,715 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Customer < Struct.new(:name, :id)
4
- extend ActiveModel::Naming
5
- include ActiveModel::Conversion
6
-
7
- def to_xml(options={})
8
- if options[:builder]
9
- options[:builder].name name
10
- else
11
- "<name>#{name}</name>"
12
- end
13
- end
14
-
15
- def to_js(options={})
16
- "name: #{name.inspect}"
17
- end
18
- alias :to_text :to_js
19
-
20
- def errors
21
- []
22
- end
23
-
24
- def persisted?
25
- id.present?
26
- end
27
- end
28
-
29
- class ValidatedCustomer < Customer
30
- def errors
31
- if name =~ /Sikachu/i
32
- []
33
- else
34
- [{:name => "is invalid"}]
35
- end
36
- end
37
- end
38
-
39
- module Quiz
40
- class Question < Struct.new(:name, :id)
41
- extend ActiveModel::Naming
42
- include ActiveModel::Conversion
43
-
44
- def persisted?
45
- id.present?
46
- end
47
- end
48
-
49
- class Store < Question
50
- end
51
- end
52
-
53
- class RespondWithController < ApplicationController
54
- class CustomerWithJson < Customer
55
- def to_json; super; end
56
- end
57
-
58
- respond_to :html, :json, :touch
59
- respond_to :xml, :except => :using_resource_with_block
60
- respond_to :js, :only => [ :using_resource_with_block, :using_resource, 'using_hash_resource' ]
61
-
62
- def using_resource
63
- respond_with(resource)
64
- end
65
-
66
- def using_hash_resource
67
- respond_with({:result => resource})
68
- end
69
-
70
- def using_resource_with_block
71
- respond_with(resource) do |format|
72
- format.csv { render :text => "CSV" }
73
- end
74
- end
75
-
76
- def using_resource_with_overwrite_block
77
- respond_with(resource) do |format|
78
- format.html { render :text => "HTML" }
79
- end
80
- end
81
-
82
- def using_resource_with_collection
83
- respond_with([resource, Customer.new("jamis", 9)])
84
- end
85
-
86
- def using_resource_with_parent
87
- respond_with(Quiz::Store.new("developer?", 11), Customer.new("david", 13))
88
- end
89
-
90
- def using_resource_with_status_and_location
91
- respond_with(resource, :location => "http://test.host/", :status => :created)
92
- end
93
-
94
- def using_resource_with_json
95
- respond_with(CustomerWithJson.new("david", request.delete? ? nil : 13))
96
- end
97
-
98
- def using_invalid_resource_with_template
99
- respond_with(resource)
100
- end
101
-
102
- def using_options_with_template
103
- @customer = resource
104
- respond_with(@customer, :status => 123, :location => "http://test.host/")
105
- end
106
-
107
- def using_resource_with_responder
108
- responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" }
109
- respond_with(resource, :responder => responder)
110
- end
111
-
112
- def using_resource_with_action
113
- respond_with(resource, :action => :foo) do |format|
114
- format.html { raise ActionView::MissingTemplate.new([], "bar", ["foo"], {}, false) }
115
- end
116
- end
117
-
118
- def using_responder_with_respond
119
- responder = Class.new(ActionController::Responder) do
120
- def respond; @controller.render :text => "respond #{format}"; end
121
- end
122
- respond_with(resource, :responder => responder)
123
- end
124
-
125
- def respond_with_additional_params
126
- @params = RespondWithController.params
127
- respond_with({:result => resource}, @params)
128
- end
129
-
130
- protected
131
- def self.params
132
- {
133
- :foo => 'bar'
134
- }
135
- end
136
-
137
- def resource
138
- Customer.new("david", request.delete? ? nil : 13)
139
- end
140
- end
141
-
142
- class InheritedRespondWithController < RespondWithController
143
- clear_respond_to
144
- respond_to :xml, :json
145
-
146
- def index
147
- respond_with(resource) do |format|
148
- format.json { render :text => "JSON" }
149
- end
150
- end
151
- end
152
-
153
- class CsvRespondWithController < ApplicationController
154
- respond_to :csv
155
-
156
- class RespondWithCsv
157
- def to_csv
158
- "c,s,v"
159
- end
160
- end
161
-
162
- def index
163
- respond_with(RespondWithCsv.new)
164
- end
165
- end
166
-
167
- class EmptyRespondWithController < ApplicationController
168
- clear_respond_to
169
- def index
170
- respond_with(Customer.new("david", 13))
171
- end
172
- end
173
-
174
- class RespondWithControllerTest < ActionController::TestCase
175
- def setup
176
- super
177
- @request.host = "www.example.com"
178
- Mime::Type.register_alias('text/html', :iphone)
179
- Mime::Type.register_alias('text/html', :touch)
180
- Mime::Type.register('text/x-mobile', :mobile)
181
- end
182
-
183
- def teardown
184
- super
185
- Mime::Type.unregister(:iphone)
186
- Mime::Type.unregister(:touch)
187
- Mime::Type.unregister(:mobile)
188
- end
189
-
190
- def test_respond_with_shouldnt_modify_original_hash
191
- get :respond_with_additional_params
192
- assert_equal RespondWithController.params, assigns(:params)
193
- end
194
-
195
- def test_using_resource
196
- @request.accept = "application/xml"
197
- get :using_resource
198
- assert_equal "application/xml", @response.content_type
199
- assert_equal "<name>david</name>", @response.body
200
-
201
- @request.accept = "application/json"
202
- get :using_resource
203
- assert_equal "application/json", @response.content_type
204
- assert_equal "{\"name\":\"david\",\"id\":13}", @response.body
205
- end
206
-
207
- def test_using_resource_with_js_simply_tries_to_render_the_template
208
- @request.accept = "text/javascript"
209
- get :using_resource
210
- assert_equal "text/javascript", @response.content_type
211
- assert_equal "alert(\"Hi\");", @response.body
212
- end
213
-
214
- def test_using_hash_resource_with_js_raises_an_error_if_template_cant_be_found
215
- @request.accept = "text/javascript"
216
- assert_raise ActionView::MissingTemplate do
217
- get :using_hash_resource
218
- end
219
- end
220
-
221
- def test_using_hash_resource
222
- @request.accept = "application/xml"
223
- get :using_hash_resource
224
- assert_equal "application/xml", @response.content_type
225
- assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <name>david</name>\n</hash>\n", @response.body
226
-
227
- @request.accept = "application/json"
228
- get :using_hash_resource
229
- assert_equal "application/json", @response.content_type
230
- assert @response.body.include?("result")
231
- assert @response.body.include?('"name":"david"')
232
- assert @response.body.include?('"id":13')
233
- end
234
-
235
- def test_using_hash_resource_with_post
236
- @request.accept = "application/json"
237
- assert_raise ArgumentError, "Nil location provided. Can't build URI." do
238
- post :using_hash_resource
239
- end
240
- end
241
-
242
- def test_using_resource_with_block
243
- @request.accept = "*/*"
244
- get :using_resource_with_block
245
- assert_equal "text/html", @response.content_type
246
- assert_equal 'Hello world!', @response.body
247
-
248
- @request.accept = "text/csv"
249
- get :using_resource_with_block
250
- assert_equal "text/csv", @response.content_type
251
- assert_equal "CSV", @response.body
252
-
253
- @request.accept = "application/xml"
254
- get :using_resource
255
- assert_equal "application/xml", @response.content_type
256
- assert_equal "<name>david</name>", @response.body
257
- end
258
-
259
- def test_using_resource_with_overwrite_block
260
- get :using_resource_with_overwrite_block
261
- assert_equal "text/html", @response.content_type
262
- assert_equal "HTML", @response.body
263
- end
264
-
265
- def test_not_acceptable
266
- @request.accept = "application/xml"
267
- assert_raises(ActionController::UnknownFormat) do
268
- get :using_resource_with_block
269
- end
270
-
271
- @request.accept = "text/javascript"
272
- assert_raises(ActionController::UnknownFormat) do
273
- get :using_resource_with_overwrite_block
274
- end
275
- end
276
-
277
- def test_using_resource_for_post_with_html_redirects_on_success
278
- with_test_route_set do
279
- post :using_resource
280
- assert_equal "text/html", @response.content_type
281
- assert_equal 302, @response.status
282
- assert_equal "http://www.example.com/customers/13", @response.location
283
- assert @response.redirect?
284
- end
285
- end
286
-
287
- def test_using_resource_for_post_with_html_rerender_on_failure
288
- with_test_route_set do
289
- errors = { :name => :invalid }
290
- Customer.any_instance.stubs(:errors).returns(errors)
291
- post :using_resource
292
- assert_equal "text/html", @response.content_type
293
- assert_equal 200, @response.status
294
- assert_equal "New world!\n", @response.body
295
- assert_nil @response.location
296
- end
297
- end
298
-
299
- def test_using_resource_for_post_with_xml_yields_created_on_success
300
- with_test_route_set do
301
- @request.accept = "application/xml"
302
- post :using_resource
303
- assert_equal "application/xml", @response.content_type
304
- assert_equal 201, @response.status
305
- assert_equal "<name>david</name>", @response.body
306
- assert_equal "http://www.example.com/customers/13", @response.location
307
- end
308
- end
309
-
310
- def test_using_resource_for_post_with_xml_yields_unprocessable_entity_on_failure
311
- with_test_route_set do
312
- @request.accept = "application/xml"
313
- errors = { :name => :invalid }
314
- Customer.any_instance.stubs(:errors).returns(errors)
315
- post :using_resource
316
- assert_equal "application/xml", @response.content_type
317
- assert_equal 422, @response.status
318
- assert_equal errors.to_xml, @response.body
319
- assert_nil @response.location
320
- end
321
- end
322
-
323
- def test_using_resource_for_post_with_json_yields_unprocessable_entity_on_failure
324
- with_test_route_set do
325
- @request.accept = "application/json"
326
- errors = { :name => :invalid }
327
- Customer.any_instance.stubs(:errors).returns(errors)
328
- post :using_resource
329
- assert_equal "application/json", @response.content_type
330
- assert_equal 422, @response.status
331
- errors = {:errors => errors}
332
- assert_equal errors.to_json, @response.body
333
- assert_nil @response.location
334
- end
335
- end
336
-
337
- def test_using_resource_for_patch_with_html_redirects_on_success
338
- with_test_route_set do
339
- patch :using_resource
340
- assert_equal "text/html", @response.content_type
341
- assert_equal 302, @response.status
342
- assert_equal "http://www.example.com/customers/13", @response.location
343
- assert @response.redirect?
344
- end
345
- end
346
-
347
- def test_using_resource_for_patch_with_html_rerender_on_failure
348
- with_test_route_set do
349
- errors = { :name => :invalid }
350
- Customer.any_instance.stubs(:errors).returns(errors)
351
- patch :using_resource
352
- assert_equal "text/html", @response.content_type
353
- assert_equal 200, @response.status
354
- assert_equal "Edit world!\n", @response.body
355
- assert_nil @response.location
356
- end
357
- end
358
-
359
- def test_using_resource_for_patch_with_html_rerender_on_failure_even_on_method_override
360
- with_test_route_set do
361
- errors = { :name => :invalid }
362
- Customer.any_instance.stubs(:errors).returns(errors)
363
- @request.env["rack.methodoverride.original_method"] = "POST"
364
- patch :using_resource
365
- assert_equal "text/html", @response.content_type
366
- assert_equal 200, @response.status
367
- assert_equal "Edit world!\n", @response.body
368
- assert_nil @response.location
369
- end
370
- end
371
-
372
- def test_using_resource_for_put_with_html_redirects_on_success
373
- with_test_route_set do
374
- put :using_resource
375
- assert_equal "text/html", @response.content_type
376
- assert_equal 302, @response.status
377
- assert_equal "http://www.example.com/customers/13", @response.location
378
- assert @response.redirect?
379
- end
380
- end
381
-
382
- def test_using_resource_for_put_with_html_rerender_on_failure
383
- with_test_route_set do
384
- errors = { :name => :invalid }
385
- Customer.any_instance.stubs(:errors).returns(errors)
386
- put :using_resource
387
-
388
- assert_equal "text/html", @response.content_type
389
- assert_equal 200, @response.status
390
- assert_equal "Edit world!\n", @response.body
391
- assert_nil @response.location
392
- end
393
- end
394
-
395
- def test_using_resource_for_put_with_html_rerender_on_failure_even_on_method_override
396
- with_test_route_set do
397
- errors = { :name => :invalid }
398
- Customer.any_instance.stubs(:errors).returns(errors)
399
- @request.env["rack.methodoverride.original_method"] = "POST"
400
- put :using_resource
401
- assert_equal "text/html", @response.content_type
402
- assert_equal 200, @response.status
403
- assert_equal "Edit world!\n", @response.body
404
- assert_nil @response.location
405
- end
406
- end
407
-
408
- def test_using_resource_for_put_with_xml_yields_no_content_on_success
409
- @request.accept = "application/xml"
410
- put :using_resource
411
- assert_equal "application/xml", @response.content_type
412
- assert_equal 204, @response.status
413
- assert_equal "", @response.body
414
- end
415
-
416
- def test_using_resource_for_put_with_json_yields_no_content_on_success
417
- @request.accept = "application/json"
418
- put :using_resource_with_json
419
- assert_equal "application/json", @response.content_type
420
- assert_equal 204, @response.status
421
- assert_equal "", @response.body
422
- end
423
-
424
- def test_using_resource_for_put_with_xml_yields_unprocessable_entity_on_failure
425
- @request.accept = "application/xml"
426
- errors = { :name => :invalid }
427
- Customer.any_instance.stubs(:errors).returns(errors)
428
- put :using_resource
429
- assert_equal "application/xml", @response.content_type
430
- assert_equal 422, @response.status
431
- assert_equal errors.to_xml, @response.body
432
- assert_nil @response.location
433
- end
434
-
435
- def test_using_resource_for_put_with_json_yields_unprocessable_entity_on_failure
436
- @request.accept = "application/json"
437
- errors = { :name => :invalid }
438
- Customer.any_instance.stubs(:errors).returns(errors)
439
- put :using_resource
440
- assert_equal "application/json", @response.content_type
441
- assert_equal 422, @response.status
442
- errors = {:errors => errors}
443
- assert_equal errors.to_json, @response.body
444
- assert_nil @response.location
445
- end
446
-
447
- def test_using_resource_for_delete_with_html_redirects_on_success
448
- with_test_route_set do
449
- Customer.any_instance.stubs(:destroyed?).returns(true)
450
- delete :using_resource
451
- assert_equal "text/html", @response.content_type
452
- assert_equal 302, @response.status
453
- assert_equal "http://www.example.com/customers", @response.location
454
- end
455
- end
456
-
457
- def test_using_resource_for_delete_with_xml_yields_no_content_on_success
458
- Customer.any_instance.stubs(:destroyed?).returns(true)
459
- @request.accept = "application/xml"
460
- delete :using_resource
461
- assert_equal "application/xml", @response.content_type
462
- assert_equal 204, @response.status
463
- assert_equal "", @response.body
464
- end
465
-
466
- def test_using_resource_for_delete_with_json_yields_no_content_on_success
467
- Customer.any_instance.stubs(:destroyed?).returns(true)
468
- @request.accept = "application/json"
469
- delete :using_resource_with_json
470
- assert_equal "application/json", @response.content_type
471
- assert_equal 204, @response.status
472
- assert_equal "", @response.body
473
- end
474
-
475
- def test_using_resource_for_delete_with_html_redirects_on_failure
476
- with_test_route_set do
477
- errors = { :name => :invalid }
478
- Customer.any_instance.stubs(:errors).returns(errors)
479
- Customer.any_instance.stubs(:destroyed?).returns(false)
480
- delete :using_resource
481
- assert_equal "text/html", @response.content_type
482
- assert_equal 302, @response.status
483
- assert_equal "http://www.example.com/customers", @response.location
484
- end
485
- end
486
-
487
- def test_using_resource_with_parent_for_get
488
- @request.accept = "application/xml"
489
- get :using_resource_with_parent
490
- assert_equal "application/xml", @response.content_type
491
- assert_equal 200, @response.status
492
- assert_equal "<name>david</name>", @response.body
493
- end
494
-
495
- def test_using_resource_with_parent_for_post
496
- with_test_route_set do
497
- @request.accept = "application/xml"
498
-
499
- post :using_resource_with_parent
500
- assert_equal "application/xml", @response.content_type
501
- assert_equal 201, @response.status
502
- assert_equal "<name>david</name>", @response.body
503
- assert_equal "http://www.example.com/quiz_stores/11/customers/13", @response.location
504
-
505
- errors = { :name => :invalid }
506
- Customer.any_instance.stubs(:errors).returns(errors)
507
- post :using_resource
508
- assert_equal "application/xml", @response.content_type
509
- assert_equal 422, @response.status
510
- assert_equal errors.to_xml, @response.body
511
- assert_nil @response.location
512
- end
513
- end
514
-
515
- def test_using_resource_with_collection
516
- @request.accept = "application/xml"
517
- get :using_resource_with_collection
518
- assert_equal "application/xml", @response.content_type
519
- assert_equal 200, @response.status
520
- assert_match(/<name>david<\/name>/, @response.body)
521
- assert_match(/<name>jamis<\/name>/, @response.body)
522
- end
523
-
524
- def test_using_resource_with_action
525
- @controller.instance_eval do
526
- def render(params={})
527
- self.response_body = "#{params[:action]} - #{formats}"
528
- end
529
- end
530
-
531
- errors = { :name => :invalid }
532
- Customer.any_instance.stubs(:errors).returns(errors)
533
-
534
- post :using_resource_with_action
535
- assert_equal "foo - #{[:html].to_s}", @controller.response.body
536
- end
537
-
538
- def test_respond_as_responder_entry_point
539
- @request.accept = "text/html"
540
- get :using_responder_with_respond
541
- assert_equal "respond html", @response.body
542
-
543
- @request.accept = "application/xml"
544
- get :using_responder_with_respond
545
- assert_equal "respond xml", @response.body
546
- end
547
-
548
- def test_clear_respond_to
549
- @controller = InheritedRespondWithController.new
550
- @request.accept = "text/html"
551
- assert_raises(ActionController::UnknownFormat) do
552
- get :index
553
- end
554
- end
555
-
556
- def test_first_in_respond_to_has_higher_priority
557
- @controller = InheritedRespondWithController.new
558
- @request.accept = "*/*"
559
- get :index
560
- assert_equal "application/xml", @response.content_type
561
- assert_equal "<name>david</name>", @response.body
562
- end
563
-
564
- def test_block_inside_respond_with_is_rendered
565
- @controller = InheritedRespondWithController.new
566
- @request.accept = "application/json"
567
- get :index
568
- assert_equal "JSON", @response.body
569
- end
570
-
571
- def test_no_double_render_is_raised
572
- @request.accept = "text/html"
573
- assert_raise ActionView::MissingTemplate do
574
- get :using_resource
575
- end
576
- end
577
-
578
- def test_using_resource_with_status_and_location
579
- @request.accept = "text/html"
580
- post :using_resource_with_status_and_location
581
- assert @response.redirect?
582
- assert_equal "http://test.host/", @response.location
583
-
584
- @request.accept = "application/xml"
585
- get :using_resource_with_status_and_location
586
- assert_equal 201, @response.status
587
- end
588
-
589
- def test_using_resource_with_status_and_location_with_invalid_resource
590
- errors = { :name => :invalid }
591
- Customer.any_instance.stubs(:errors).returns(errors)
592
-
593
- @request.accept = "text/xml"
594
-
595
- post :using_resource_with_status_and_location
596
- assert_equal errors.to_xml, @response.body
597
- assert_equal 422, @response.status
598
- assert_equal nil, @response.location
599
-
600
- put :using_resource_with_status_and_location
601
- assert_equal errors.to_xml, @response.body
602
- assert_equal 422, @response.status
603
- assert_equal nil, @response.location
604
- end
605
-
606
- def test_using_invalid_resource_with_template
607
- errors = { :name => :invalid }
608
- Customer.any_instance.stubs(:errors).returns(errors)
609
-
610
- @request.accept = "text/xml"
611
-
612
- post :using_invalid_resource_with_template
613
- assert_equal errors.to_xml, @response.body
614
- assert_equal 422, @response.status
615
- assert_equal nil, @response.location
616
-
617
- put :using_invalid_resource_with_template
618
- assert_equal errors.to_xml, @response.body
619
- assert_equal 422, @response.status
620
- assert_equal nil, @response.location
621
- end
622
-
623
- def test_using_options_with_template
624
- @request.accept = "text/xml"
625
-
626
- post :using_options_with_template
627
- assert_equal "<customer-name>david</customer-name>", @response.body
628
- assert_equal 123, @response.status
629
- assert_equal "http://test.host/", @response.location
630
-
631
- put :using_options_with_template
632
- assert_equal "<customer-name>david</customer-name>", @response.body
633
- assert_equal 123, @response.status
634
- assert_equal "http://test.host/", @response.location
635
- end
636
-
637
- def test_using_resource_with_responder
638
- get :using_resource_with_responder
639
- assert_equal "Resource name is david", @response.body
640
- end
641
-
642
- def test_using_resource_with_set_responder
643
- RespondWithController.responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" }
644
- get :using_resource
645
- assert_equal "Resource name is david", @response.body
646
- ensure
647
- RespondWithController.responder = ActionController::Responder
648
- end
649
-
650
- def test_raises_missing_renderer_if_an_api_behavior_with_no_renderer
651
- @controller = CsvRespondWithController.new
652
- assert_raise ActionController::MissingRenderer do
653
- get :index, format: 'csv'
654
- end
655
- end
656
-
657
- def test_error_is_raised_if_no_respond_to_is_declared_and_respond_with_is_called
658
- @controller = EmptyRespondWithController.new
659
- @request.accept = "*/*"
660
- assert_raise RuntimeError do
661
- get :index
662
- end
663
- end
664
-
665
- private
666
- def with_test_route_set
667
- with_routing do |set|
668
- set.draw do
669
- resources :customers
670
- resources :quiz_stores do
671
- resources :customers
672
- end
673
- get ":controller/:action"
674
- end
675
- yield
676
- end
677
- end
678
- end
679
-
680
- class LocationsController < ApplicationController
681
- respond_to :html
682
- before_filter :set_resource
683
-
684
- def create
685
- respond_with @resource, location: -> { 'given_location' }
686
- end
687
-
688
- def update
689
- respond_with @resource, location: 'given_location'
690
- end
691
-
692
- def set_resource
693
- @resource = Address.new
694
- @resource.errors[:fail] << "FAIL" if params[:fail]
695
- end
696
- end
697
-
698
- class LocationResponderTest < ActionController::TestCase
699
- tests LocationsController
700
-
701
- def test_redirects_to_block_location_on_success
702
- post :create
703
- assert_redirected_to 'given_location'
704
- end
705
-
706
- def test_renders_page_on_fail
707
- post :create, fail: true
708
- assert @response.body.include?('new.html.erb')
709
- end
710
-
711
- def test_redirects_to_plain_string
712
- post :update
713
- assert_redirected_to 'given_location'
714
- end
715
- end