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



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

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



53
54
55
# File 'app/models/spree/variant/pricing_options.rb', line 53

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:

  • AppConfiguration.new.default_pricing_options
  • #initialize


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_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



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

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



84
85
86
# File 'app/models/spree/variant/pricing_options.rb', line 84

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



77
78
79
# File 'app/models/spree/variant/pricing_options.rb', line 77

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:



68
69
70
71
72
# File 'app/models/spree/variant/pricing_options.rb', line 68

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