Sha256: b2bcb9b065d2f8722cbdb6bb9380e490d35d30892cbbb326f8a2e1ba2e7e448f

Contents?: true

Size: 761 Bytes

Versions: 6

Compression:

Stored size: 761 Bytes

Contents

# frozen_string_literal: true

module ViewComponent
  class Collection
    def render_in(view_context, &block)
      as = @component.collection_parameter_name

      @collection.map do |item|
        @component.new(@options.merge(as => item)).render_in(view_context, &block)
      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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
view_component-2.4.0 lib/view_component/collection.rb
view_component-2.3.0 lib/view_component/collection.rb
view_component-2.2.2 lib/view_component/collection.rb
view_component-2.2.1 lib/view_component/collection.rb
view_component-2.2.0 lib/view_component/collection.rb
view_component-2.1.0 lib/view_component/collection.rb