Skip to content

Commit

Permalink
Remove special handline of slot content in favor of to_s
Browse files Browse the repository at this point in the history
  • Loading branch information
BlakeWilliams committed Oct 16, 2020
1 parent e80b679 commit ed34155
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 21 deletions.
8 changes: 7 additions & 1 deletion lib/view_component/slot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

module ViewComponent
class Slot
attr_accessor :content
def _view_component_internal_content=(content)
@_view_component_internal_content = content
end

def to_s
@_view_component_internal_content
end
end
end
7 changes: 1 addition & 6 deletions lib/view_component/slotable_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@ def validate_slot_name(slot_name)
if self.registered_slots.key?(slot_name)
raise ArgumentError.new("#{slot_name} slot declared multiple times")
end

# Ensure slot name is not :content
if slot_name == :content
raise ArgumentError.new ":content is a reserved slot name. Please use another name, such as ':body'"
end
end
end

Expand Down Expand Up @@ -175,7 +170,7 @@ def set_slot(slot_name, *args, **kwargs, &block)
end

if block_given?
slot_instance.content = view_context.capture(&block)
slot_instance._view_component_internal_content = view_context.capture(&block)
end

if slot[:collection]
Expand Down
10 changes: 5 additions & 5 deletions test/app/components/slots_v2_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<div class="card <%= @class_names %>">
<div class="title">
<%= title.content %>
<%= title %>
</div>
<div class="subtitle">
<%= subtitle.content %>
<%= subtitle %>
</div>
<div class="nav">
<%= tabs.inspect %>
<% if tabs.any? %>
<% tabs.each do |tab| %>
<div class="tab">
<%= tab.content %>
<%= tab %>
</div>
<% end %>
<% else %>
Expand All @@ -21,14 +21,14 @@
<% if items.any? %>
<% items.each do |item| %>
<div class="item <%= item.class_names %>">
<%= item.content %>
<%= item %>
</div>
<% end %>
<% else %>
No items provided
<% end %>
</div>
<div class="footer <%= footer.class_names %>">
<%= footer.content %>
<%= footer %>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h1 class="<%= item.class_names %>"><%= item.title %></h1>

<div class="item-content">
<%= item.content %>
<%= item %>
</div>
</div>
<% end %>
8 changes: 0 additions & 8 deletions test/view_component/slotable_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ def test_with_slot_raise_with_duplicate_slot_name
assert_includes exception.message, "title slot declared multiple times"
end

def test_with_slot_raise_with_content_keyword
exception = assert_raises ArgumentError do
SlotsV2Component.renders_one :content
end

assert_includes exception.message, ":content is a reserved slot name"
end

def test_with_slot_with_positional_args
render_inline(SlotsV2WithPosArgComponent.new(class_names: "mt-4")) do |component|
component.item("my item", class_names: "hello") { "My rad item" }
Expand Down

0 comments on commit ed34155

Please sign in to comment.