api: refactor default options for xml responses.
In xml responses, if the result is an empty array we want the response to look like this: <posts type="array"/> not like this (the default): <nil-classes type="array"/> This refactors controllers so that this is done automatically instead of having to manually call `@things.to_xml(root: "things")` everywhere. We do this by overriding the behavior of `respond_with` in `ApplicationResponder` to set the `root` option by default in xml responses.
This commit is contained in:
12
app/logical/application_responder.rb
Normal file
12
app/logical/application_responder.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
# https://github.com/plataformatec/responders
|
||||
# https://github.com/plataformatec/responders/blob/master/lib/action_controller/responder.rb
|
||||
class ApplicationResponder < ActionController::Responder
|
||||
# this is called by respond_with for non-html, non-js responses.
|
||||
def to_format
|
||||
if format == :xml
|
||||
options[:root] ||= resource.table_name.dasherize if resource.respond_to?(:table_name)
|
||||
end
|
||||
|
||||
super
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user