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

#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

#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