Sha256: 75a73e002c7cec7ddad086bb3ee778349e57b609e068e67593ea52e500b16114

Contents?: true

Size: 1.28 KB

Versions: 62

Compression:

Stored size: 1.28 KB

Contents

module Workarea
  module Storefront
    class ProductViewModel
      class SkuOptions
        def initialize(variants)
          @variants = variants
        end

        def to_a
          if options.one?
            options
          else
            [[::I18n.t('workarea.storefront.products.select_options'), nil]] +
              options
          end
        end

        private

        def options
          @variants.map { |variant| [text_for_variant(variant), variant.sku, options_for_variant(variant)] }
        end

        def options_for_variant(variant)
          {
            data: {
              sku_option_details: json_details_for_variant(variant)
            }
          }
        end

        def json_details_for_variant(variant)
          return {}.to_json unless variant.details.present?
          variant.details.map { |k, v| [k.systemize, v] }.to_h.to_json
        end

        def text_for_variant(variant)
          if variant.name != variant.sku
            variant.name
          elsif variant.details.blank?
            variant.sku
          else
            details = variant.details.map do |k, v|
              "#{k.titleize}: #{[v].flatten.join(', ')}"
            end

            "#{variant.sku} - #{details.join(', ')}"
          end
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-storefront-3.4.27 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.5.4 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.26 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.5.3 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.25 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.5.2 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.24 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.5.1 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.23 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.22 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.5.0 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.21 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.5.0.beta.1 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.20 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.19 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.18 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.17 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.16 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.15 app/view_models/workarea/storefront/product_view_model/sku_options.rb
workarea-storefront-3.4.14 app/view_models/workarea/storefront/product_view_model/sku_options.rb