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

#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



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/view_component/collection.rb', line 19

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



33
34
35
# File 'lib/view_component/collection.rb', line 33

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

#render_in(view_context, &block) ⇒ Object



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

def render_in(view_context, &block)
  components.map do |component|
    component.render_in(view_context, &block)
  end.join.html_safe # rubocop:disable Rails/OutputSafety
end