Skip to content

Commit 6a3b591

Browse files
Add Url helpers to previews (#1765)
* added url helpers to previews * updated changelog * fixed lint errors * fixed lint errors * fixed lint errors * fixed lint errors
1 parent 46ace49 commit 6a3b591

File tree

6 files changed

+26
-0
lines changed

6 files changed

+26
-0
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
*Pasha Kalashnikov*
2020

21+
* Add URL helpers to previews
22+
23+
*Reegan Viljoen*
24+
2125
## 3.1.0
2226

2327
* Check `defined?(Rails) && Rails.application` before using `ViewComponent::Base.config.view_component_path`.

lib/view_component/preview.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
module ViewComponent # :nodoc:
66
class Preview
7+
include Rails.application.routes.url_helpers
78
include ActionView::Helpers::TagHelper
89
include ActionView::Helpers::AssetTagHelper
910
extend ActiveSupport::DescendantsTracker
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a href="<%= @url %>" target="_blank">root</a>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class UrlHelperComponent < ViewComponent::Base
4+
def initialize(url: root_path)
5+
@url = url
6+
end
7+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class UrlHelperComponentPreview < ViewComponent::Preview
4+
def default
5+
render(UrlHelperComponent.new(url: root_path))
6+
end
7+
end

test/sandbox/test/components/render_preview_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ def test_render_preview_from_class
1212

1313
assert_selector("div", text: "hello,world!")
1414
end
15+
16+
def test_render_preview_with_url_helper
17+
render_preview(:default, from: UrlHelperComponentPreview)
18+
19+
assert_selector("a[href='/']", text: "root")
20+
end
1521
end

0 commit comments

Comments
 (0)