Skip to content

Commit 4f2c93c

Browse files
authored
Support variants with dots in their names (#1585)
This follows the same principles as commit 95ceed1. Quoting that commit: > ViewComponent defines variant method calls with `def > call_#{variant}`, which results in invalid Ruby code when the variant > contains a [dash or dot]. Replacing dots with triple underscores in the method definition (similar to what Bundler does when converting hostnames to environment variables [1]) and modifying the code getting the variant so it includes dots in the variant name solves the issue. [1] https://bundler.io/man/bundle-config.1.html#CONFIGURE-BUNDLER-DIRECTORIES
1 parent e1b9b47 commit 4f2c93c

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* Support variants with dots in their names.
14+
15+
*Javi Martín*
16+
1317
## 2.77.0
1418

1519
* Support variants with dashes in their names.

lib/view_component/compiler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def templates
189189
pieces = File.basename(path).split(".")
190190
memo << {
191191
path: path,
192-
variant: pieces.second.split("+").second&.to_sym,
192+
variant: pieces[1..-2].join(".").split("+").second&.to_sym,
193193
handler: pieces.last
194194
}
195195
end
@@ -254,7 +254,7 @@ def call_method_name(variant)
254254
end
255255

256256
def normalized_variant_name(variant)
257-
variant.to_s.gsub("-", "__")
257+
variant.to_s.gsub("-", "__").gsub(".", "___")
258258
end
259259

260260
def should_compile_superclass?
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Mini Watch
1+
Mini Watch with dash
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Mini Watch with dot

test/sandbox/test/rendering_test.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,15 @@ def test_renders_component_with_variant_containing_a_dash
198198
with_variant :"mini-watch" do
199199
render_inline(VariantsComponent.new)
200200

201-
assert_text("Mini Watch")
201+
assert_text("Mini Watch with dash")
202+
end
203+
end
204+
205+
def test_renders_component_with_variant_containing_a_dot
206+
with_variant :"mini.watch" do
207+
render_inline(VariantsComponent.new)
208+
209+
assert_text("Mini Watch with dot")
202210
end
203211
end
204212

0 commit comments

Comments
 (0)