From c9a36562e6ab2f23ae0ed7eaf1b69e825f2992d4 Mon Sep 17 00:00:00 2001 From: Blake Williams Date: Thu, 15 Oct 2020 21:26:15 -0400 Subject: [PATCH] Fix up documentation --- lib/view_component/slot.rb | 3 ++ lib/view_component/slotable_v2.rb | 57 +++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/lib/view_component/slot.rb b/lib/view_component/slot.rb index df48a6e32..e944976a4 100644 --- a/lib/view_component/slot.rb +++ b/lib/view_component/slot.rb @@ -2,6 +2,9 @@ module ViewComponent class Slot + # Necessary for Slots v1 + attr_accessor :content + def _view_component_internal_content=(content) @_view_component_internal_content = content end diff --git a/lib/view_component/slotable_v2.rb b/lib/view_component/slotable_v2.rb index 3afa692ed..5192ad0e2 100644 --- a/lib/view_component/slotable_v2.rb +++ b/lib/view_component/slotable_v2.rb @@ -24,11 +24,8 @@ module V2 # # = Example # - # with_slot( - # :item, - # collection: true - # ) do - # def initialize(name:) + # renders_one :header do + # def initialize(classes:) # @name = name # end # end @@ -40,9 +37,7 @@ module V2 # is a collection). # #

- # <%= items.each do |item| %> - # <%= item.content %> - # <% end %> + # <%= header %> #

# # = Setting slot content @@ -53,12 +48,8 @@ module V2 # slot. # # <%= render_inline(MyComponent.new) do |component| %> - # <%= component.item(name: "Foo") do %> - #

One

- # <% end %> - # - # <%= component.item(name: "Bar") do %> - #

two

+ # <%= component.header(classes: "Foo") do %> + #

Bar

# <% end %> # <% end %> def renders_one(slot_name, &block) @@ -75,6 +66,44 @@ def renders_one(slot_name, &block) register_slot(slot_name, collection: false, &block) end + ## + # Registers a collection slot on the component. + # + # = Example + # + # render_many :items do + # def initialize(name:) + # @name = name + # end + # end + # + # = Rendering slot content + # + # The component's sidecar template can access the slot by calling a + # helper method with the same name as the slot (pluralized if the slot + # is a collection). + # + #

+ # <%= items.each do |item| %> + # <%= item %> + # <% end %> + #

+ # + # = Setting slot content + # + # Renderers of the component can set the content of a slot by calling a + # helper method with the same name as the slot. The method can be + # called multiple times to append to the slot. + # + # <%= render_inline(MyComponent.new) do |component| %> + # <%= component.item(name: "Foo") do %> + #

One

+ # <% end %> + # + # <%= component.item(name: "Bar") do %> + #

two

+ # <% end %> + # <% end %> def renders_many(slot_name, &block) validate_slot_name(slot_name)