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


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


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

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

#promo_total(*args) ⇒ Object


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

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

#promotions_adjustments(promotion_id, adjustments = adjustments) ⇒ Object


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

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


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

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