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:

See Also:

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (PricingOptions) initialize(desired_attributes = {})

Returns a new instance of PricingOptions



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

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

Instance Attribute Details

- (Hash) desired_attributes (readonly)

Returns The hash of exact desired attributes

Returns:

  • (Hash)

    The hash of exact desired attributes



51
52
53
# File 'app/models/spree/variant/pricing_options.rb', line 51

def desired_attributes
  @desired_attributes
end

Class Method Details

+ (Hash) default_price_attributes

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:



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

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

+ (Spree::Variant::PricingOptions) from_line_item(line_item)

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

Returns:

See Also:



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

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

+ (Spree::Variant::PricingOptions) from_price(price)

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


46
47
48
# File 'app/models/spree/variant/pricing_options.rb', line 46

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

Instance Method Details

- (String) cache_key

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



91
92
93
# File 'app/models/spree/variant/pricing_options.rb', line 91

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

- (String?) country_iso

Shorthand for accessing the country part of the desired attributes

Returns:

  • (String, nil)

    two-digit country code or nil



82
83
84
# File 'app/models/spree/variant/pricing_options.rb', line 82

def country_iso
  desired_attributes[:country_iso]
end

- (String?) currency

Shorthand for accessing the currency part of the desired attributes

Returns:

  • (String, nil)

    three-digit currency code or nil



75
76
77
# File 'app/models/spree/variant/pricing_options.rb', line 75

def currency
  desired_attributes[:currency]
end

- (Hash) search_arguments

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:



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

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