Module: ModelProbe::Probes::Subclasses

Included in:
ModelProbe::Probes
Defined in:
lib/model_probe/probes/subclasses.rb

Instance Method Summary collapse

Instance Method Details

#probe_subclassesObject



4
5
6
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
40
41
42
43
# File 'lib/model_probe/probes/subclasses.rb', line 4

def probe_subclasses
  eager_load_rails!
  @count = 0
  @max_name_size = 0
  puts Rainbow(name).green + Rainbow(" < ").dimgray.faint + Rainbow(superclass.name).green.faint
  grouped = subclasses.each_with_object({}) do |model, memo|
    @max_name_size = model.name.size if model.name.size > @max_name_size
    location = Object.const_source_location(model.name)
    case location
    when Array
      path = location.first
      type = "First party" if first_party?(path)
      type = "Third party" if third_party?(path)
      type ||= "Unknown"
      memo[type] ||= []
      memo[type] << {name: model.name, path: path}
    when NilClass
      memo["Dynamic"] ||= []
      memo["Dynamic"] << {name: model.name, path: "defined dynamically at runtime"}
    else
      memo["Unknown"] ||= []
      memo["Unknown"] << {name: model.name, path: nil}
    end
  end

  keys = [
    "First party",
    "Dynamic",
    "Third party",
    "Unknown"
  ]

  keys.each do |key|
    next unless grouped[key]&.any?
    puts Rainbow("  #{key} subclasses ".ljust(35, ".")).dimgray
    grouped[key].sort_by { |entry| entry[:name] }.each do |entry|
      probe_subclass entry[:name], entry[:path]
    end
  end
end