Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 431 lines (408 sloc) 16.631 kb
ddfc072 Added AbstractController::Collector.
José Valim and Mikel Lindsaar authored
1 require 'abstract_controller/collector'
2
1c16649 @dhh Added better support for using the same actions to output for differe…
dhh authored
3 module ActionController #:nodoc:
c59456a @joliss removed :nodoc: from ActionController::MimeResponds
joliss authored
4 module MimeResponds
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
5 extend ActiveSupport::Concern
6
7 included do
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
8 class_attribute :responder, :mimes_for_respond_to
2e37eff @josevalim Unify class_inheritable_accessor and extlib_inheritable_accessor and …
josevalim authored
9 self.responder = ActionController::Responder
fa0cf66 @josevalim Add a couple more tests to respond_with.
josevalim authored
10 clear_respond_to
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
11 end
12
13 module ClassMethods
79f9fcd @wincent doc: cleanup respond_to documentation
wincent authored
14 # Defines mime types that are rendered by default when invoking
15 # <tt>respond_with</tt>.
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
16 #
17 # respond_to :html, :xml, :json
18 #
79f9fcd @wincent doc: cleanup respond_to documentation
wincent authored
19 # Specifies that all actions in the controller respond to requests
20 # for <tt>:html</tt>, <tt>:xml</tt> and <tt>:json</tt>.
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
21 #
79f9fcd @wincent doc: cleanup respond_to documentation
wincent authored
22 # To specify on per-action basis, use <tt>:only</tt> and
23 # <tt>:except</tt> with an array of actions or a single action:
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
24 #
25 # respond_to :html
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
26 # respond_to :xml, :json, except: [ :edit ]
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
27 #
79f9fcd @wincent doc: cleanup respond_to documentation
wincent authored
28 # This specifies that all actions respond to <tt>:html</tt>
29 # and all actions except <tt>:edit</tt> respond to <tt>:xml</tt> and
30 # <tt>:json</tt>.
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
31 #
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
32 # respond_to :json, only: :create
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
33 #
79f9fcd @wincent doc: cleanup respond_to documentation
wincent authored
34 # This specifies that the <tt>:create</tt> action and no other responds
5850f16 @fxn removes the RJS template handler
fxn authored
35 # to <tt>:json</tt>.
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
36 def respond_to(*mimes)
37 options = mimes.extract_options!
38
50d262f @avakhov Support symbol and string actions in AC#respond_to options
avakhov authored
39 only_actions = Array(options.delete(:only)).map(&:to_s)
40 except_actions = Array(options.delete(:except)).map(&:to_s)
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
41
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
42 new = mimes_for_respond_to.dup
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
43 mimes.each do |mime|
44 mime = mime.to_sym
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
45 new[mime] = {}
46 new[mime][:only] = only_actions unless only_actions.empty?
47 new[mime][:except] = except_actions unless except_actions.empty?
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
48 end
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
49 self.mimes_for_respond_to = new.freeze
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
50 end
51
79f9fcd @wincent doc: cleanup respond_to documentation
wincent authored
52 # Clear all mime types in <tt>respond_to</tt>.
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
53 #
fa0cf66 @josevalim Add a couple more tests to respond_with.
josevalim authored
54 def clear_respond_to
d8996d3 @vatrai remove usages of AS::OrderedHash
vatrai authored
55 self.mimes_for_respond_to = Hash.new.freeze
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
56 end
57 end
3f445b3 @josevalim Refactor Responder to only calculate available mime types. Those are …
josevalim authored
58
01f032f Added responds_to to new base.
Yehuda Katz + Carl Lerche authored
59 # Without web-service support, an action which collects the data for displaying a list of people
60 # might look something like this:
61 #
62 # def index
1a6c4c8 @willcannings Correct deprecated AR usage in ActionController::MimeResponds documen…
willcannings authored
63 # @people = Person.all
01f032f Added responds_to to new base.
Yehuda Katz + Carl Lerche authored
64 # end
65 #
66 # Here's the same action, with web-service support baked in:
67 #
68 # def index
1a6c4c8 @willcannings Correct deprecated AR usage in ActionController::MimeResponds documen…
willcannings authored
69 # @people = Person.all
01f032f Added responds_to to new base.
Yehuda Katz + Carl Lerche authored
70 #
71 # respond_to do |format|
72 # format.html
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
73 # format.xml { render xml: @people }
01f032f Added responds_to to new base.
Yehuda Katz + Carl Lerche authored
74 # end
75 # end
76 #
77 # What that says is, "if the client wants HTML in response to this action, just respond as we
78 # would have before, but if the client wants XML, return them the list of people in XML format."
79 # (Rails determines the desired response format from the HTTP Accept header submitted by the client.)
80 #
81 # Supposing you have an action that adds a new person, optionally creating their company
82 # (by name) if it does not already exist, without web-services, it might look like this:
83 #
84 # def create
85 # @company = Company.find_or_create_by_name(params[:company][:name])
86 # @person = @company.people.create(params[:person])
87 #
88 # redirect_to(person_list_url)
89 # end
90 #
91 # Here's the same action, with web-service support baked in:
92 #
93 # def create
94 # company = params[:person].delete(:company)
95 # @company = Company.find_or_create_by_name(company[:name])
96 # @person = @company.people.create(params[:person])
97 #
98 # respond_to do |format|
99 # format.html { redirect_to(person_list_url) }
100 # format.js
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
101 # format.xml { render xml: @person.to_xml(include: @company) }
01f032f Added responds_to to new base.
Yehuda Katz + Carl Lerche authored
102 # end
103 # end
104 #
5850f16 @fxn removes the RJS template handler
fxn authored
105 # If the client wants HTML, we just redirect them back to the person list. If they want JavaScript,
106 # then it is an Ajax request and we render the JavaScript template associated with this action.
01f032f Added responds_to to new base.
Yehuda Katz + Carl Lerche authored
107 # Lastly, if the client wants XML, we render the created person as XML, but with a twist: we also
108 # include the person's company in the rendered XML, so you get something like this:
109 #
110 # <person>
111 # <id>...</id>
112 # ...
113 # <company>
114 # <id>...</id>
115 # <name>...</name>
116 # ...
117 # </company>
118 # </person>
119 #
120 # Note, however, the extra bit at the top of that action:
121 #
122 # company = params[:person].delete(:company)
123 # @company = Company.find_or_create_by_name(company[:name])
124 #
125 # This is because the incoming XML document (if a web-service request is in process) can only contain a
126 # single root-node. So, we have to rearrange things so that the request looks like this (url-encoded):
127 #
128 # person[name]=...&person[company][name]=...&...
129 #
130 # And, like this (xml-encoded):
131 #
132 # <person>
133 # <name>...</name>
134 # <company>
135 # <name>...</name>
136 # </company>
137 # </person>
138 #
139 # In other words, we make the request so that it operates on a single entity's person. Then, in the action,
140 # we extract the company data from the request, find or create the company, and then create the new person
141 # with the remaining data.
142 #
143 # Note that you can define your own XML parameter parser which would allow you to describe multiple entities
144 # in a single request (i.e., by wrapping them all in a single root node), but if you just go with the flow
145 # and accept Rails' defaults, life will be much easier.
146 #
147 # If you need to use a MIME type which isn't supported by default, you can register your own handlers in
7e075e6 Fixed many references to the old config/environment.rb and Rails::Ini…
Benjamin Quorning authored
148 # config/initializers/mime_types.rb as follows.
01f032f Added responds_to to new base.
Yehuda Katz + Carl Lerche authored
149 #
150 # Mime::Type.register "image/jpg", :jpg
09de34c @josevalim Added respond_with.
josevalim authored
151 #
152 # Respond to also allows you to specify a common block for different formats by using any:
153 #
154 # def index
1a6c4c8 @willcannings Correct deprecated AR usage in ActionController::MimeResponds documen…
willcannings authored
155 # @people = Person.all
09de34c @josevalim Added respond_with.
josevalim authored
156 #
157 # respond_to do |format|
158 # format.html
159 # format.any(:xml, :json) { render request.format.to_sym => @people }
160 # end
161 # end
162 #
163 # In the example above, if the format is xml, it will render:
164 #
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
165 # render xml: @people
09de34c @josevalim Added respond_with.
josevalim authored
166 #
167 # Or if the format is json:
168 #
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
169 # render json: @people
09de34c @josevalim Added respond_with.
josevalim authored
170 #
171 # Since this is a common pattern, you can use the class method respond_to
172 # with the respond_with method to have the same results:
173 #
174 # class PeopleController < ApplicationController
175 # respond_to :html, :xml, :json
176 #
177 # def index
1a6c4c8 @willcannings Correct deprecated AR usage in ActionController::MimeResponds documen…
willcannings authored
178 # @people = Person.all
6ac56ac @vijaydev fix incorrect code example
vijaydev authored
179 # respond_with(@people)
09de34c @josevalim Added respond_with.
josevalim authored
180 # end
181 # end
182 #
3561e85 @fxn copy-edits 9674d2c
fxn authored
183 # Be sure to check the documentation of +respond_with+ and
184 # <tt>ActionController::MimeResponds.respond_to</tt> for more examples.
3f445b3 @josevalim Refactor Responder to only calculate available mime types. Those are …
josevalim authored
185 def respond_to(*mimes, &block)
67b2d08 @josevalim Ensure that the proper accept header value is set during tests.
josevalim authored
186 raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
3f78de6 @josevalim Ensure that blocks are also handled inside the responder.
josevalim authored
187
3def1c8 @sikachu Fix override API response bug in respond_with
sikachu authored
188 if collector = retrieve_collector_from_mimes(mimes, &block)
20bc3d4 @josevalim Clean up a bit default_response handling and cache format negotiation.
josevalim authored
189 response = collector.response
b90d24b @spastorino Avoid ImplicitRender just call render directly
spastorino authored
190 response ? response.call : render({})
3f78de6 @josevalim Ensure that blocks are also handled inside the responder.
josevalim authored
191 end
09de34c @josevalim Added respond_with.
josevalim authored
192 end
672941d @jeremy Introduce a default respond_to block for custom types. Closes #8174.
jeremy authored
193
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
194 # For a given controller action, respond_with generates an appropriate
195 # response based on the mime-type requested by the client.
5b7e81e @josevalim Allow respond_with to deal with http verb accordingly.
josevalim authored
196 #
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
197 # If the method is called with just a resource, as in this example -
5b7e81e @josevalim Allow respond_with to deal with http verb accordingly.
josevalim authored
198 #
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
199 # class PeopleController < ApplicationController
200 # respond_to :html, :xml, :json
201 #
202 # def index
203 # @people = Person.all
204 # respond_with @people
205 # end
09de34c @josevalim Added respond_with.
josevalim authored
206 # end
207 #
9e98d2d @MarkMT Revised comments for respond_with
MarkMT authored
208 # then the mime-type of the response is typically selected based on the
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
209 # request's Accept header and the set of available formats declared
9e98d2d @MarkMT Revised comments for respond_with
MarkMT authored
210 # by previous calls to the controller's class method +respond_to+. Alternatively
211 # the mime-type can be selected by explicitly setting <tt>request.format</tt> in
212 # the controller.
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
213 #
d25d3fb @MarkMT respond_with description: changed 'response' to 'format'
MarkMT authored
214 # If an acceptable format is not identified, the application returns a
9e98d2d @MarkMT Revised comments for respond_with
MarkMT authored
215 # '406 - not acceptable' status. Otherwise, the default response is to render
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
216 # a template named after the current action and the selected format,
217 # e.g. <tt>index.html.erb</tt>. If no template is available, the behavior
218 # depends on the selected format:
219 #
9e98d2d @MarkMT Revised comments for respond_with
MarkMT authored
220 # * for an html response - if the request method is +get+, an exception
221 # is raised but for other requests such as +post+ the response
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
222 # depends on whether the resource has any validation errors (i.e.
223 # assuming that an attempt has been made to save the resource,
224 # e.g. by a +create+ action) -
9e98d2d @MarkMT Revised comments for respond_with
MarkMT authored
225 # 1. If there are no errors, i.e. the resource
226 # was saved successfully, the response +redirect+'s to the resource
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
227 # i.e. its +show+ action.
228 # 2. If there are validation errors, the response
9e98d2d @MarkMT Revised comments for respond_with
MarkMT authored
229 # renders a default action, which is <tt>:new</tt> for a
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
230 # +post+ request or <tt>:edit</tt> for +put+.
231 # Thus an example like this -
232 #
233 # respond_to :html, :xml
234 #
235 # def create
236 # @user = User.new(params[:user])
237 # flash[:notice] = 'User was successfully created.' if @user.save
238 # respond_with(@user)
239 # end
240 #
241 # is equivalent, in the absence of <tt>create.html.erb</tt>, to -
242 #
243 # def create
244 # @user = User.new(params[:user])
245 # respond_to do |format|
246 # if @user.save
247 # flash[:notice] = 'User was successfully created.'
248 # format.html { redirect_to(@user) }
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
249 # format.xml { render xml: @user }
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
250 # else
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
251 # format.html { render action: "new" }
252 # format.xml { render xml: @user }
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
253 # end
254 # end
255 # end
256 #
257 # * for a javascript request - if the template isn't found, an exception is
258 # raised.
259 # * for other requests - i.e. data formats such as xml, json, csv etc, if
9e98d2d @MarkMT Revised comments for respond_with
MarkMT authored
260 # the resource passed to +respond_with+ responds to <code>to_<format></code>,
261 # the method attempts to render the resource in the requested format
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
262 # directly, e.g. for an xml request, the response is equivalent to calling
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
263 # <code>render xml: resource</code>.
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
264 #
265 # === Nested resources
266 #
267 # As outlined above, the +resources+ argument passed to +respond_with+
9e98d2d @MarkMT Revised comments for respond_with
MarkMT authored
268 # can play two roles. It can be used to generate the redirect url
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
269 # for successful html requests (e.g. for +create+ actions when
270 # no template exists), while for formats other than html and javascript
271 # it is the object that gets rendered, by being converted directly to the
272 # required format (again assuming no template exists).
273 #
9e98d2d @MarkMT Revised comments for respond_with
MarkMT authored
274 # For redirecting successful html requests, +respond_with+ also supports
275 # the use of nested resources, which are supplied in the same way as
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
276 # in <code>form_for</code> and <code>polymorphic_url</code>. For example -
277 #
278 # def create
279 # @project = Project.find(params[:project_id])
280 # @task = @project.comments.build(params[:task])
281 # flash[:notice] = 'Task was successfully created.' if @task.save
282 # respond_with(@project, @task)
283 # end
284 #
285 # This would cause +respond_with+ to redirect to <code>project_task_url</code>
286 # instead of <code>task_url</code>. For request formats other than html or
287 # javascript, if multiple resources are passed in this way, it is the last
9e98d2d @MarkMT Revised comments for respond_with
MarkMT authored
288 # one specified that is rendered.
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
289 #
290 # === Customizing response behavior
291 #
292 # Like +respond_to+, +respond_with+ may also be called with a block that
293 # can be used to overwrite any of the default responses, e.g. -
f59984c @josevalim Add nagivational behavior to respond_with.
josevalim authored
294 #
aebbbc7 @willcannings Fix respond_with example code so it makes sense
willcannings authored
295 # def create
296 # @user = User.new(params[:user])
1fd65c8 @josevalim Encapsulate respond_with behavior in a presenter.
josevalim authored
297 # flash[:notice] = "User was successfully created." if @user.save
5b7e81e @josevalim Allow respond_with to deal with http verb accordingly.
josevalim authored
298 #
1fd65c8 @josevalim Encapsulate respond_with behavior in a presenter.
josevalim authored
299 # respond_with(@user) do |format|
300 # format.html { render }
301 # end
7034272 @josevalim Add destroyed? to ActiveRecord, include tests for polymorphic urls fo…
josevalim authored
302 # end
303 #
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
304 # The argument passed to the block is an ActionController::MimeResponds::Collector
305 # object which stores the responses for the formats defined within the
306 # block. Note that formats with responses defined explicitly in this way
307 # do not have to first be declared using the class method +respond_to+.
308 #
309 # Also, a hash passed to +respond_with+ immediately after the specified
9e98d2d @MarkMT Revised comments for respond_with
MarkMT authored
310 # resource(s) is interpreted as a set of options relevant to all
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
311 # formats. Any option accepted by +render+ can be used, e.g.
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
312 # respond_with @people, status: 200
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
313 # However, note that these options are ignored after an unsuccessful attempt
314 # to save a resource, e.g. when automatically rendering <tt>:new</tt>
315 # after a post request.
316 #
317 # Two additional options are relevant specifically to +respond_with+ -
318 # 1. <tt>:location</tt> - overwrites the default redirect location used after
319 # a successful html +post+ request.
320 # 2. <tt>:action</tt> - overwrites the default render action used after an
321 # unsuccessful html +post+ request.
4f9047e @josevalim Ensure collections are not treated as nested resources.
josevalim authored
322 def respond_with(*resources, &block)
d2e7c1b @josevalim Raise an error if respond_with is invoked and no format is declared.
josevalim authored
323 raise "In order to use respond_with, first you need to declare the formats your " <<
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
324 "controller responds to in the class level" if self.class.mimes_for_respond_to.empty?
d2e7c1b @josevalim Raise an error if respond_with is invoked and no format is declared.
josevalim authored
325
3def1c8 @sikachu Fix override API response bug in respond_with
sikachu authored
326 if collector = retrieve_collector_from_mimes(&block)
b1667c7 @chriseppstein Correctly handle the case of an API response that returns a hash by t…
chriseppstein authored
327 options = resources.size == 1 ? {} : resources.extract_options!
284041c @sikachu Always passing a respond block from to responder
sikachu authored
328 options[:default_response] = collector.response
329 (options.delete(:responder) || self.class.responder).call(self, resources, options)
684a6b3 @josevalim Attempt to render the template inside the responder, so it can be use…
josevalim authored
330 end
aed135d @josevalim Renamed presenter to renderer, added some documentation and defined i…
josevalim authored
331 end
7a4a679 @josevalim Remove any resource logic from respond_to.
josevalim authored
332
333 protected
334
09de34c @josevalim Added respond_with.
josevalim authored
335 # Collect mimes declared in the class method respond_to valid for the
336 # current action.
337 def collect_mimes_from_class_level #:nodoc:
50d262f @avakhov Support symbol and string actions in AC#respond_to options
avakhov authored
338 action = action_name.to_s
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
339
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
340 self.class.mimes_for_respond_to.keys.select do |mime|
341 config = self.class.mimes_for_respond_to[mime]
09de34c @josevalim Added respond_with.
josevalim authored
342
343 if config[:except]
447b6a4 @fxn removes usage of Object#in? from the code base (the method remains de…
fxn authored
344 !config[:except].include?(action)
09de34c @josevalim Added respond_with.
josevalim authored
345 elsif config[:only]
447b6a4 @fxn removes usage of Object#in? from the code base (the method remains de…
fxn authored
346 config[:only].include?(action)
09de34c @josevalim Added respond_with.
josevalim authored
347 else
348 true
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
349 end
350 end
09de34c @josevalim Added respond_with.
josevalim authored
351 end
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
352
fb094ac @MarkMT Updated description of #retrieve_collector_from_mimes
MarkMT authored
353 # Returns a Collector object containing the appropriate mime-type response
354 # for the current request, based on the available responses defined by a block.
355 # In typical usage this is the block passed to +respond_with+ or +respond_to+.
356 #
357 # Sends :not_acceptable to the client and returns nil if no suitable format
358 # is available.
3def1c8 @sikachu Fix override API response bug in respond_with
sikachu authored
359 def retrieve_collector_from_mimes(mimes=nil, &block) #:nodoc:
d2e7c1b @josevalim Raise an error if respond_with is invoked and no format is declared.
josevalim authored
360 mimes ||= collect_mimes_from_class_level
20bc3d4 @josevalim Clean up a bit default_response handling and cache format negotiation.
josevalim authored
361 collector = Collector.new(mimes)
3f78de6 @josevalim Ensure that blocks are also handled inside the responder.
josevalim authored
362 block.call(collector) if block_given?
20bc3d4 @josevalim Clean up a bit default_response handling and cache format negotiation.
josevalim authored
363 format = collector.negotiate_format(request)
684a6b3 @josevalim Attempt to render the template inside the responder, so it can be use…
josevalim authored
364
20bc3d4 @josevalim Clean up a bit default_response handling and cache format negotiation.
josevalim authored
365 if format
f2c0a35 @josevalim Finish cleaning up rendering stack from views and move assigns evalua…
josevalim authored
366 self.content_type ||= format.to_s
157ea76 @spastorino format lookup for partials is derived from the format in which the te…
spastorino authored
367 lookup_context.formats = [format.to_sym]
7638004 @josevalim Set the rendered_format on respond_to.
josevalim authored
368 lookup_context.rendered_format = lookup_context.formats.first
3def1c8 @sikachu Fix override API response bug in respond_with
sikachu authored
369 collector
684a6b3 @josevalim Attempt to render the template inside the responder, so it can be use…
josevalim authored
370 else
6471ced @ssoroka remove .new from raise ActionController::UnknownFormat
ssoroka authored
371 raise ActionController::UnknownFormat
684a6b3 @josevalim Attempt to render the template inside the responder, so it can be use…
josevalim authored
372 end
373 end
374
98b4ef7 @MarkMT Revised description for responds_with
MarkMT authored
375 # A container for responses available from the current controller for
376 # requests for different mime-types sent to a particular action.
4cb71ea @MarkMT Added documentation for the ActionController::MimeResponds::Collector…
MarkMT authored
377 #
378 # The public controller methods +respond_with+ and +respond_to+ may be called
379 # with a block that is used to define responses to different mime-types, e.g.
380 # for +respond_to+ :
381 #
382 # respond_to do |format|
383 # format.html
62f273b @AvnerCohen Multiple changes to 1,9 hash syntax
AvnerCohen authored
384 # format.xml { render xml: @people }
4cb71ea @MarkMT Added documentation for the ActionController::MimeResponds::Collector…
MarkMT authored
385 # end
386 #
387 # In this usage, the argument passed to the block (+format+ above) is an
388 # instance of the ActionController::MimeResponds::Collector class. This
389 # object serves as a container in which available responses can be stored by
390 # calling any of the dynamically generated, mime-type-specific methods such
391 # as +html+, +xml+ etc on the Collector. Each response is represented by a
392 # corresponding block if present.
393 #
394 # A subsequent call to #negotiate_format(request) will enable the Collector
395 # to determine which specific mime-type it should respond with for the current
396 # request, with this response then being accessible by calling #response.
397 class Collector
ddfc072 Added AbstractController::Collector.
José Valim and Mikel Lindsaar authored
398 include AbstractController::Collector
20bc3d4 @josevalim Clean up a bit default_response handling and cache format negotiation.
josevalim authored
399 attr_accessor :order, :format
3f445b3 @josevalim Refactor Responder to only calculate available mime types. Those are …
josevalim authored
400
20bc3d4 @josevalim Clean up a bit default_response handling and cache format negotiation.
josevalim authored
401 def initialize(mimes)
402 @order, @responses = [], {}
74049b5 @spastorino implicit self here
spastorino authored
403 mimes.each { |mime| send(mime) }
7af12d0 @dhh Added synonym and custom type handling to respond_to [DHH]
dhh authored
404 end
6480d49 @jamis Add MimeResponds::Responder#any for managing multiple types with iden…
jamis authored
405
406 def any(*args, &block)
011e469 @lifo Make MimeResponds::Responder#any work without explicit types. Closes …
lifo authored
407 if args.any?
408 args.each { |type| send(type, &block) }
409 else
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored
410 custom(Mime::ALL, &block)
011e469 @lifo Make MimeResponds::Responder#any work without explicit types. Closes …
lifo authored
411 end
6dea52c @dhh Finish custom handling [DHH]
dhh authored
412 end
7e280c3 @josevalim Remove Mime::ALL from Mime::SET.
josevalim authored
413 alias :all :any
3f445b3 @josevalim Refactor Responder to only calculate available mime types. Those are …
josevalim authored
414
415 def custom(mime_type, &block)
6e55916 @spastorino There's no need for ternary op here
spastorino authored
416 mime_type = Mime::Type.lookup(mime_type.to_s) unless mime_type.is_a?(Mime::Type)
3f445b3 @josevalim Refactor Responder to only calculate available mime types. Those are …
josevalim authored
417 @order << mime_type
418 @responses[mime_type] ||= block
6dc1288 @wycats Remove method missing use in respond_to
wycats authored
419 end
672941d @jeremy Introduce a default respond_to block for custom types. Closes #8174.
jeremy authored
420
20bc3d4 @josevalim Clean up a bit default_response handling and cache format negotiation.
josevalim authored
421 def response
422 @responses[format] || @responses[Mime::ALL]
423 end
424
425 def negotiate_format(request)
426 @format = request.negotiate_mime(order)
3f445b3 @josevalim Refactor Responder to only calculate available mime types. Those are …
josevalim authored
427 end
1c16649 @dhh Added better support for using the same actions to output for differe…
dhh authored
428 end
429 end
0ee1cb2 @jeremy Ruby 1.9 compat, consistent load paths
jeremy authored
430 end
Something went wrong with that request. Please try again.