module Primer::Forms::Utils
def classify(options)
so the options hash can be easily passed to Rails form builder methods.
3. Combines options[:class] and options[:classes] into options[:class]
is expected by Rails/HTML while the second is specific to Primer.
2. Runs classify on both options[:class] and options[:classes]. The first
1. Runs Primer's classify routine to convert entries like mb: 1 to mb-1.
This method does the following:
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
def const_source_location(class_name)
Now that the Ruby bug above has been fixed and released, this method should be used only
public API to map constants to source files.
conventions, so it should work ok. Zeitwerk also has this information but lacks a
for the file in the configured autoload paths. Doing so relies on Rails' autoloading
Ruby's native Module.const_source_location. Instead we have to fudge it by searching
Unfortunately this bug (https://github.com/ruby/ruby/pull/5646) prevents us from using
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