Sha256: d19e39d87e67af5ff765e64d3de207a7f6d0b02d893d0923769ac9b01b6b81a9

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require "dry/view/part_builder"
require "snowpack/view/parts/pager"
require_relative "parts/paginated"

module Snowpack
  module View
    class PartBuilder < Dry::View::PartBuilder
      def call(name, value, **options)
        if paginated_collection?(value)
          build_paginated_collection_part(name, value, **options)
        else
          super
        end
      end

      private

      def paginated_collection?(value)
        value.respond_to?(:to_a) && value.respond_to?(:pager)
      end

      def build_paginated_collection_part(name, value, **options)
        pager_part = build_part(:pager, value.pager, as: Snowpack::View::Parts::Pager)

        collection_as = collection_options(name: name, **options)[:as]
        item_name, item_as = collection_item_options(name: name, **options).values_at(:name, :as)

        arr = value.to_ary.map { |obj|
          call(item_name, obj, **options.merge(as: item_as))
        }

        collection_part_class = part_class(name: name, fallback_class: Parts::Paginated, **options.merge(as: collection_as))

        collection_part_class.new(
          name: name,
          value: arr,
          pager: pager_part,
          render_env: render_env,
        )
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
snowpack-1.0.0.alpha7 lib/snowpack/view/part_builder.rb
snowpack-1.0.0.alpha6 lib/snowpack/view/part_builder.rb
snowpack-1.0.0.alpha5 lib/snowpack/view/part_builder.rb
snowpack-1.0.0.alpha4 lib/snowpack/view/part_builder.rb
snowpack-1.0.0.alpha3 lib/snowpack/view/part_builder.rb
snowpack-1.0.0.alpha2 lib/snowpack/view/part_builder.rb