Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

support for collection_root_override #65

Closed
wants to merge 1 commit into from

2 participants

@mrzor

This patch adds support for (optional) overriding root json key for collections.

This allows us to keep the legacy acts_as_api behavior, back at the time where all collections came with a "records" root.

@fabrik42
Owner

Please add some tests and I will merge it :)

@fabrik42
Owner

any updates on this?

@mrzor
@fabrik42
Owner

Alright, take your time :)

@fabrik42
Owner

Uh, closing this for now. But I'd be happy to review it again if you added specs. :)

@fabrik42 fabrik42 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 27, 2012
  1. @mrzor
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 2 deletions.
  1. +7 −1 lib/acts_as_api/config.rb
  2. +5 −1 lib/acts_as_api/rendering.rb
View
8 lib/acts_as_api/config.rb
@@ -4,7 +4,7 @@ module Config
class << self
- attr_writer :accepted_api_formats, :dasherize_for, :include_root_in_json_collections, :add_root_node_for, :default_root, :allow_jsonp_callback, :add_http_status_to_jsonp_response
+ attr_writer :accepted_api_formats, :dasherize_for, :include_root_in_json_collections, :add_root_node_for, :default_root, :allow_jsonp_callback, :add_http_status_to_jsonp_response, :collection_root_override
# The accepted response formats
# Default is <tt>[:xml, :json]</tt>
@@ -51,6 +51,12 @@ def allow_jsonp_callback
def add_http_status_to_jsonp_response
@add_http_status_to_jsonp_response.nil? ? true : @add_http_status_to_jsonp_response
end
+
+ # If set, the JSON root for collections will be set to the override
+ # instead of the pluralized model name.
+ def collection_root_override
+ @collection_root_override || nil
+ end
end
end
View
6 lib/acts_as_api/rendering.rb
@@ -57,6 +57,10 @@ def render_for_api(api_template_or_options, render_options)
if api_model.is_a?(Array) || (defined?(ActiveRecord) && api_model.is_a?(ActiveRecord::Relation))
api_root_name = api_root_name.pluralize
+
+ if ActsAsApi::Config.collection_root_override
+ api_root_name = ActsAsApi::Config.collection_root_override
+ end
end
api_root_name = api_root_name.dasherize if ActsAsApi::Config.dasherize_for.include? api_format.to_sym
@@ -77,7 +81,7 @@ def render_for_api(api_template_or_options, render_options)
end
api_response = meta_hash.merge api_response if meta_hash
-
+
if ActsAsApi::Config.allow_jsonp_callback && params[:callback]
output_params[:callback] = params[:callback]
api_format = :acts_as_api_jsonp if ActsAsApi::Config.add_http_status_to_jsonp_response
Something went wrong with that request. Please try again.