Class: Spree::Adjustable::PromotionAccumulator
- Inherits:
-
Object
- Object
- Spree::Adjustable::PromotionAccumulator
- Defined in:
- app/models/spree/adjustable/promotion_accumulator.rb
Instance Attribute Summary collapse
-
#adjustments ⇒ Object
readonly
Returns the value of attribute adjustments.
-
#promotions ⇒ Object
readonly
Returns the value of attribute promotions.
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
Class Method Summary collapse
-
.add_to(adjustable) ⇒ Object
Adds accumulator as an attribute of adjustable to avoid changing number of passed arguments to adjustment#update! and concequential methods.
Instance Method Summary collapse
- #add_adjustment(adjustment, opts = {}) ⇒ Object
-
#initialize(adjustable) ⇒ PromotionAccumulator
constructor
A new instance of PromotionAccumulator.
- #item_total_with_promotion(promotion_id) ⇒ Object
- #promo_total(*args) ⇒ Object
- #promotions_adjustments(promotion_id, adjustments = adjustments()) ⇒ Object
- #total_with_promotion(promotion_id) ⇒ Object
Constructor Details
#initialize(adjustable) ⇒ PromotionAccumulator
Returns a new instance of PromotionAccumulator.
17 18 19 20 21 |
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 17 def initialize(adjustable) @adjustable = adjustable @adjustments, @sources, @promotions = [], [], [] all_adjustments.each { |a| add_adjustment(a) } end |
Instance Attribute Details
#adjustments ⇒ Object (readonly)
Returns the value of attribute adjustments.
4 5 6 |
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 4 def adjustments @adjustments end |
#promotions ⇒ Object (readonly)
Returns the value of attribute promotions.
4 5 6 |
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 4 def promotions @promotions end |
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
4 5 6 |
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 4 def sources @sources end |
Class Method Details
.add_to(adjustable) ⇒ Object
Adds accumulator as an attribute of adjustable to avoid changing number of passed arguments to adjustment#update! and concequential methods
9 10 11 12 13 14 15 |
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 9 def self.add_to(adjustable) class << adjustable attr_accessor :promotion_accumulator end adjustable.promotion_accumulator = new(adjustable) end |
Instance Method Details
#add_adjustment(adjustment, opts = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 23 def add_adjustment(adjustment, opts = {}) return unless adjustment.promotion? source = opts[:source] || adjustment.source promotion = opts[:promotion] || source.promotion add(adjustments, adjustment, adjustment.id) add(sources, source, adjustment.source_id) add(promotions, promotion, source.promotion_id) end |
#item_total_with_promotion(promotion_id) ⇒ Object
47 48 49 |
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 47 def item_total_with_promotion(promotion_id) amount + promo_total(promotion_id, item_adjustments) end |
#promo_total(*args) ⇒ Object
39 40 41 |
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 39 def promo_total(*args) promotions_adjustments(*args).map(&:amount).reduce(0, &:+) end |
#promotions_adjustments(promotion_id, adjustments = adjustments()) ⇒ Object
33 34 35 36 37 |
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 33 def promotions_adjustments(promotion_id, adjustments = adjustments()) where(sources, promotion_id: promotion_id).map do |source| where(adjustments, source_id: source.id) end.flatten end |
#total_with_promotion(promotion_id) ⇒ Object
43 44 45 |
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 43 def total_with_promotion(promotion_id) amount + ship_total + promo_total(promotion_id) end |