Class: Spree::Variant::PricingOptions

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/variant/pricing_options.rb

Overview

Instances of this class represent the set of circumstances that influence how expensive a variant is. For this particular pricing options class, country_iso and currency influence the price of a variant.

Pricing options can be instantiated from a line item or from the view context:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(desired_attributes = {}) ⇒ PricingOptions

Returns a new instance of PricingOptions.



65
66
67
# File 'app/models/spree/variant/pricing_options.rb', line 65

def initialize(desired_attributes = {})
  @desired_attributes = self.class.default_price_attributes.merge(desired_attributes)
end

Instance Attribute Details

#desired_attributesHash (readonly)

Returns The hash of exact desired attributes.

Returns:

  • (Hash)

    The hash of exact desired attributes



63
64
65
# File 'app/models/spree/variant/pricing_options.rb', line 63

def desired_attributes
  @desired_attributes
end

Class Method Details

.default_price_attributesHash

When editing variants in the admin, this is the standard price the admin interacts with: The price in the admin’s globally configured currency, for the admin’s globally configured country. These options get merged with any options the user provides when instantiating new pricing options.

Returns:

  • (Hash)

    The attributes that admin prices usually have

See Also:



22
23
24
25
26
27
# File 'app/models/spree/variant/pricing_options.rb', line 22

def self.default_price_attributes
  {
    currency: Spree::Config.currency,
    country_iso: Spree::Config.admin_vat_country_iso
  }
end

.from_context(context) ⇒ Spree::Variant::PricingOptions

This creates the correct pricing options for a price, so the store owners can easily customize how to find the pricing based on the view context, having available current_store, current_spree_user, request.host_name, etc.

Returns:



55
56
57
58
59
60
# File 'app/models/spree/variant/pricing_options.rb', line 55

def self.from_context(context)
  new(
    currency: context.current_store.try!(:default_currency).presence || Spree::Config[:currency],
    country_iso: context.current_store.try!(:cart_tax_country_iso).presence
  )
end

.from_line_item(line_item) ⇒ Spree::Variant::PricingOptions

This creates the correct pricing options for a line item, taking into account its currency and tax address country, if available.

Returns:

See Also:



35
36
37
38
39
40
41
# File 'app/models/spree/variant/pricing_options.rb', line 35

def self.from_line_item(line_item)
  tax_address = line_item.order.try!(:tax_address)
  new(
    currency: line_item.currency || Spree::Config.currency,
    country_iso: tax_address && tax_address.country.try!(:iso)
  )
end

.from_price(price) ⇒ Spree::Variant::PricingOptions

This creates the correct pricing options for a price, so that we can easily find other prices with the same pricing-relevant attributes and mark them as non-default.

Returns:

See Also:

  • Price#set_default_price


48
49
50
# File 'app/models/spree/variant/pricing_options.rb', line 48

def self.from_price(price)
  new(currency: price.currency, country_iso: price.country_iso)
end

Instance Method Details

#cache_keyString

Since the current pricing options determine the price to be shown to users, product pages have to be cached and their caches invalidated using the data from this object. This method makes it easy to use with Rails ‘cache` helper.

Returns:

  • (String)

    cache key to be used in views



103
104
105
# File 'app/models/spree/variant/pricing_options.rb', line 103

def cache_key
  desired_attributes.values.select(&:present?).map(&:to_s).join("/")
end

#country_isoString?

Shorthand for accessing the country part of the desired attributes

Returns:

  • (String, nil)

    two-digit country code or nil



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

def country_iso
  desired_attributes[:country_iso]
end

#currencyString?

Shorthand for accessing the currency part of the desired attributes

Returns:

  • (String, nil)

    three-digit currency code or nil



87
88
89
# File 'app/models/spree/variant/pricing_options.rb', line 87

def currency
  desired_attributes[:currency]
end

#search_argumentsHash

A slightly modified version of the ‘desired_attributes` Hash. Instead of having “nil” or an actual country ISO code under the `:country_iso` key, this creates an array under the country_iso key that includes both the actual country iso we want and nil as a shorthand for the fallback price. This is useful so that we can determine the availability of variants by price:

Returns:

  • (Hash)

    arguments to be passed into ActiveRecord.where()

See Also:



78
79
80
81
82
# File 'app/models/spree/variant/pricing_options.rb', line 78

def search_arguments
  search_arguments = desired_attributes
  search_arguments[:country_iso] = [desired_attributes[:country_iso], nil].flatten.uniq
  search_arguments
end