Class: ViewComponent::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/view_component/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__vc_original_view_contextObject

Returns the value of attribute __vc_original_view_context.



12
13
14
# File 'lib/view_component/collection.rb', line 12

def __vc_original_view_context
  @__vc_original_view_context
end

#componentObject (readonly)

Returns the value of attribute component.



8
9
10
# File 'lib/view_component/collection.rb', line 8

def component
  @component
end

Instance Method Details

#componentsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/view_component/collection.rb', line 25

def components
  return @components if defined? @components

  iterator = ActionView::PartialIteration.new(@collection.size)

  component.validate_collection_parameter!(validate_default: true)

  @components = @collection.map do |item|
    component.new(**component_options(item, iterator)).tap do |component|
      iterator.iterate!
    end
  end
end

#each(&block) ⇒ Object



39
40
41
# File 'lib/view_component/collection.rb', line 39

def each(&block)
  components.each(&block)
end

#formatObject

Rails expects us to define ‘format` on all renderables, but we do not know the `format` of a ViewComponent until runtime.



45
46
47
# File 'lib/view_component/collection.rb', line 45

def format
  nil
end

#render_in(view_context, &block) ⇒ Object



18
19
20
21
22
23
# File 'lib/view_component/collection.rb', line 18

def render_in(view_context, &block)
  components.map do |component|
    component.set_original_view_context(__vc_original_view_context)
    component.render_in(view_context, &block)
  end.join(rendered_spacer(view_context)).html_safe
end

#set_original_view_context(view_context) ⇒ Object



14
15
16
# File 'lib/view_component/collection.rb', line 14

def set_original_view_context(view_context)
  self.__vc_original_view_context = view_context
end