Skip to content

Commit

Permalink
Use ActionView logic for parsing template names
Browse files Browse the repository at this point in the history
Removes support for variant names containing `.`.
  • Loading branch information
sfnelson committed Nov 4, 2024
1 parent 8fddc85 commit 0b08d50
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 24 deletions.
7 changes: 7 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ nav_order: 5

*Joel Hawksley*

* BREAKING: Remove support for variant names containing `.`

This feature is not supported by Rails and prevents the use of Rails logic
for file name parsing.

*Stephen Nelson*

* Ensure HTML output safety wrapper is used for all inline templates.

*Joel Hawksley*
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ ViewComponent is built by over a hundred members of the community, including:
<img src="https://avatars.githubusercontent.com/sammyhenningsson?s=64" alt="sammyhenningsson" width="32" />
<img src="https://avatars.githubusercontent.com/sampart?s=64" alt="sampart" width="32" />
<img src="https://avatars.githubusercontent.com/seanpdoyle?s=64" alt="seanpdoyle" width="32" />
<img src="https://avatars.githubusercontent.com/sfnelson?s=64" alt="sfnelson" width="32" />
<img src="https://avatars.githubusercontent.com/simonrand?s=64" alt="simonrand" width="32" />
<img src="https://avatars.githubusercontent.com/skryukov?s=64" alt="skryukov" width="32" />
<img src="https://avatars.githubusercontent.com/smashwilson?s=64" alt="smashwilson" width="32" />
Expand Down
4 changes: 3 additions & 1 deletion lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ def template_errors
def gather_templates
@templates ||=
begin
path_parser = ActionView::Resolver::PathParser.new
templates = @component.sidecar_files(
ActionView::Template.template_handler_extensions
).map do |path|
out = Template::File.new(component: @component, path: path)
details = path_parser.parse(path).details
out = Template::File.new(component: @component, path: path, details: details)

out
end
Expand Down
19 changes: 5 additions & 14 deletions lib/view_component/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,14 @@ def initialize(
end

class File < Template
def initialize(component:, path:)
# Extract format and variant from template filename
this_format, variant =
::File
.basename(path) # "variants_component.html+mini.watch.erb"
.split(".")[1..-2] # ["html+mini", "watch"]
.join(".") # "html+mini.watch"
.split("+") # ["html", "mini.watch"]
.map(&:to_sym) # [:html, :"mini.watch"]

def initialize(component:, path:, details:)
super(
component: component,
path: path,
lineno: 0,
extension: path.split(".").last,
this_format: this_format.to_s.split(".").last&.to_sym, # strip locale from this_format, see #2113
variant: variant
extension: details.handler.to_s,
this_format: details.format,
variant: details.variant
)
end

Expand Down Expand Up @@ -161,7 +152,7 @@ def safe_method_name
end

def normalized_variant_name
@variant.to_s.gsub("-", "__").gsub(".", "___")
@variant.to_s.gsub("-", "__")
end

def defined_on_self?
Expand Down

This file was deleted.

8 changes: 0 additions & 8 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,6 @@ def test_renders_component_with_variant_containing_a_dash
end
end

def test_renders_component_with_variant_containing_a_dot
with_variant :"mini.watch" do
render_inline(VariantsComponent.new)

assert_text("Mini Watch with dot")
end
end

def test_renders_default_template_when_variant_template_is_not_present
with_variant :variant_without_template do
render_inline(VariantsComponent.new)
Expand Down

0 comments on commit 0b08d50

Please sign in to comment.