Sha256: 6ff4a4d1d9062048cdc77b04ec73485ba07ec6f986dbb93cd1add2913b498fc2

Contents?: true

Size: 1.39 KB

Versions: 17

Compression:

Stored size: 1.39 KB

Contents

require 'fun_with_json_api/controller_methods'
require 'fun_with_json_api/action_controller_extensions/serialization'

Mime::Type.register FunWithJsonApi::MEDIA_TYPE, :json_api

module FunWithJsonApi
  # Mountable engine for fun with json_api
  class Railtie < Rails::Railtie
    initializer :register_json_api_mime_type do
      parsers =
        if Rails::VERSION::MAJOR >= 5
          ActionDispatch::Http::Parameters
        else
          ActionDispatch::ParamsParser
        end

      parsers::DEFAULT_PARSERS[Mime::Type.lookup(FunWithJsonApi::MEDIA_TYPE)] = lambda do |body|
        data = JSON.parse(body)
        data = { _json: data } unless data.is_a?(Hash)
        data.with_indifferent_access
      end
    end
    initializer :register_json_api_renderer do
      ActionController::Renderers.add :json_api do |json, options|
        json = json.to_json(options) unless json.is_a?(String)
        self.content_type ||= Mime::Type.lookup(FunWithJsonApi::MEDIA_TYPE)
        json
      end
    end
    initializer :register_json_api_serializers do
      ActiveSupport.on_load(:action_controller) do
        include(FunWithJsonApi::ActionControllerExtensions::Serialization)
      end
    end
    initializer :add_json_api_locales do
      translations = File.expand_path('../../../config/locales/**/*.{rb,yml}', __FILE__)
      Dir.glob(translations) { |f| config.i18n.load_path << f.to_s }
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
fun_with_json_api-0.0.10.4 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.10.3 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.10.2 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.10.1 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.10 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.9 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.8.2 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.8.1 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.8 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.7 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.6.pre.alpha.2 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.6.pre.alpha.1 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.6.1 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.6 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.5 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.4 lib/fun_with_json_api/railtie.rb
fun_with_json_api-0.0.3 lib/fun_with_json_api/railtie.rb