Class: ReferenceExtractor::Internal::AssociationInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/reference_extractor/internal/association_inspector.rb

Overview

Extracts the implicit constant reference from an Active Record association

Constant Summary collapse

RAILS_ASSOCIATIONS =
[
  :belongs_to,
  :has_many,
  :has_one,
  :has_and_belongs_to_many
].to_set
DEFAULT_EXCLUDED_FILES =
Set.new([
  "spec/factories/**",
  "test/factories/**"
])

Instance Method Summary collapse

Constructor Details

#initialize(inflector: ActiveSupport::Inflector, custom_associations: Set.new, excluded_files: DEFAULT_EXCLUDED_FILES) ⇒ AssociationInspector

Returns a new instance of AssociationInspector.



19
20
21
22
23
24
25
26
27
# File 'lib/reference_extractor/internal/association_inspector.rb', line 19

def initialize(
  inflector: ActiveSupport::Inflector,
  custom_associations: Set.new,
  excluded_files: DEFAULT_EXCLUDED_FILES
)
  @inflector = inflector
  @associations = RAILS_ASSOCIATIONS + custom_associations
  @excluded_files = excluded_files
end

Instance Method Details

#constant_name_from_node(node, ancestors:, relative_path:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/reference_extractor/internal/association_inspector.rb', line 29

def constant_name_from_node(node, ancestors:, relative_path:)
  return unless NodeHelpers.method_call?(node)
  return if excluded?(relative_path)
  return unless association?(node)

  arguments = NodeHelpers.method_arguments(node)
  association_name = association_name(arguments)
  return unless association_name

  if (class_name_node = custom_class_name(arguments))
    return unless NodeHelpers.string?(class_name_node)

    NodeHelpers.literal_value(class_name_node)
  else
    @inflector.classify(association_name.to_s)
  end
end