Skip to content

Commit 21b94a7

Browse files
Allow implicit locale in component file names (#2118)
* Add failing test case for localised erb templates * Simpler example * Remove locale from format when checking default * Add changelog entry * Add to contributor list * Update docs/CHANGELOG.md * move locale removal logic to compiler --------- Co-authored-by: Joel Hawksley <[email protected]> Co-authored-by: Joel Hawksley <[email protected]>
1 parent 212d159 commit 21b94a7

File tree

6 files changed

+17
-1
lines changed

6 files changed

+17
-1
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+
* Fix bug where implicit locales in component filenames threw a `NameError`.
14+
15+
*Chloe Fons*
16+
1317
* Register ViewComponent tests directory for `rails stats`.
1418

1519
*Javier Aranda*

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ ViewComponent is built by over a hundred members of the community, including:
228228
<img src="https://avatars.githubusercontent.com/allan-pires?s=64" alt="allan-pires" width="32" />
229229
<img src="https://avatars.githubusercontent.com/jasonkim?s=64" alt="jasonkim" width="32" />
230230
<img src="https://avatars.githubusercontent.com/tkowalewski" alt="tkowalewski" width="32" />
231+
<img src="https://avatars.githubusercontent.com/chloe-meister" alt="chloe-meister" width="32" />
231232
<img src="https://avatars.githubusercontent.com/zaratan" alt="zaratan" width="32" />
232233

233234
## Who uses ViewComponent?

lib/view_component/compiler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def gather_templates
192192
path: path,
193193
lineno: 0,
194194
extension: path.split(".").last,
195-
this_format: this_format,
195+
this_format: this_format.to_s.split(".").last&.to_sym, # strip locale from this_format, see #2113
196196
variant: variant
197197
)
198198

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>salut,monde!</div>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
class LocalisedComponent < ViewComponent::Base
4+
end

test/sandbox/test/rendering_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,4 +1215,10 @@ def test_with_format
12151215
assert_equal(rendered_json["hello"], "world")
12161216
end
12171217
end
1218+
1219+
def test_localised_component
1220+
render_inline(LocalisedComponent.new)
1221+
1222+
assert_selector("div", text: "salut,monde!")
1223+
end
12181224
end

0 commit comments

Comments
 (0)