Skip to content

Commit 570d46c

Browse files
Add prefix to use_helper(s) (#2064)
* add: implementation, docs comming soon * add named prefixes * add docs and collection implementation * add changelog entry * Update docs/CHANGELOG.md --------- Co-authored-by: Joel Hawksley <[email protected]> Co-authored-by: Joel Hawksley <[email protected]>
1 parent 4183f42 commit 570d46c

File tree

9 files changed

+99
-5
lines changed

9 files changed

+99
-5
lines changed

docs/CHANGELOG.md

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

1919
*Seth Herr*
2020

21+
* Add `prefix:` option to `use_helpers`.
22+
23+
*Reegan Viljoen*
24+
2125
## 3.13.0
2226

2327
* Add ruby head and YJIT to CI.

docs/guide/helpers.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ class UserComponent < ViewComponent::Base
8181
end
8282
```
8383

84+
Use the `prefix:` keyword to prefix the helper method with the name of the helper module:
85+
86+
```ruby
87+
class UserComponent < ViewComponent::Base
88+
use_helpers :icon, :icon?, from: IconHelper, prefix: true
89+
90+
def profile_icon
91+
icon_helper_icon? ? icon_helper_icon(:user) : icon_helper_icon(:guest)
92+
end
93+
end
94+
```
95+
96+
or use the `prefix:` keyword with a custom prefix:
97+
98+
```ruby
99+
class UserComponent < ViewComponent::Base
100+
use_helpers :icon, :icon?, from: IconHelper, prefix: :user
101+
102+
def profile_icon
103+
user_icon? ? user_icon(:user) : user_icon(:guest)
104+
end
105+
end
106+
```
107+
84108
The singular version `use_helper` is also available:
85109

86110
```ruby

lib/view_component/use_helpers.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,35 @@ module ViewComponent::UseHelpers
44
extend ActiveSupport::Concern
55

66
class_methods do
7-
def use_helpers(*args, from: nil)
8-
args.each { |helper_method| use_helper(helper_method, from: from) }
7+
def use_helpers(*args, from: nil, prefix: false)
8+
args.each { |helper_method| use_helper(helper_method, from: from, prefix: prefix) }
99
end
1010

11-
def use_helper(helper_method, from: nil)
11+
def use_helper(helper_method, from: nil, prefix: false)
12+
helper_method_name = full_helper_method_name(helper_method, prefix: prefix, source: from)
13+
1214
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
13-
def #{helper_method}(*args, &block)
15+
def #{helper_method_name}(*args, &block)
1416
raise HelpersCalledBeforeRenderError if view_context.nil?
1517
1618
#{define_helper(helper_method: helper_method, source: from)}
1719
end
1820
RUBY
19-
ruby2_keywords(helper_method) if respond_to?(:ruby2_keywords, true)
21+
ruby2_keywords(helper_method_name) if respond_to?(:ruby2_keywords, true)
2022
end
2123

2224
private
2325

26+
def full_helper_method_name(helper_method, prefix: false, source: nil)
27+
return helper_method unless prefix.present?
28+
29+
if !!prefix == prefix
30+
"#{source.to_s.underscore}_#{helper_method}"
31+
else
32+
"#{prefix}_#{helper_method}"
33+
end
34+
end
35+
2436
def define_helper(helper_method:, source:)
2537
return "__vc_original_view_context.#{helper_method}(*args, &block)" unless source.present?
2638

test/sandbox/app/components/use_helper_macro_component.html copy.erb renamed to test/sandbox/app/components/use_helper_macro_component.html.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
<%= message_with_kwargs(name: 'macro kwargs helper method') %>
1111
</div>
1212

13+
<div class='helper__prefix-message'>
14+
<%= macro_helper_message_with_prefix('macro prefix helper method') %>
15+
</div>
16+
17+
<div class='helper__prefix-message'>
18+
<%= named_message_with_named_prefix('macro named prefix helper method') %>
19+
</div>
20+
1321
<div class='helper__block-message'>
1422
<%= block_content %>
1523
</div>

test/sandbox/app/components/use_helper_macro_component.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ class UseHelperMacroComponent < ViewComponent::Base
44
use_helper :message, from: MacroHelper
55
use_helper :message_with_args, from: MacroHelper
66
use_helper :message_with_kwargs, from: MacroHelper
7+
use_helper :message_with_prefix, from: MacroHelper, prefix: true
78
use_helper :message_with_block, from: MacroHelper
9+
use_helper :message_with_named_prefix, from: MacroHelper, prefix: :named
810

911
def block_content
1012
message_with_block { "Hello block helper method" }

test/sandbox/app/components/use_helpers_macro_component.html.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@
1313
<div class='helper__block-message'>
1414
<%= block_content %>
1515
</div>
16+
17+
<div class='helper__prefix-message'>
18+
<%= macro_helper_message_with_args('macro prefix helper method') %>
19+
</div>
20+
21+
<div class='helper__named-prefix-message'>
22+
<%= named_message_with_args('macro named prefix helper method') %>
23+
</div>

test/sandbox/app/components/use_helpers_macro_component.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
class UseHelpersMacroComponent < ViewComponent::Base
44
use_helpers :message, :message_with_args, :message_with_kwargs, :message_with_block, from: MacroHelper
55

6+
use_helpers :message_with_args, from: MacroHelper, prefix: true
7+
8+
use_helpers :message_with_args, from: MacroHelper, prefix: :named
9+
610
def block_content
711
message_with_block { "Hello block helper method" }
812
end

test/sandbox/app/helpers/macro_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def message_with_kwargs(name:)
1313
"Hello #{name}"
1414
end
1515

16+
def message_with_prefix(name)
17+
"Hello #{name}"
18+
end
19+
20+
def message_with_named_prefix(name)
21+
"Hello #{name}"
22+
end
23+
1624
def message_with_block
1725
yield
1826
end

test/sandbox/test/rendering_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,4 +1169,28 @@ def test_use_helper_macros_with_block
11691169

11701170
assert_selector ".helper__block-message", text: "Hello block helper method"
11711171
end
1172+
1173+
def test_use_helper_macros_with_prefix
1174+
render_inline(UseHelperMacroComponent.new)
1175+
1176+
assert_selector ".helper__prefix-message", text: "Hello macro prefix helper method"
1177+
end
1178+
1179+
def test_use_helper_macros_with_named_prefix
1180+
render_inline(UseHelperMacroComponent.new)
1181+
1182+
assert_selector ".helper__prefix-message", text: "Hello macro named prefix helper method"
1183+
end
1184+
1185+
def test_use_helpers_macros_with_prefix
1186+
render_inline(UseHelpersMacroComponent.new)
1187+
1188+
assert_selector ".helper__prefix-message", text: "Hello macro prefix helper method"
1189+
end
1190+
1191+
def test_use_helpers_macros_with_named_prefix
1192+
render_inline(UseHelpersMacroComponent.new)
1193+
1194+
assert_selector ".helper__named-prefix-message", text: "Hello macro named prefix helper method"
1195+
end
11721196
end

0 commit comments

Comments
 (0)