Skip to content

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ def with(area, content = nil, &block)
raise ArgumentError.new "Unknown content_area '#{area}' - expected one of '#{content_areas}'"
end

if block_given?
content = view_context.capture(&block)
define_singleton_method(area) do |*args|
block = ->() { content } unless block_given?
view_context.capture(*args, &block)
Copy link
Contributor Author

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.

end

instance_variable_set("@#{area}".to_sym, content)
nil
end

Expand Down Expand Up @@ -236,7 +236,7 @@ def with_content_areas(*areas)
if areas.include?(:content)
raise ArgumentError.new ":content is a reserved content area name. Please use another name, such as ':body'"
end
attr_reader *areas
attr_reader *areas # provide a default implementation for all areas. Will be redifined by calls to .with.
self.content_areas = areas
end

Expand Down
13 changes: 13 additions & 0 deletions test/app/components/form_component.html.erb
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>
9 changes: 9 additions & 0 deletions test/app/components/form_component.rb
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
1 change: 1 addition & 0 deletions test/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
get :controller_inline, to: "integration_examples#controller_inline"
get :controller_inline_baseline, to: "integration_examples#controller_inline_baseline"
get :controller_to_string, to: "integration_examples#controller_to_string"
resources :posts
end
29 changes: 18 additions & 11 deletions test/view_component/view_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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|
Expand Down Expand Up @@ -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)
Expand Down