Module: Spree::FlowIoProductDecorator

Defined in:
app/models/spree/flow_io_product_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



6
7
8
9
10
11
# File 'app/models/spree/flow_io_product_decorator.rb', line 6

def self.prepended(base)
  base.serialize :meta, ActiveRecord::Coders::JSON.new(symbolize_keys: true)

  base.store_accessor :meta, :flow_data, :zone_ids
  base.after_save :sync_variants_with_flow
end

Instance Method Details

#add_flow_price_range(prices, product_zone) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/spree/flow_io_product_decorator.rb', line 63

def add_flow_price_range(prices, product_zone)
  flow_experience_key = product_zone&.flow_data&.[]('key')
  return prices if flow_experience_key.blank?

  master_price = master.flow_local_price(flow_experience_key)
  currency = product_zone.flow_io_experience_currency
  min = nil
  max = nil

  if variants.any?
    variants.each do |v|
      price = v.flow_local_price(flow_experience_key)
      next if price.amount.nil? || price.currency != currency

      min = [price, min].compact.min { |a, b| a.amount <=> b.amount }
      max = [price, max].compact.max { |a, b| a.amount <=> b.amount }
    end
  end

  if master_price.currency == currency
    min ||= master_price
    max ||= master_price
  end

  rmin = round_with_precision(min, 0)
  rmax = round_with_precision(max, 0)

  prices[currency] = { min: rmin, max: rmax }
  prices
end

#flow_included?(flow_exp) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'app/models/spree/flow_io_product_decorator.rb', line 25

def flow_included?(flow_exp)
  return true unless flow_exp

  flow_data["#{flow_exp.key}.excluded"].to_i != 1
end

#flow_local_price(flow_exp) ⇒ Object

returns price bound to local experience from master variant



21
22
23
# File 'app/models/spree/flow_io_product_decorator.rb', line 21

def flow_local_price(flow_exp)
  master.flow_local_price(flow_exp) || Spree::Price.new(variant_id: id, currency: 'USD', amount: 0)
end

#price_in_zone(currency, product_zone) ⇒ Object



13
14
15
16
17
18
# File 'app/models/spree/flow_io_product_decorator.rb', line 13

def price_in_zone(currency, product_zone)
  flow_experience_key = product_zone&.flow_data&.[]('key')
  return flow_local_price(flow_experience_key) if flow_experience_key.present?

  price_in(currency)
end

#price_range(product_zone, currencies = []) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/spree/flow_io_product_decorator.rb', line 31

def price_range(product_zone, currencies = [])
  prices = {}
  master_prices_with_currencies(currencies).each do |p|
    currency = p.currency
    min = nil
    max = nil

    if variants.any?
      variants.each do |v|
        price = v.price_in(currency)
        next if price.nil? || price.amount.nil?

        min = [price, min].compact.min { |a, b| a.amount <=> b.amount }
        max = [price, max].compact.max { |a, b| a.amount <=> b.amount }
      end
    else
      min = max = master.price_in(currency)
    end

    rmin = round_with_precision(min, 0)
    rmax = round_with_precision(max, 0)

    prices[currency] = { min: rmin, max: rmax }
  end

  add_flow_price_range(prices, product_zone)
end

#round_with_precision(number, precision) ⇒ Object



59
60
61
# File 'app/models/spree/flow_io_product_decorator.rb', line 59

def round_with_precision(number, precision)
  number&.amount&.to_s(:rounded, precision: precision) || 0
end

#sync_variants_with_flowObject



94
95
96
# File 'app/models/spree/flow_io_product_decorator.rb', line 94

def sync_variants_with_flow
  variants_including_master.each(&:sync_product_to_flow)
end