-
Notifications
You must be signed in to change notification settings - Fork 458
Allow arguments to be passed to content areas #285 #323
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
Closed
jonspalmer
wants to merge
2
commits into
ViewComponent:master
from
jonspalmer:arguments_to_content_areas
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<div class="post_form"> | ||
<%= form_for post do |post_form| %> | ||
<div class="header"> | ||
<%= header %> | ||
</div> | ||
<div class="body"> | ||
<%= body(post_form) %> | ||
</div> | ||
<div class="footer"> | ||
<%= footer(post_form) %> | ||
</div> | ||
<% end %> | ||
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class FormComponent < ViewComponent::Base | ||
with_content_areas :header, :body, :footer | ||
|
||
def post | ||
@post ||= Post.new(title: "Check It Out") | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,17 +176,6 @@ def test_renders_content_areas_template_replaces_content | |
assert_selector(".footer", text: "Bye!") | ||
end | ||
|
||
def test_renders_content_areas_template_can_wrap_render_arguments | ||
render_inline(ContentAreasComponent.new(title: "Hello!", footer: "Bye!")) do |component| | ||
component.with(:title) { "<strong>#{component.title}</strong>".html_safe } | ||
component.with(:body) { "Have a nice day." } | ||
end | ||
|
||
assert_selector(".title strong", text: "Hello!") | ||
assert_selector(".body", text: "Have a nice day.") | ||
assert_selector(".footer", text: "Bye!") | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this change "wrapping" the render arguments results in a stack overflow. Its a weird use case anyway and not one that I'm that bothered about supporting. Seems like anyone rendering the component can choose one mechanism or the other. |
||
|
||
def test_renders_content_areas_template_raise_with_unknown_content_areas | ||
exception = assert_raises ArgumentError do | ||
render_inline(ContentAreasComponent.new(footer: "Bye!")) do |component| | ||
|
@@ -504,6 +493,24 @@ def test_render_single_item_from_collection | |
assert_selector("p", text: "On sale", count: 1) | ||
end | ||
|
||
def test_content_area_named_attributes | ||
render_inline(FormComponent.new) do |component| | ||
component.with(:header) do || | ||
"Form Header: #{component.post.title}" | ||
end | ||
component.with(:body) do |form| | ||
form.text_field :title | ||
end | ||
component.with(:footer) do |form| | ||
form.submit "Save" | ||
end | ||
end | ||
|
||
assert_selector(".header", text: "Form Header: Check It Out") | ||
assert_selector(".body input[name='post[title]'][value='Check It Out']") | ||
assert_selector(".footer input[value='Save']") | ||
end | ||
|
||
private | ||
|
||
def modify_file(file, content) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the key change. Rather than capturing the block immediately and storing the result in the attr_reader instance variable we redefine the method and capture the block when its called passing the arguments.