Class: Workarea::Storefront::ProductViewModel::OptionSet
- Inherits:
-
Object
- Object
- Workarea::Storefront::ProductViewModel::OptionSet
- Defined in:
- app/view_models/workarea/storefront/product_view_model/option_set.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#product ⇒ Object
readonly
Returns the value of attribute product.
Class Method Summary collapse
Instance Method Summary collapse
- #all_options ⇒ Object
- #current_sku ⇒ Object
- #current_variant ⇒ Object
- #currently_selected_options ⇒ Object
-
#initialize(product, options = {}) ⇒ OptionSet
constructor
A new instance of OptionSet.
- #options_for_selection ⇒ Object
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, = {}) @product = product = .transform_keys { |k| k.to_s.optionize } end |
Instance Attribute Details
#options ⇒ Object (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 end |
#product ⇒ Object (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, = {}) matching_variant = product.variants.detect { |v| v.sku == sku } = matching_variant&.details&.transform_values(&:first) || {} new(product, .merge()) end |
Instance Method Details
#all_options ⇒ Object
18 19 20 21 22 23 24 |
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 18 def ||= product .variants .flat_map { |v| v.details.keys } .uniq .map { |o| o.to_s.optionize } end |
#current_sku ⇒ Object
67 68 69 70 |
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 67 def current_sku return nil if .blank? current_variant.try(:sku) end |
#current_variant ⇒ Object
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 .blank? @current_variant ||= product.variants.detect do |variant| variant.details.size == .size && variant.matches_details?() end end |
#currently_selected_options ⇒ Object
26 27 28 29 30 31 32 |
# File 'app/view_models/workarea/storefront/product_view_model/option_set.rb', line 26 def ||= .merge( .select do |key| key.optionize.in?() && [key.optionize].present? end ) end |
#options_for_selection ⇒ Object
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 ||= begin result = .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 = (key, all_values) if values.any? memo << Option.new( product, key, values, ) end memo end Workarea.config.option_selections_sort.call(product, result) end end |