Class: Solargraph::Rails::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/rails/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



4
5
6
# File 'lib/solargraph/rails/model.rb', line 4

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

.valid_filename?(filename) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/solargraph/rails/model.rb', line 8

def self.valid_filename?(filename)
  filename.include?('app/models')
end

Instance Method Details

#extract_custom_class_name(ast) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/solargraph/rails/model.rb', line 92

def extract_custom_class_name(ast)
  options = ast.children[3..-1].find { |n| n.type == :hash }
  return unless options

  class_name_pair =
    options.children.find do |n|
      n.children[0].deconstruct == %i[sym class_name] &&
        n.children[1].type == :str
    end
  class_name_pair && class_name_pair.children.last.children.last
end

#plural_association(ns, ast) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/solargraph/rails/model.rb', line 65

def plural_association(ns, ast)
  relation_name = ast.children[2].children.first
  class_name =
    extract_custom_class_name(ast) ||
      relation_name.to_s.singularize.camelize

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

#process(source_map, ns) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/solargraph/rails/model.rb', line 12

def process(source_map, ns)
  return [] unless self.class.valid_filename?(source_map.filename)

  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.on :send, [nil, :scope] do |ast|
    next if ast.children[2].nil?
    name = ast.children[2].children.last

    method_pin =
      Util.build_public_method(
        ns,
        name.to_s,
        types: ns.return_type.map(&:tag),
        scope: :class,
        location: Util.build_location(ast, ns.filename)
      )

    if ast.children.last.type == :block
      location = ast.children.last.location
      block_pin =
        source_map.locate_block_pin(location.line, location.column)
      method_pin.parameters.concat(block_pin.parameters.clone)
    end
    pins << method_pin
  end

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

#singular_association(ns, ast) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/solargraph/rails/model.rb', line 79

def singular_association(ns, ast)
  relation_name = ast.children[2].children.first
  class_name =
    extract_custom_class_name(ast) || relation_name.to_s.camelize

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