Sha256: 61468a5e69170e0e8b566d44d538e6b970c1fa13cfa79650defab10bd7d9c903

Contents?: true

Size: 846 Bytes

Versions: 2

Compression:

Stored size: 846 Bytes

Contents

module Workarea
  decorate Admin::VariantViewModel, with: :product_bundles do
    def product
      @product ||=
        options[:product] || Admin::ProductViewModel.wrap(model.product)
    end

    def components
      @components ||=
        model
          .components
          .sort_by { |c| product.product_ids.index(c.product_id) || 999 }
          .map do |component|
            Admin::VariantComponentViewModel.wrap(
              component,
              product: find_product_for_component(component)
            )
          end
    end

    private

    def find_product_for_component(component)
      product.bundled_products.detect do |product|
        if component.product_id.present?
          product.id == component.product_id
        else
          product.skus.include?(component.sku)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-product_bundles-1.0.1 app/view_models/workarea/admin/variant_view_model.decorator
workarea-product_bundles-1.0.0 app/view_models/workarea/admin/variant_view_model.decorator