Class: Graphiti::Railtie Deprecated

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/graphiti/railtie.rb

Overview

Deprecated.

Use graphiti-rails’s Graphiti::Rails::Railtie

Constant Summary collapse

PARSER =

from jsonapi-rails

lambda do |body|
  data = JSON.parse(body)
  data[:format] = :jsonapi
  data.with_indifferent_access
end

Instance Method Summary collapse

Instance Method Details

#configure_endpoint_lookupObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/graphiti/railtie.rb', line 98

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_concurrencyObject

Only run concurrently if our environment supports it



93
94
95
96
# File 'lib/graphiti/railtie.rb', line 93

def establish_concurrency
  Graphiti.config.concurrency = !::Rails.env.test? &&
    ::Rails.application.config.cache_classes
end

#register_parameter_parserObject



52
53
54
55
56
57
58
# File 'lib/graphiti/railtie.rb', line 52

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_renderersObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/graphiti/railtie.rb', line 60

def register_renderers
  ActiveSupport.on_load(:action_controller) do
    ::ActionController::Renderers.add(:jsonapi) do |proxy, options|
      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(options.merge(json: proxy))
      else
        proxy.to_jsonapi(options)
      end
    end
  end

  ActiveSupport.on_load(:action_controller) do
    ::ActionController::Renderers.add(:jsonapi_errors) do |proxy, options|
      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