Class: Spree::Adjustable::PromotionAccumulator

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/adjustable/promotion_accumulator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adjustable) ⇒ PromotionAccumulator

Returns a new instance of PromotionAccumulator.



17
18
19
20
21
22
23
# 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

#adjustmentsObject (readonly)

Returns the value of attribute adjustments.



4
5
6
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 4

def adjustments
  @adjustments
end

#promotionsObject (readonly)

Returns the value of attribute promotions.



4
5
6
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 4

def promotions
  @promotions
end

#sourcesObject (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



25
26
27
28
29
30
31
32
33
34
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 25

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



50
51
52
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 50

def item_total_with_promotion(promotion_id)
  amount + promo_total(promotion_id, item_adjustments)
end

#promo_total(*args) ⇒ Object



42
43
44
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 42

def promo_total(*args)
  promotions_adjustments(*args).map(&:amount).reduce(0, &:+)
end

#promotions_adjustments(promotion_id, adjustments = self.adjustments) ⇒ Object



36
37
38
39
40
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 36

def promotions_adjustments(promotion_id, adjustments = self.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



46
47
48
# File 'app/models/spree/adjustable/promotion_accumulator.rb', line 46

def total_with_promotion(promotion_id)
  amount + ship_total + promo_total(promotion_id)
end