Sha256: ccd7660382ad46c12acbe9827d77c547947391af44c987545dd26933d8d542a2

Contents?: true

Size: 1.3 KB

Versions: 10

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require "action_view/renderer/collection_renderer" if Rails.version.to_f >= 6.1

module ViewComponent
  class Collection
    attr_reader :component
    delegate :format, to: :component

    def render_in(view_context, &block)
      iterator = ActionView::PartialIteration.new(@collection.size)

      component.validate_collection_parameter!(validate_default: true)

      @collection.map do |item|
        content = component.new(**component_options(item, iterator)).render_in(view_context, &block)
        iterator.iterate!
        content
      end.join.html_safe
    end

    private

    def initialize(component, object, **options)
      @component = component
      @collection = collection_variable(object || [])
      @options = options
    end

    def collection_variable(object)
      if object.respond_to?(:to_ary)
        object.to_ary
      else
        raise ArgumentError.new("The value of the argument isn't a valid collection. Make sure it responds to to_ary: #{object.inspect}")
      end
    end

    def component_options(item, iterator)
      item_options = { component.collection_parameter => item }
      item_options[component.collection_counter_parameter] = iterator.index + 1 if component.counter_argument_present?

      @options.merge(item_options)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
view_component-2.31.2 lib/view_component/collection.rb
view_component-2.32.0 lib/view_component/collection.rb
view_component-2.31.1 lib/view_component/collection.rb
view_component-2.31.0 lib/view_component/collection.rb
view_component-2.30.0 lib/view_component/collection.rb
view_component-2.29.0 lib/view_component/collection.rb
view_component-2.28.0 lib/view_component/collection.rb
view_component-2.27.0 lib/view_component/collection.rb
view_component-2.26.1 lib/view_component/collection.rb
view_component-2.26.0 lib/view_component/collection.rb