Module: Yattho::Forms::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/yattho/forms/utils.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#const_source_location(class_name) ⇒ Object

Unfortunately this bug (github.com/ruby/ruby/pull/5646) prevents us from using Ruby’s native Module.const_source_location. Instead we have to fudge it by searching for the file in the configured autoload paths. Doing so relies on Rails’ autoloading conventions, so it should work ok. Zeitwerk also has this information but lacks a public API to map constants to source files.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/yattho/forms/utils.rb', line 13

def const_source_location(class_name)
  # NOTE: underscore respects namespacing, i.e. will convert Foo::Bar to foo/bar.
  class_path = "#{class_name.underscore}.rb"

  ActiveSupport::Dependencies.autoload_paths.each do |autoload_path|
    absolute_path = File.join(autoload_path, class_path)
    return absolute_path if File.exist?(absolute_path)
  end

  nil
end