Module: SpreeCmCommissioner::PromotionDecorator

Defined in:
app/models/spree_cm_commissioner/promotion_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



3
4
5
6
7
8
9
# File 'app/models/spree_cm_commissioner/promotion_decorator.rb', line 3

def self.prepended(base)
  base.attr_accessor :auto_apply
  base.before_validation :setup_auto_apply_promotion, if: :auto_apply?

  base.has_one :store_promotion, class_name: 'Spree::StorePromotion'
  base.has_one :default_store, through: :store_promotion, source: :store, class_name: 'Spree::Store'
end

Instance Method Details

#adjusted_credits_count(promotable) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/spree_cm_commissioner/promotion_decorator.rb', line 50

def adjusted_credits_count(promotable)
  # credits() above excludes incomplete orders, so don't subtract an
  # incomplete promotable's own adjustments from credits_count — they
  # were never part of that count to begin with.
  order = promotable.is_a?(Spree::Order) ? promotable : promotable.order
  if order.completed?
    super
  else
    credits_count
  end
end

#auto_apply?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'app/models/spree_cm_commissioner/promotion_decorator.rb', line 16

def auto_apply?
  if auto_apply.present?
    auto_apply.in?([true, 'true', '1'])
  else
    code.nil? && path.nil?
  end
end

#creditsObject

Only count usage toward usage_limit for orders that actually completed (paid). Without this, an abandoned/incomplete cart that applied the coupon permanently consumes the limit even though nothing was redeemed.



46
47
48
# File 'app/models/spree_cm_commissioner/promotion_decorator.rb', line 46

def credits
  super.for_complete_order
end

#date_eligible?(date) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
# File 'app/models/spree_cm_commissioner/promotion_decorator.rb', line 24

def date_eligible?(date)
  rules = promotion_rules.select { |rule| rule.respond_to?(:date_eligible?) }

  if match_all?
    rules.all? { |rule| rule.date_eligible?(date) }
  else
    rules.any? { |rule| rule.date_eligible?(date) }
  end
end

#guest_eligible?(guest, line_item) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'app/models/spree_cm_commissioner/promotion_decorator.rb', line 34

def guest_eligible?(guest, line_item)
  rules = promotion_rules.select { |rule| rule.respond_to?(:guest_eligible?) }
  if match_all?
    rules.all? { |rule| rule.guest_eligible?(guest, line_item) }
  else
    rules.any? { |rule| rule.guest_eligible?(guest, line_item) }
  end
end

#setup_auto_apply_promotionObject



11
12
13
14
# File 'app/models/spree_cm_commissioner/promotion_decorator.rb', line 11

def setup_auto_apply_promotion
  self.code = nil
  self.path = nil
end