Class: Graphiti::Rails::Railtie
- Inherits:
-
Rails::Railtie
- Object
- Rails::Railtie
- Graphiti::Rails::Railtie
- Defined in:
- lib/graphiti/rails/railtie.rb
Overview
This Railtie exposes some configuration options:
-
‘config.graphiti.handled_exception_formats`, defaulting to `[:jsonapi]`. Formats in this list will always have their exceptions handled by Graphiti.
-
‘config.graphiti.respond_to_formats`, defaulting to `[:json, :jsonapi, :xml]`. When Responders is included in a controller it will respond to these mime types by default.
Constant Summary collapse
- MEDIA_TYPE =
from jsonapi-rails
'application/vnd.api+json'.freeze
- PARSER =
lambda do |body| data = JSON.parse(body) data[:format] = :jsonapi data.with_indifferent_access end
Instance Method Summary collapse
- #configure_endpoint_lookup ⇒ Object
-
#establish_concurrency ⇒ Object
Only run concurrently if our environment supports it.
- #register_mime_type ⇒ Object
- #register_parameter_parser ⇒ Object
- #register_renderers ⇒ Object
Instance Method Details
#configure_endpoint_lookup ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/graphiti/rails/railtie.rb', line 119 def configure_endpoint_lookup Graphiti.config.context_for_endpoint = ->(path, action) { method = :GET case action when :show then path = "#{path}/1" when :create then method = :POST when :update path = "#{path}/1" method = :PUT when :destroy path = "#{path}/1" method = :DELETE end route = begin ::Rails.application.routes.recognize_path(path, method: method) rescue nil end "#{route[:controller]}_controller".classify.safe_constantize if route } end |
#establish_concurrency ⇒ Object
Only run concurrently if our environment supports it
114 115 116 117 |
# File 'lib/graphiti/rails/railtie.rb', line 114 def establish_concurrency Graphiti.config.concurrency = !::Rails.env.test? && ::Rails.application.config.cache_classes end |
#register_mime_type ⇒ Object
69 70 71 |
# File 'lib/graphiti/rails/railtie.rb', line 69 def register_mime_type Mime::Type.register(MEDIA_TYPE, :jsonapi) end |
#register_parameter_parser ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/graphiti/rails/railtie.rb', line 73 def register_parameter_parser if ::Rails::VERSION::MAJOR >= 5 ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER else ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER end end |
#register_renderers ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/graphiti/rails/railtie.rb', line 81 def register_renderers ActiveSupport.on_load(:action_controller) do ::ActionController::Renderers.add(:jsonapi) do |proxy, | self.content_type ||= Mime[:jsonapi] # opts = {} # if respond_to?(:default_jsonapi_render_options) # opts = default_jsonapi_render_options # end if proxy.is_a?(Hash) # for destroy render(.merge(json: proxy)) else proxy.to_jsonapi() end end end ActiveSupport.on_load(:action_controller) do ActionController::Renderers.add(:jsonapi_errors) do |proxy, | self.content_type ||= Mime[:jsonapi] validation = GraphitiErrors::Validation::Serializer.new \ proxy.data, proxy.payload.relationships render \ json: {errors: validation.errors}, status: :unprocessable_entity end end end |