Sha256: 0c5d0746b1185db80154309fffc7b59305791ee5f00845cc8244838a7da3d3a5

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

require 'rails/railtie'

module SimpleJsonapi
  module Rails
    class Railtie < ::Rails::Railtie
      initializer 'simple_jsonapi_rails.initialize' do
        Mime::Type.register(SimpleJsonapi::MIME_TYPE, :jsonapi)

        ActiveSupport.on_load(:action_controller) do
          ::ActionDispatch::Request.parameter_parsers[:jsonapi] = ->(raw_post) do
            ActiveSupport::JSON.decode(raw_post)
          end

          # In the renderers, `self` is the controller

          ::ActionController::Renderers.add(:jsonapi_resource) do |resource, options|
            self.content_type ||= Mime[:jsonapi]
            SimpleJsonapi.render_resource(resource, options).to_json
          end

          ::ActionController::Renderers.add(:jsonapi_resources) do |resources, options|
            self.content_type ||= Mime[:jsonapi]
            SimpleJsonapi.render_resources(resources, options).to_json
          end

          ::ActionController::Renderers.add(:jsonapi_errors) do |errors, options|
            self.content_type ||= Mime[:jsonapi]
            SimpleJsonapi.render_errors(errors, options).to_json
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simple_jsonapi_rails-1.2.0 lib/simple_jsonapi/rails/railtie.rb
simple_jsonapi_rails-1.1.1 lib/simple_jsonapi/rails/railtie.rb
simple_jsonapi_rails-1.1.0 lib/simple_jsonapi/rails/railtie.rb
simple_jsonapi_rails-1.0.0 lib/simple_jsonapi/rails/railtie.rb