Class: Solargraph::Arc::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/arc/relation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



4
5
6
# File 'lib/solargraph/arc/relation.rb', line 4

def self.instance
  @instance ||= self.new
end

Instance Method Details

#plural_association(ns, ast) ⇒ Object

TODO: handle custom class for relation



36
37
38
39
40
41
42
43
44
45
# File 'lib/solargraph/arc/relation.rb', line 36

def plural_association(ns, ast)
  relation_name = ast.children[2].children.first

  Util.build_public_method(
    ns,
    relation_name.to_s,
    types: ["ActiveRecord::Associations::CollectionProxy<#{relation_name.to_s.singularize.camelize}>"],
    location: Util.build_location(ast, ns.filename)
  )
end

#process(source_map, ns) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/solargraph/arc/relation.rb', line 8

def process(source_map, ns)
  return [] unless source_map.filename.include?("app/models")

  walker = Walker.from_source(source_map.source)
  pins   = []

  walker.on :send, [nil, :belongs_to] do |ast|
    pins << singular_association(ns, ast)
  end

  walker.on :send, [nil, :has_one] do |ast|
    pins << singular_association(ns, ast)
  end

  walker.on :send, [nil, :has_many] do |ast|
    pins << plural_association(ns, ast)
  end

  walker.on :send, [nil, :has_and_belongs_to_many] do |ast|
    pins << plural_association(ns, ast)
  end

  walker.walk
  Solargraph.logger.debug("[ARC][Relation] added #{pins.map(&:name)} to #{ns.path}") if pins.any?
  pins
end

#singular_association(ns, ast) ⇒ Object

TODO: handle custom class for relation



48
49
50
51
52
53
54
55
56
57
# File 'lib/solargraph/arc/relation.rb', line 48

def singular_association(ns, ast)
  relation_name = ast.children[2].children.first

  Util.build_public_method(
    ns,
    relation_name.to_s,
    types: [relation_name.to_s.camelize],
    location: Util.build_location(ast, ns.filename)
  )
end