Sha256: b508205e629a5530e589ee87b47c836e2aeb2ad627a391fed50707097059c14f

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true
# author: Vadim Shaveiko <@vshaveyko>
# :nodoc:
# :nodoc:
module RailsApiDoc::Controller::Response

  # :nodoc:
  class Rabl

    include RailsApiDoc::Controller::Response::Headers

    class << self

      attr_accessor :renderer

    end

    attr_reader :map

    # pass all controllers registered for api doc
    # TODO: add setting for displaying all from start
    def initialize(controllers)
      @controllers = controllers
      @routes = Rails.application.routes.set.anchored_routes.reject { |r| r.defaults[:internal] }
      @map = construct_controller_template_map
    end

    def load_template(ctrl, action)
      RablCompiler.new("#{ctrl.controller_path}/#{action}").compile_source
    end

    def action_route(ctrl, action)
      action_route = @map[ctrl][:routes].detect { |r| r.defaults[:action] == action }
      return unless action_route
      method = action_route.instance_variable_get(:@request_method_match)&.first&.name&.split('::')&.last
      route = action_route.path.spec.to_s
      [method, route].compact.join(' ')
    end

    private

    def construct_controller_template_map
      @controllers.each_with_object({}) do |ctrl, map|
        map[ctrl] = ctrl_actions(ctrl)
      end
    end

    def ctrl_actions(ctrl)
      routes = @routes.select do |route|
        route.defaults[:controller].in?([ctrl.controller_path, ctrl.controller_name])
      end
      actions = routes.map { |r| r.defaults[:action] }
      {
        routes: routes,
        actions: ctrl.action_methods
      }
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_api_documentation-0.2.3 lib/rails_api_doc/controller/response/rabl.rb