Sha256: dfcbc7854fcd1d2eb909a2153628af429f2c74c82cd81c8ff16acf965b957396

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module ActsAsApi

  # The methods of this module are included into the AbstractController::Rendering
  # module.
  module Rendering

    # Provides an alternative to the +render+ method used within the controller
    # to simply generate API outputs.
    #
    # The default Rails serializers are used to serialize the data.
    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


      api_format =  api_format_options.keys.first
      api_model =  api_format_options.values.first

      # set the params to render
      output_params = render_options

      # set the name of the root node - pluralize for arrays
      api_root_name = api_model.is_a?(Array) ? api_model.first.class.name.downcase.pluralize : api_model.class.name.downcase

      output_params[:root] = api_root_name

      api_response = api_model.as_api_response

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


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

      render output_params

    end

  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_api-0.0.2 lib/acts_as_api/rendering.rb