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, user: nil, quantity: nil, date: nil, order: nil) ⇒ Context

Initializes the context

Parameters:

  • variant (Spree::Variant) (defaults to: nil)
  • currency (String)
  • store (Spree::Store) (defaults to: nil)
  • zone (Spree::Zone) (defaults to: nil)
  • user (Spree::User) (defaults to: nil)
  • quantity (Integer) (defaults to: nil)
  • date (Time) (defaults to: nil)
  • order (Spree::Order) (defaults to: nil)


15
16
17
18
19
20
21
22
23
24
# File 'lib/spree/core/pricing/context.rb', line 15

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

Instance Attribute Details

#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

#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:



30
31
32
# File 'lib/spree/core/pricing/context.rb', line 30

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

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



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spree/core/pricing/context.rb', line 34

def self.from_order(variant, order, quantity: nil)
  new(
    variant: variant,
    currency: order.currency,
    store: order.store,
    zone: order.tax_zone || order.store.checkout_zone,
    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)


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/spree/core/pricing/context.rb', line 48

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