Class: Spree::Pricing::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/core/pricing/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variant: nil, currency:, store: nil, zone: nil, market: nil, channel: nil, user: nil, quantity: nil, date: nil, order: nil) ⇒ Context

Initializes the context

Parameters:



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spree/core/pricing/context.rb', line 17

def initialize(variant: nil, currency:, store: nil, zone: nil, market: nil, channel: nil, user: nil, quantity: nil, date: nil, order: nil)
  @variant = variant
  @currency = currency
  @store = store || Spree::Current.store
  @zone = zone || Spree::Current.zone
  @market = market || Spree::Current.market
  @channel = channel || Spree::Current.channel
  @user = user
  @quantity = quantity
  @date = date || Time.current
  @order = order
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



4
5
6
# File 'lib/spree/core/pricing/context.rb', line 4

def channel
  @channel
end

#currencyObject (readonly)

Returns the value of attribute currency.



4
5
6
# File 'lib/spree/core/pricing/context.rb', line 4

def currency
  @currency
end

#dateObject (readonly)

Returns the value of attribute date.



4
5
6
# File 'lib/spree/core/pricing/context.rb', line 4

def date
  @date
end

#marketObject (readonly)

Returns the value of attribute market.



4
5
6
# File 'lib/spree/core/pricing/context.rb', line 4

def market
  @market
end

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'lib/spree/core/pricing/context.rb', line 4

def order
  @order
end

#quantityObject (readonly)

Returns the value of attribute quantity.



4
5
6
# File 'lib/spree/core/pricing/context.rb', line 4

def quantity
  @quantity
end

#storeObject (readonly)

Returns the value of attribute store.



4
5
6
# File 'lib/spree/core/pricing/context.rb', line 4

def store
  @store
end

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'lib/spree/core/pricing/context.rb', line 4

def user
  @user
end

#variantObject (readonly)

Returns the value of attribute variant.



4
5
6
# File 'lib/spree/core/pricing/context.rb', line 4

def variant
  @variant
end

#zoneObject (readonly)

Returns the value of attribute zone.



4
5
6
# File 'lib/spree/core/pricing/context.rb', line 4

def zone
  @zone
end

Class Method Details

.from_currency(variant, currency) ⇒ Spree::Pricing::Context

Returns a new context from a variant and currency

Parameters:

Returns:



34
35
36
# File 'lib/spree/core/pricing/context.rb', line 34

def self.from_currency(variant, currency)
  new(variant: variant, currency: currency)
end

.from_order(variant, order, quantity: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spree/core/pricing/context.rb', line 38

def self.from_order(variant, order, quantity: nil)
  new(
    variant: variant,
    currency: order.currency,
    store: order.store,
    zone: order.tax_zone || Spree::Zone.default_tax,
    channel: order.channel,
    user: order.user,
    quantity: quantity || order.line_items.find_by(variant: variant)&.quantity,
    order: order
  )
end

Instance Method Details

#cache_keyString

Returns the cache key for the context

Returns:

  • (String)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/spree/core/pricing/context.rb', line 53

def cache_key
  [
    'spree',
    'pricing',
    variant.id,
    currency,
    store&.id,
    zone&.id,
    market&.id,
    channel&.id,
    user&.id,
    quantity,
    date&.to_i
  ].compact.join('/')
end