Skip to content

Commit

Permalink
Improve form path detection
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron committed Dec 9, 2024
1 parent 4bfbadc commit 08df9fe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
11 changes: 9 additions & 2 deletions app/lib/primer/forms/acts_as_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ def self.extended(base)
TemplateGlob = Struct.new(:glob_pattern, :method_name, :on_compile_callback)
TemplateParams = Struct.new(:source, :identifier, :type, :format, keyword_init: true)

attr_accessor :template_root_path

def renders_templates(glob_pattern, method_name = nil, &block)
template_globs << TemplateGlob.new(glob_pattern, method_name, block)
end
Expand All @@ -70,6 +68,15 @@ def compile!

private

def template_root_path
return @template_root_path if defined?(@template_root_path)

form_path = Utils.const_source_location(self.name)
@template_root_path = if form_path
File.join(File.dirname(form_path), self.name.demodulize.underscore)
end
end

def template_globs
@template_globs ||= []
end
Expand Down
5 changes: 0 additions & 5 deletions app/lib/primer/forms/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ def new(builder, **options)
end

def inherited(base)
form_path = Utils.const_source_location(base.name)
return unless form_path

base.template_root_path = File.join(File.dirname(form_path), base.name.demodulize.underscore)

base.renders_template "after_content.html.erb" do
base.instance_variable_set(:@has_after_content, true)
end
Expand Down
8 changes: 5 additions & 3 deletions app/lib/primer/forms/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ class BaseComponent
include Primer::ClassNameHelper
extend ActsAsComponent

def self.inherited(base)
base_path = Utils.const_source_location(base.name)
def self.compile!
base_path = Utils.const_source_location(self.name)

unless base_path
warn "Could not identify the template for #{base}"
return
end

dir = File.dirname(base_path)
base.renders_template File.join(dir, "#{base.name.demodulize.underscore}.html.erb"), :render_template
renders_template File.join(dir, "#{self.name.demodulize.underscore}.html.erb"), :render_template

super
end

delegate :required?, :disabled?, :hidden?, to: :@input
Expand Down
12 changes: 11 additions & 1 deletion app/lib/primer/forms/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ module Utils
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"

ActiveSupport::Dependencies.autoload_paths.each do |autoload_path|
autoload_paths = if Rails.respond_to?(:autoloaders) && Rails.autoloaders.zeitwerk_enabled?
Rails.autoloaders.main.dirs
else
ActiveSupport::Dependencies.autoload_paths
end

Rails.autoloaders.main.dirs.each do |autoload_path|
absolute_path = File.join(autoload_path, class_path)
return absolute_path if File.exist?(absolute_path)
end
Expand Down

0 comments on commit 08df9fe

Please sign in to comment.