Module: RailVerdict::RailsContext::Resolvers::SchemaResolver

Defined in:
lib/rail_verdict/rails_context/resolvers/schema_resolver.rb

Class Method Summary collapse

Class Method Details

.for(repository_root:, kind:, constant:, path:) ⇒ Object



7
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
34
35
36
37
38
39
# File 'lib/rail_verdict/rails_context/resolvers/schema_resolver.rb', line 7

def self.for(repository_root:, kind:, constant:, path:)
  return [] unless kind == "model"
  return [] if constant.nil? || constant.empty?

  table = inferred_table(constant)
  custom = custom_table_name(repository_root, path)
  if custom
    if custom[:literal]
      return [{ "path" => "db/schema.rb", "relationship" => "schema_table:#{custom[:table]}", "confidence" => "exact", "provenance" => "literal:self.table_name=\"#{custom[:table]}\"" }]
    else
      return [{ "path" => "db/schema.rb", "relationship" => "schema_table", "confidence" => "unresolved", "provenance" => "dynamic_table_name" }]
    end
  end

  schema = SchemaParser.parse(repository_root: repository_root)
  has_structure_sql = File.file?(File.join(repository_root, "db/structure.sql"))

  if schema.key?(table)
    return [{ "path" => "db/schema.rb", "relationship" => "schema_table:#{table}", "confidence" => "conventional", "provenance" => "schema_table:#{table}" }]
  end

  if has_structure_sql
    return [{ "path" => "db/structure.sql", "relationship" => "schema_table", "confidence" => "unresolved", "provenance" => "structure_sql_not_parsed" }]
  end

  if table
    return [{ "path" => "db/schema.rb", "relationship" => "schema_table:#{table}", "confidence" => "inferred", "provenance" => "inferred_table:#{table}" }]
  end

  []
rescue StandardError
  []
end