Class: RubyLsp::ActionPolicy::Definition

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Inflector, Requests::Support::Common
Defined in:
lib/ruby_lsp/action_policy/addon.rb

Constant Summary collapse

POLICY_SUPERCLASSES =
["ApplicationPolicy", "ActionPolicy::Base"].freeze

Instance Method Summary collapse

Constructor Details

#initialize(response_builder, uri, node_context, dispatcher) ⇒ Definition

Returns a new instance of Definition.



33
34
35
36
37
38
39
40
41
# File 'lib/ruby_lsp/action_policy/addon.rb', line 33

def initialize(response_builder, uri, node_context, dispatcher)
  @response_builder = response_builder
  @node_context = node_context
  @uri = uri
  @path = uri.to_standardized_path
  @policy_rules_cache = {}

  dispatcher.register(self, :on_symbol_node_enter)
end

Instance Method Details

#on_symbol_node_enter(node) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_lsp/action_policy/addon.rb', line 43

def on_symbol_node_enter(node)
  return unless in_authorize_call?

  target = @node_context.call_node
  # authorization target is the first argument (if explicit)
  policy_class = find_policy_class(target.arguments&.child_node&.first)
  return unless policy_class

  policy_path = find_policy_file(policy_class)
  return unless policy_path

  ensure_policy_rules_cached(policy_path)
  add_definition(policy_path, node.value)
end