Module: ViewComponent::SlotableV2

Extended by:
ActiveSupport::Concern
Defined in:
lib/view_component/slotable_v2.rb

Instance Method Summary collapse

Instance Method Details

#get_slot(slot_name) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/view_component/slotable_v2.rb', line 185

def get_slot(slot_name)
  slot = self.class.registered_slots[slot_name]
  @_set_slots ||= {}

  if @_set_slots[slot_name]
    return @_set_slots[slot_name]
  end

  if slot[:collection]
    []
  else
    nil
  end
end

#set_slot(slot_name, *args, **kwargs, &block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/view_component/slotable_v2.rb', line 200

def set_slot(slot_name, *args, **kwargs, &block)
  slot_definition = self.class.registered_slots[slot_name]

  slot = SlotV2.new(self)

  # Passing the block to the sub-component wrapper like this has two
  # benefits:
  #
  # 1. If this is a `content_area` style sub-component, we will render the
  # block via the `slot`
  #
  # 2. Since we have to pass block content to components when calling
  # `render`, evaluating the block here would require us to call
  # `view_context.capture` twice, which is slower
  slot._content_block = block if block_given?

  # If class
  if slot_definition[:renderable]
    slot._component_instance = slot_definition[:renderable].new(*args, **kwargs)
  # If class name as a string
  elsif slot_definition[:renderable_class_name]
    slot._component_instance = self.class.const_get(slot_definition[:renderable_class_name]).new(*args, **kwargs)
  # If passed a lambda
  elsif slot_definition[:renderable_function]
    # Use `bind(self)` to ensure lambda is executed in the context of the
    # current component. This is necessary to allow the lambda to access helper
    # methods like `content_tag` as well as parent component state.
    renderable_value = slot_definition[:renderable_function].bind(self).call(*args, **kwargs, &block)

    # Function calls can return components, so if it's a component handle it specially
    if renderable_value.respond_to?(:render_in)
      slot._component_instance = renderable_value
    else
      slot._content = renderable_value
    end
  end

  @_set_slots ||= {}

  if slot_definition[:collection]
    @_set_slots[slot_name] ||= []
    @_set_slots[slot_name].push(slot)
  else
    @_set_slots[slot_name] = slot
  end

  nil
end