Sha256: bbfc5e236443ca83512d8b71a578e5442ed01974f339a53c8e3640f3fca487be
Contents?: true
Size: 1.2 KB
Versions: 12
Compression:
Stored size: 1.2 KB
Contents
# frozen_string_literal: true module ViewComponent class Collection def render_in(view_context, &block) iterator = ActionView::PartialIteration.new(@collection.size) @component.compile(raise_errors: true) @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
12 entries across 12 versions & 1 rubygems