Sha256: 512a393389181778bc9a9693d3bbceaff0cf966e509c2feb25f291e9c29de230

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

module Workarea
  decorate(Storefront::ProductViewModel, with: :inventory_notifications) do

    def unavailable_sku_options
      @unavailable_sku_options ||= format_variant_options(unavailable_skus)
    end

    def unavailable_skus
      @unavailable_skus ||= variants.select do |variant|
        !inventory.for_sku(variant.sku).purchasable?
      end
    end

    def current_sku_unavailable?
      unavailable_skus.any? { |sku| sku.sku == current_sku }
    end

    def purchasable?
      super && !current_sku_unavailable?
    end

    private

    def format_variant_options variants
      variant_options = variants.map do |variant|
        text = if variant.details.blank?
                 variant.sku
               else
                  sku = variant.sku
                   details = variant.details.map do |k, v|
                     "#{k.titleize}: #{[v].flatten.join(', ')}"
                   end.join(", ")
                   "#{sku} - #{details}"
               end
        [ text, variant.sku ]
      end

      if variant_options.one?
        variant_options
      else
        [['Please select options', nil ]] + variant_options
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
workarea-inventory_notifications-2.1.5 app/view_models/product_view_model.decorator
workarea-inventory_notifications-2.1.4 app/view_models/product_view_model.decorator
workarea-inventory_notifications-2.1.3 app/view_models/product_view_model.decorator
workarea-inventory_notifications-2.1.2 app/view_models/product_view_model.decorator