Skip to content

Commit

Permalink
pivot - only allow with_content to be called with strings
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelpuyol committed Apr 22, 2021
1 parent 785d809 commit 963dd77
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 105 deletions.
12 changes: 2 additions & 10 deletions docs/guide/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Content passed to a ViewComponent as a block is captured and assigned to the `co

## `#with_content`

Content can also be passed to a ViewComponent by calling `#with_content`, which is especially useful when rendering components outside of views (such as in a controller, background job, script, etc):
String content can also be passed to a ViewComponent by calling `#with_content`, which is especially useful when rendering components outside of views (such as in a controller, background job, script, etc).

```rb
class MyController < ApplicationController
Expand All @@ -41,14 +41,6 @@ class MyComponentBuilder
end
```

`#with_content` also accepts passing another component as content:

```rb
component = MyComponent.new.with_content(SomeComponent.new)
...
render(component)
```

### Slots

`#with_content` is also available for slots:
Expand All @@ -70,7 +62,7 @@ class MySlotComponentBuilder
component.another_slot(args).with_content("This is another slot content")
end

component.yet_another_slot(args).with_content(SomeComponent.new)
component.yet_another_slot(args).with_content("More content here")

component
end
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def content
@_content = if @view_context && @_render_in_block
view_context.capture(self, &@_render_in_block)
elsif defined?(@_content_set_by_with_content)
render_or_return(@_content_set_by_with_content.call(self))
@_content_set_by_with_content.call(self)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/slot_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def to_s
elsif defined?(@_content_block)
view_context.capture(&@_content_block)
elsif defined?(@_content_set_by_with_content)
render_or_return(@_content_set_by_with_content.call(self))
@_content_set_by_with_content.call(self)
end

@content
Expand Down
10 changes: 0 additions & 10 deletions lib/view_component/with_content_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,5 @@ def with_content(value = nil, &block)

self
end

# Renders the given object and returns the result, if object can be rendered.
# Otherwise, returns object.
def render_or_return(object)
if object.respond_to?(:render_in)
render(object)
else
object
end
end
end
end
67 changes: 0 additions & 67 deletions test/view_component/slotable_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,27 +326,6 @@ def test_renders_pass_through_slot_using_with_content_block
render_inline(component)
assert_selector(".title", text: "This is my title!")
end

def test_renders_pass_through_slot_passing_another_component
component = SlotsV2Component.new
component.title("some_argument").with_content(MyComponent.new)

render_inline(component)
assert_selector(".title") do
assert_selector("div", text: "hello,world")
end
end

def test_renders_pass_through_slot_passing_another_component_as_block
component = SlotsV2Component.new
component.title("some_argument").with_content { MyComponent.new }

render_inline(component)
assert_selector(".title") do
assert_selector("div", text: "hello,world")
end
end

def test_renders_lambda_slot_using_with_content
component = SlotsV2Component.new
component.item(highlighted: false).with_content("This is my item!")
Expand All @@ -363,26 +342,6 @@ def test_renders_lambda_slot_using_with_content_block
assert_selector(".item.normal", text: "This is my item!")
end

def test_renders_lambda_slot_passing_another_component
component = SlotsV2Component.new
component.item(highlighted: false).with_content(MyComponent.new)

render_inline(component)
assert_selector(".item.normal") do
assert_selector("div", text: "hello,world")
end
end

def test_renders_lambda_slot_passing_another_component_as_block
component = SlotsV2Component.new
component.item(highlighted: false).with_content { MyComponent.new }

render_inline(component)
assert_selector(".item.normal") do
assert_selector("div", text: "hello,world")
end
end

def test_renders_component_slot_using_with_content
component = SlotsV2Component.new
component.extra(message: "My message").with_content("This is my content!")
Expand All @@ -404,30 +363,4 @@ def test_renders_component_slot_using_with_content_block
assert_text("My message")
end
end

def test_renders_component_slot_passing_another_component
component = SlotsV2Component.new
component.extra(message: "My message").with_content(MyComponent.new)

render_inline(component)
assert_selector(".extra") do
assert_selector("div") do
assert_selector("div", text: "hello,world")
assert_text("My message")
end
end
end

def test_renders_component_slot_passing_another_component_as_block
component = SlotsV2Component.new
component.extra(message: "My message").with_content { MyComponent.new }

render_inline(component)
assert_selector(".extra") do
assert_selector("div") do
assert_selector("div", text: "hello,world")
assert_text("My message")
end
end
end
end
16 changes: 0 additions & 16 deletions test/view_component/view_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,6 @@ def test_renders_content_given_as_block
assert_selector("span", text: "from block")
end

def test_renders_component_given_as_content_as_argument
render_inline(WrapperComponent.new.with_content(MyComponent.new))

assert_selector("div", text: "hello,world")
end

def test_renders_component_given_as_content_as_block
component = WrapperComponent.new.with_content do
MyComponent.new
end

render_inline(component)

assert_selector("div", text: "hello,world")
end

def test_raises_error_when_content_given_as_argument_and_block
exception = assert_raises ArgumentError do
WrapperComponent.new.with_content("from arg") do
Expand Down

0 comments on commit 963dd77

Please sign in to comment.