Class: Spree::Variant::PriceSelector
- Inherits:
-
Object
- Object
- Spree::Variant::PriceSelector
- 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
-
#variant ⇒ Object
readonly
Returns the value of attribute variant.
Class Method Summary collapse
-
.pricing_options_class ⇒ Object
The pricing options represent “given circumstances” for a price: The currency we need and the country that the price applies to.
Instance Method Summary collapse
-
#initialize(variant) ⇒ PriceSelector
constructor
A new instance of PriceSelector.
-
#price_for(price_options) ⇒ Spree::Money?
The variant’s price, given a set of pricing options.
Constructor Details
#initialize(variant) ⇒ PriceSelector
Returns a new instance of PriceSelector.
19 20 21 |
# File 'app/models/spree/variant/price_selector.rb', line 19 def initialize(variant) @variant = variant end |
Instance Attribute Details
#variant ⇒ Object (readonly)
Returns the value of attribute variant.
17 18 19 |
# File 'app/models/spree/variant/price_selector.rb', line 17 def variant @variant end |
Class Method Details
.pricing_options_class ⇒ Object
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.
13 14 15 |
# File 'app/models/spree/variant/price_selector.rb', line 13 def self. Spree::Variant::PricingOptions end |
Instance Method Details
#price_for(price_options) ⇒ Spree::Money?
The variant’s price, given a set of pricing options
26 27 28 29 30 31 32 |
# File 'app/models/spree/variant/price_selector.rb', line 26 def price_for() variant.currently_valid_prices.detect do |price| ( price.country_iso == .desired_attributes[:country_iso] || price.country_iso.nil? ) && price.currency == .desired_attributes[:currency] end.try!(:money) end |