Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new helper API #1860

Merged
merged 37 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
1500fdd
add: helper api
reeganviljoen Sep 25, 2023
305a952
remove: redundant self
reeganviljoen Sep 25, 2023
11e3aef
add: args and block passthrough
reeganviljoen Sep 25, 2023
903cd3e
add: changelog entry
reeganviljoen Sep 25, 2023
b8bbde9
Fix: markdown liniting errors
reeganviljoen Sep 25, 2023
39df32a
Update lib/view_component/helpers_api.rb
reeganviljoen Sep 29, 2023
d298203
Update docs/CHANGELOG.md
reeganviljoen Sep 29, 2023
b06db04
Update test/sandbox/app/components/helpers_api_component.rb
reeganviljoen Sep 29, 2023
c467068
Update test/sandbox/test/rendering_test.rb
reeganviljoen Sep 29, 2023
7bcc15f
refactor: use_helpers
reeganviljoen Sep 30, 2023
7052b36
refactor to use safe navigation
reeganviljoen Oct 1, 2023
c568550
wip
reeganviljoen Oct 23, 2023
b50b889
Merge branch 'main' into rv_helpers_api
reeganviljoen Oct 23, 2023
9f0eafe
add: with_helpers hellper
reeganviljoen Oct 23, 2023
da9a8af
edit: cleanup
reeganviljoen Oct 23, 2023
52eb3ba
edit: cleanup
reeganviljoen Oct 23, 2023
25867a6
edit: cleanup
reeganviljoen Oct 23, 2023
7508639
edit: cleanup
reeganviljoen Oct 23, 2023
8f7c2c0
Merge branch 'main' into rv_helpers_api
reeganviljoen Oct 23, 2023
1315a54
edit: cleanup
reeganviljoen Oct 23, 2023
9250082
remove Style/RedundantSelf lint error
reeganviljoen Oct 24, 2023
d5e5797
remove Layout/EmptyLinesAroundAccessModifier lint error
reeganviljoen Oct 24, 2023
9fbf47d
Update lib/view_component/use_helpers.rb
reeganviljoen Oct 31, 2023
927e496
Merge branch 'main' into rv_helpers_api
reeganviljoen Oct 31, 2023
1aa8539
edit: change helper spelling
reeganviljoen Oct 31, 2023
43289df
edit: omptimize helper eval
reeganviljoen Oct 31, 2023
c4fdaaa
remove test_helpers and fix use_helpers
reeganviljoen Oct 31, 2023
b270ef8
add: docs
reeganviljoen Oct 31, 2023
ba7dd22
fix markdown issues
reeganviljoen Oct 31, 2023
d0198a1
fix markdown issues
reeganviljoen Oct 31, 2023
6798415
edit: changelog entry
reeganviljoen Oct 31, 2023
530ab54
Update docs/guide/helpers.md
reeganviljoen Oct 31, 2023
357ba44
make helpers example more clear
reeganviljoen Oct 31, 2023
ea2d7bc
fix markdown issues
reeganviljoen Oct 31, 2023
3012915
edit: changelog entry
reeganviljoen Oct 31, 2023
60b66df
Merge branch 'main' into rv_helpers_api
camertron Nov 3, 2023
c2b5a95
Update helpers.md
camertron Nov 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

* Add `use_helper` API.

*Reegan Viljoen*

* Fix bug where the `Rails` module wasn't being searched from the root namespace.

*Zenéixe*
Expand Down
20 changes: 20 additions & 0 deletions docs/guide/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ class UserComponent < ViewComponent::Base
end
```

## UseHelpers setter

By default, ViewComponents don't have access to helper methods defined externally. The `use_helpers` method allows external helpers to be called from the component.

To use the `use_helpers` method, include `ViewComponent::UseHelpers`.
`UseHelpers` defines the helper on the component and is similar in use to using `delegate` on helpers.

```ruby
class UseHelpersComponent < ViewComponent::Base
include ViewComponent::UseHelpers
use_helpers :icon

erb_template <<-ERB
<div class="icon">
<%= icon :user %>
</div>
ERB
end
```

## Nested URL helpers

Rails nested URL helpers implicitly depend on the current `request` in certain cases. Since ViewComponent is built to enable reusing components in different contexts, nested URL helpers should be passed their options explicitly:
Expand Down
1 change: 1 addition & 0 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require "view_component/slotable"
require "view_component/translatable"
require "view_component/with_content_helper"
require "view_component/use_helpers"

module ViewComponent
class Base < ActionView::Base
Expand Down
20 changes: 20 additions & 0 deletions lib/view_component/use_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module ViewComponent::UseHelpers
extend ActiveSupport::Concern

class_methods do
def use_helpers(*args)
args.each do |helper_method|
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
def #{helper_method}(*args, &block)
raise HelpersCalledBeforeRenderError if view_context.nil?
__vc_original_view_context.#{helper_method}(*args, &block)
end
RUBY

ruby2_keywords(helper_method) if respond_to?(:ruby2_keywords, true)
end
end
end
end
3 changes: 3 additions & 0 deletions test/sandbox/app/components/use_helpers_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="helper__message">
<%= message %>
</div>
7 changes: 7 additions & 0 deletions test/sandbox/app/components/use_helpers_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class UseHelpersComponent < ViewComponent::Base
include ViewComponent::UseHelpers

use_helpers :message
end
5 changes: 5 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1094,4 +1094,9 @@ def test_content_security_policy_nonce

assert_selector("script", text: "\n//<![CDATA[\n \"alert('hello')\"\n\n//]]>\n", visible: :hidden)
end

def test_use_helper
render_inline(UseHelpersComponent.new)
assert_selector ".helper__message", text: "Hello helper method"
end
end
Loading