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

.expand_file_path(file_path) ⇒ Pathname?

Expand a file path argument to an absolute path.

Parameters:

  • file_path (String, Pathname, nil)

Returns:

  • (Pathname, nil)

    nil if no path was given or it cannot be expanded.



66
67
68
69
70
71
72
# File 'lib/support_table_data/tasks/utils.rb', line 66

def expand_file_path(file_path)
  return nil if file_path.nil?

  Pathname.new(file_path.to_s).expand_path
rescue ArgumentError
  nil
end

.model_file_path(klass) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/support_table_data/tasks/utils.rb', line 74

def model_file_path(klass)
  return nil unless klass.name

  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:



56
57
58
59
60
# File 'lib/support_table_data/tasks/utils.rb', line 56

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

def support_table_sources(file_path = nil)
  resolved_path = expand_file_path(file_path)
  return [] if file_path && resolved_path.nil?

  require resolved_path.to_s if resolved_path

  sources = []

  ActiveRecord::Base.descendants.each do |klass|
    next unless klass.include?(SupportTableData)
    # Only the class that added the data files is documented. Single table inheritance
    # subclasses that don't add their own data files inherit the helper methods and
    # would otherwise duplicate the base class documentation.
    next unless klass.instance_variable_defined?(:@support_table_data_files)
    next if klass.instance_names.empty?

    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 resolved_path.nil?

  sources.select { |source| source.path.expand_path == resolved_path }
end