Skip to content

Commit

Permalink
Allow implicit locale in component file names (#2118)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Oct 15, 2024
1 parent 212d159 commit 21b94a7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Fix bug where implicit locales in component filenames threw a `NameError`.

*Chloe Fons*

* Register ViewComponent tests directory for `rails stats`.

*Javier Aranda*
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ ViewComponent is built by over a hundred members of the community, including:
<img src="https://avatars.githubusercontent.com/allan-pires?s=64" alt="allan-pires" width="32" />
<img src="https://avatars.githubusercontent.com/jasonkim?s=64" alt="jasonkim" width="32" />
<img src="https://avatars.githubusercontent.com/tkowalewski" alt="tkowalewski" width="32" />
<img src="https://avatars.githubusercontent.com/chloe-meister" alt="chloe-meister" width="32" />
<img src="https://avatars.githubusercontent.com/zaratan" alt="zaratan" width="32" />

## Who uses ViewComponent?
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def gather_templates
path: path,
lineno: 0,
extension: path.split(".").last,
this_format: this_format,
this_format: this_format.to_s.split(".").last&.to_sym, # strip locale from this_format, see #2113
variant: variant
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>salut,monde!</div>
4 changes: 4 additions & 0 deletions test/sandbox/app/components/localised_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class LocalisedComponent < ViewComponent::Base
end
6 changes: 6 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1215,4 +1215,10 @@ def test_with_format
assert_equal(rendered_json["hello"], "world")
end
end

def test_localised_component
render_inline(LocalisedComponent.new)

assert_selector("div", text: "salut,monde!")
end
end

0 comments on commit 21b94a7

Please sign in to comment.