Module: Primer::Forms::Utils

Extended by:
Utils
Includes:
ClassNameHelper
Included in:
Utils
Defined in:
lib/primer/forms/utils.rb

Overview

:nodoc:

Constant Summary collapse

PRIMER_UTILITY_KEYS =
Primer::Classify::Utilities::UTILITIES.keys.freeze

Instance Method Summary collapse

Methods included from ClassNameHelper

#class_names

Instance Method Details

#classify(options) ⇒ Object

This method does the following:

  1. Runs Primer’s classify routine to convert entries like mb: 1 to mb-1.

  2. Runs classify on both options and options. The first is expected by Rails/HTML while the second is specific to Primer.

  3. Combines options and options into options so the options hash can be easily passed to Rails form builder methods.



39
40
41
42
43
44
45
# File 'lib/primer/forms/utils.rb', line 39

def classify(options)
  options[:classes] = class_names(options.delete(:class), options[:classes])
  options.merge!(Primer::Classify.call(options))
  options.except!(*PRIMER_UTILITY_KEYS)
  options[:class] = class_names(options[:class], options.delete(:classes))
  options
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.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/primer/forms/utils.rb', line 17

def const_source_location(class_name)
  return nil unless 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