Class: RailsApiDocs::Inspectors::JsonRouteDetector::JsonActionVisitor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/rails-api-docs/inspectors/json_route_detector.rb

Overview

Walks every method definition and records the names of methods that contain a ‘render` call with a `json:` keyword. Nested calls inside `respond_to do |format| format.json { render json: … } end` are caught because `super` recurses into child nodes.

Instance Method Summary collapse

Constructor Details

#initializeJsonActionVisitor

Returns a new instance of JsonActionVisitor.



143
144
145
146
147
# File 'lib/rails-api-docs/inspectors/json_route_detector.rb', line 143

def initialize
  @actions        = Set.new
  @current_method = nil
  super
end

Instance Method Details

#actionsObject



149
150
151
# File 'lib/rails-api-docs/inspectors/json_route_detector.rb', line 149

def actions
  @actions.to_a
end

#visit_call_node(node) ⇒ Object



160
161
162
163
164
165
# File 'lib/rails-api-docs/inspectors/json_route_detector.rb', line 160

def visit_call_node(node)
  if @current_method && node.name == :render && renders_json?(node)
    @actions.add(@current_method)
  end
  super
end

#visit_def_node(node) ⇒ Object



153
154
155
156
157
158
# File 'lib/rails-api-docs/inspectors/json_route_detector.rb', line 153

def visit_def_node(node)
  prev = @current_method
  @current_method = node.name.to_s
  super
  @current_method = prev
end