File: responder.rb

package info (click to toggle)
ruby-acts-as-api 1.0.1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, trixie
  • size: 624 kB
  • sloc: ruby: 2,366; makefile: 2
file content (34 lines) | stat: -rw-r--r-- 1,207 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module ActsAsApi
  # A custom Rails responder class to automatically use render_for_api in your
  # controller actions.
  #
  # Example:
  #
  #  class UsersController < ApplicationController
  #    # Set this controller to use our custom responder
  #    # (This could be done in a base controller class, if desired)
  #    self.responder = ActsAsApi::Responder
  #
  #    respond_to :json, :xml
  #
  #    def index
  #      @users = User.all
  #      respond_with @users, :api_template => :name_only
  #    end
  #  end
  #
  # The `:api_template` parameter is required so the responder knows which api template it should render.
  class Responder < ActionController::Responder
    # Overrides the base implementation of display, replacing it with
    # the render_for_api method whenever api_template is specified.
    def display(resource, given_options = {})
      api_template = options[:api_template]

      if api_template.nil? || !resource.respond_to?(:as_api_response)
        controller.render given_options.merge!(options).merge!(format => resource)
      else
        controller.render_for_api api_template, given_options.merge!(options).merge!(format => resource)
      end
    end
  end
end