Class: Workarea::Storefront::ProductViewModel::OptionSet

Inherits:
Object
  • Object
show all
Defined in:
app/view_models/workarea/storefront/product_view_model/option_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(product, options = {}) ⇒ OptionSet

Returns a new instance of OptionSet.



13
14
15
16
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 13

def initialize(product, options = {})
  @product = product
  @options = options.transform_keys { |k| k.to_s.optionize }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 5

def options
  @options
end

#productObject (readonly)

Returns the value of attribute product.



5
6
7
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 5

def product
  @product
end

Class Method Details

.from_sku(product, sku, options = {}) ⇒ Object



7
8
9
10
11
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 7

def self.from_sku(product, sku, options = {})
  matching_variant = product.variants.detect { |v| v.sku == sku }
  sku_options = matching_variant&.details&.transform_values(&:first) || {}
  new(product, sku_options.merge(options))
end

Instance Method Details

#all_optionsObject



18
19
20
21
22
23
24
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 18

def all_options
  @all_options ||= product
    .variants
    .flat_map { |v| v.details.keys }
    .uniq
    .map { |o| o.to_s.optionize }
end

#current_skuObject



67
68
69
70
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 67

def current_sku
  return nil if currently_selected_options.blank?
  current_variant.try(:sku)
end

#current_variantObject



58
59
60
61
62
63
64
65
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 58

def current_variant
  return nil if currently_selected_options.blank?

  @current_variant ||= product.variants.detect do |variant|
    variant.details.size == currently_selected_options.size &&
      variant.matches_details?(currently_selected_options)
  end
end

#currently_selected_optionsObject



26
27
28
29
30
31
32
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 26

def currently_selected_options
  @currently_selected_options ||= options_with_one_value.merge(
    options.select do |key|
      key.optionize.in?(all_options) && options[key.optionize].present?
    end
  )
end

#options_for_selectionObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 34

def options_for_selection
  @options_for_selection ||=
    begin
      result = all_options.reduce([]) do |memo, key|
        variants_with_this = product.variants.select { |v| v.has_detail?(key) }
        all_values = variants_with_this.map { |v| v.fetch_detail(key) }.flatten.uniq
        values = values_based_on_other_options(key, all_values)

        if values.any?
          memo << Option.new(
            product,
            key,
            values,
            currently_selected_options
          )
        end

        memo
      end

      Workarea.config.option_selections_sort.call(product, result)
    end
end