Skip to content

Commit

Permalink
added metadata support
Browse files Browse the repository at this point in the history
  • Loading branch information
coneybeare committed Sep 19, 2010
1 parent 008c869 commit b4decdf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.rdoc
Expand Up @@ -59,6 +59,24 @@ Now you just have to exchange the +render+ method in your controller for the +re

end

=== Metadata

You can include metadata elements in the response outside of your objects and collections. Typical usage for this is to include a total results count or page. Just pass in a :meta key with a hash of the meta items you want.

class CustomersController < ApplicationController

def index
@customers = Customer.paginate(:all, :page = params[:page])
metadata = { :total => @customers.total_entries, :page => params[:page] }
respond_to do |format|
format.html # show.html.erb
format.xml { render_for_api :xml => @customer, :meta => metadata }
format.json { render_for_api :json => @customer, :meta => metadata }
end
end

end

=== That's it!

Try it. The response should now look like this:
Expand Down
9 changes: 6 additions & 3 deletions lib/acts_as_api/rendering.rb
Expand Up @@ -12,14 +12,15 @@ def render_for_api(render_options)

# extract the api format and model
api_format_options = {}

ActsAsApi::ACCEPTED_API_FORMATS.each do |item|
if render_options.has_key?(item)
api_format_options[item] = render_options[item]
render_options.delete item
end
end


meta_hash = render_options[:meta] if render_options.has_key?(:meta)

api_format = api_format_options.keys.first
api_model = api_format_options.values.first
Expand Down Expand Up @@ -59,10 +60,12 @@ def render_for_api(render_options)

api_response = api_model.as_api_response

if ActsAsApi::ADD_ROOT_NODE_FOR.include? api_format
if meta_hash or ActsAsApi::ADD_ROOT_NODE_FOR.include? api_format
api_response = { api_root_name.to_sym => api_response}
end

api_response = meta_hash.merge api_response if meta_hash

# create the Hash as response
output_params[api_format] = api_response

Expand Down

0 comments on commit b4decdf

Please sign in to comment.