Module: SupportTableData::Tasks::Utils

Defined in:
lib/support_table_data/tasks/utils.rb

Class Method Summary collapse

Class Method Details

.eager_load!Object

Helper for eager loading a Rails application.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/support_table_data/tasks/utils.rb', line 8

def eager_load!
  return unless defined?(Rails.application.config.eager_load)
  return if Rails.application.config.eager_load

  if defined?(Rails.application.eager_load!)
    Rails.application.eager_load!
  elsif defined?(Rails.autoloaders.zeitwerk_enabled?) && Rails.autoloaders.zeitwerk_enabled?
    Rails.autoloaders.each(&:eager_load)
  else
    raise "Failed to eager load application."
  end
end

.model_file_path(klass) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/support_table_data/tasks/utils.rb', line 65

def model_file_path(klass)
  file_path = "#{klass.name.underscore}.rb"
  model_path = nil

  Rails.application.config.paths["app/models"].each do |path_prefix|
    path = Pathname.new(path_prefix.to_s).join(file_path)
    if path&.file? && path.readable?
      model_path = path
      break
    end
  end

  model_path
end

.support_table_rbs_files(file_path = nil) ⇒ Array<SupportTableData::Documentation::RbsFile>

Return RBS file handlers for all support table models.

Parameters:

  • file_path (String, Pathname, nil) (defaults to: nil)

    Optional file path to filter by.

Returns:



59
60
61
62
63
# File 'lib/support_table_data/tasks/utils.rb', line 59

def support_table_rbs_files(file_path = nil)
  support_table_sources(file_path).map do |source|
    Documentation::RbsFile.new(source.klass, source.path)
  end
end

.support_table_sources(file_path = nil) ⇒ Array<SupportTableData::Documentation::SourceFile>

Return all source files for models that include SupportTableData.

Parameters:

  • file_path (String, Pathname, nil) (defaults to: nil)

    Optional file path to filter by.

Returns:



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
# File 'lib/support_table_data/tasks/utils.rb', line 25

def support_table_sources(file_path = nil)
  file_path = Pathname.new(file_path) if file_path.is_a?(String)
  require file_path.expand_path if file_path

  sources = []

  ActiveRecord::Base.descendants.each do |klass|
    next unless klass.include?(SupportTableData)

    begin
      next if klass.instance_names.empty?
    rescue NoMethodError
      # Skip models where instance_names is not properly initialized
      next
    end

    model_file_path = SupportTableData::Tasks::Utils.model_file_path(klass)
    next unless model_file_path&.file? && model_file_path.readable?

    sources << Documentation::SourceFile.new(klass, model_file_path)
  end

  return sources if file_path.nil?

  resolved_path = Pathname.new(file_path.to_s).expand_path
  sources.select { |source| source.path.expand_path == resolved_path }
rescue ArgumentError
  []
end