Sha256: 34abe0fda7ff63a47be8373fcc4814deec7aa68a668026d1c30a4ff94e3d5704

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module Shamu
  module Rails

    # Support JSON API responses with the standard rails `#respond_with` method.
    module JsonApiResponder

      # Render the response as JSON
      # @return [String]
      def to_json
        if has_errors?
          display_errors
        elsif get?
          display resource
        elsif put? || patch?
          display resource, :location => api_location
        elsif post?
          display resource, :status => :created, :location => api_location
        else
          head :no_content
        end
      end
      alias_method :to_json_api, :to_json

      protected

        # @visibility private
        def display( resource, given_options = {} )
          given_options.merge!( options )

          json =
            if resource.is_a?( Enumerable )
              controller.json_collection resource, **given_options
            else
              controller.json_resource resource, **given_options
            end

          super json, given_options
        end

        # @visibility private
        def display_errors
          controller.render format => controller.json_validation_errors( resource_errors ), :status => :unprocessable_entity # rubocop:disable Metrics/LineLength
        end

      private

        def validation_resource?( resource )
          resource.respond_to?( :valid? ) && resource.respond_to?( :errors )
        end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shamu-0.0.7 lib/shamu/rails/json_api_responder.rb
shamu-0.0.5 lib/shamu/rails/json_api_responder.rb