Module: Primer::Forms::Utils
- Extended by:
- Utils
- Includes:
- ClassNameHelper
- Included in:
- Utils
- Defined in:
- app/lib/primer/forms/utils.rb
Overview
:nodoc:
Constant Summary collapse
Instance Method Summary collapse
-
#classify(options) ⇒ Object
This method does the following:.
-
#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.
Methods included from ClassNameHelper
Instance Method Details
#classify(options) ⇒ Object
This method does the following:
-
Runs Primer’s classify routine to convert entries like mb: 1 to mb-1.
-
Runs classify on both options and options. The first is expected by Rails/HTML while the second is specific to Primer.
-
Combines options and options into options so the options hash can be easily passed to Rails form builder methods.
54 55 56 57 58 59 60 |
# File 'app/lib/primer/forms/utils.rb', line 54 def classify() [:classes] = class_names(.delete(:class), [:classes]) .merge!(Primer::Classify.call()) .except!(*PRIMER_UTILITY_KEYS) [:class] = class_names([:class], .delete(:classes)) end |
#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.
Now that the Ruby bug above has been fixed and released, this method should be used only as a fallback for older Rubies.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/lib/primer/forms/utils.rb', line 20 def const_source_location(class_name) return nil unless class_name if (location = Object.const_source_location(class_name)&.[](0)) return location end # NOTE: underscore respects namespacing, i.e. will convert Foo::Bar to foo/bar. class_path = "#{class_name.underscore}.rb" # Prefer Zeitwerk-managed paths, falling back to ActiveSupport::Dependencies if Zeitwerk # is disabled or not in use (i.e. the case for older Rails versions). autoload_paths = if Rails.respond_to?(:autoloaders) && Rails.autoloaders.zeitwerk_enabled? Rails.autoloaders.main.dirs else ActiveSupport::Dependencies.autoload_paths end 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 |