Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/demo/primer/primitiv…
Browse files Browse the repository at this point in the history
…es-10.0.0
  • Loading branch information
langermank authored Dec 16, 2024
2 parents 4144f95 + eea9da6 commit c561922
Show file tree
Hide file tree
Showing 15 changed files with 818 additions and 94 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-seals-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Improve template path detection for forms
5 changes: 5 additions & 0 deletions .changeset/sharp-jokes-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Adds `role="radiogroup"` on `fieldset` in radio group component
2 changes: 1 addition & 1 deletion .github/workflows/demo-production-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
with:
ruby-version: '3.3'
bundler-cache: true
- uses: actions/cache@v4.1.0
- uses: actions/cache@v4.2.0
with:
path: demo/gemfiles/vendor/bundle
key: gems-build-kuby-main-ruby-3.3.x-${{ hashFiles('demo/gemfiles/kuby.gemfile.lock') }}
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v4
- name: Get specific changed files
id: changed-files
uses: tj-actions/changed-files@v45.0.4
uses: tj-actions/changed-files@v45.0.5
with:
files: |
docs/**/*.md
Expand All @@ -38,7 +38,7 @@ jobs:
- uses: actions/checkout@v4
- name: Get specific changed files
id: changed-files
uses: tj-actions/changed-files@v45.0.4
uses: tj-actions/changed-files@v45.0.5
with:
files: |
app/**/*.rb
Expand All @@ -58,7 +58,7 @@ jobs:
- uses: actions/checkout@v4
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v45.0.4
uses: tj-actions/changed-files@v45.0.5
with:
files: |
app/components/**/*.erb
Expand All @@ -75,7 +75,7 @@ jobs:
- uses: actions/checkout@v4
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v45.0.4
uses: tj-actions/changed-files@v45.0.5
with:
files: |
app/components/**/*.ts
Expand All @@ -95,7 +95,7 @@ jobs:
- uses: actions/checkout@v4
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v45.0.4
uses: tj-actions/changed-files@v45.0.5
with:
files: |
app/components/**/*.pcss
Expand Down
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
2 changes: 1 addition & 1 deletion app/lib/primer/forms/radio_button_group.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="FormControl-radio-group-wrap">
<%= content_tag(:fieldset, **@input.input_arguments) do %>
<%= content_tag(:fieldset, role: 'radiogroup', **@input.input_arguments) do %>
<% if @input.label %>
<%= content_tag(:legend, **@input.label_arguments) do %>
<%= @input.label %>
Expand Down
17 changes: 16 additions & 1 deletion app/lib/primer/forms/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,28 @@ module Utils
# 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.
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|
# 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
Expand Down
2 changes: 1 addition & 1 deletion demo/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ GEM
racc (~> 1.4)
nokogiri (1.16.8-x86_64-linux)
racc (~> 1.4)
octicons (19.12.0)
octicons (19.13.0)
oj (3.16.3)
bigdecimal (>= 3.0)
os (1.1.4)
Expand Down
1 change: 0 additions & 1 deletion demo/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class Application < Rails::Application
config.view_component.preview_controller = "PreviewController"
config.view_component.preview_paths << Rails.root.join("..", "previews")

config.autoload_paths << Rails.root.join("..", "test", "forms")
config.autoload_paths << Rails.root.join("..", "test", "test_helpers", "components")

config.action_dispatch.default_headers.clear
Expand Down
Loading

0 comments on commit c561922

Please # to comment.