Class: Spree::Variant::PriceSelector

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

Overview

This class is responsible for selecting a price for a variant given certain pricing options. A variant can have multiple or even dynamic prices. The ‘price_for` method determines which price applies under the given circumstances.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variant) ⇒ PriceSelector

Returns a new instance of PriceSelector.



21
22
23
# File 'app/models/spree/variant/price_selector.rb', line 21

def initialize(variant)
  @variant = variant
end

Instance Attribute Details

#variantObject (readonly)

Returns the value of attribute variant.



19
20
21
# File 'app/models/spree/variant/price_selector.rb', line 19

def variant
  @variant
end

Class Method Details

.pricing_options_classObject

The pricing options represent “given circumstances” for a price: The currency we need and the country that the price applies to. Every price selector is designed to work with a particular set of pricing options embodied in it’s pricing options class.



15
16
17
# File 'app/models/spree/variant/price_selector.rb', line 15

def self.pricing_options_class
  Spree::Variant::PricingOptions
end

Instance Method Details

#price_for(price_options) ⇒ Spree::Money?

The variant’s price, given a set of pricing options

Parameters:

Returns:

  • (Spree::Money, nil)

    The most specific price for this set of pricing options.



28
29
30
31
32
33
34
# File 'app/models/spree/variant/price_selector.rb', line 28

def price_for(price_options)
  variant.currently_valid_prices.detect do |price|
    ( price.country_iso == price_options.desired_attributes[:country_iso] ||
      price.country_iso.nil?
    ) && price.currency == price_options.desired_attributes[:currency]
  end.try!(:money)
end