Class: Spree::Promotion

Inherits:
Activator
  • Object
show all
Defined in:
app/models/spree/promotion.rb,
app/models/spree/promotion/rules/user.rb,
app/models/spree/promotion/rules/product.rb,
app/models/spree/promotion/rules/item_total.rb,
app/models/spree/promotion/rules/first_order.rb,
app/models/spree/promotion/rules/user_logged_in.rb,
app/models/spree/promotion/actions/create_adjustment.rb,
app/models/spree/promotion/actions/create_line_items.rb

Defined Under Namespace

Modules: Actions, Rules

Constant Summary collapse

MATCH_POLICIES =
%w(all any)
UNACTIVATABLE_ORDER_STATES =
["complete", "awaiting_return", "returned"]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Activator

active, #expired?, register_event_name

Class Method Details

.advertisedObject


31
32
33
# File 'app/models/spree/promotion.rb', line 31

def self.advertised
  where(advertise: true)
end

.with_codeObject


35
36
37
# File 'app/models/spree/promotion.rb', line 35

def self.with_code
  where(event_name: 'spree.checkout.coupon_code_added')
end

Instance Method Details

#activate(payload) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/spree/promotion.rb', line 39

def activate(payload)
  return unless order_activatable? payload[:order]

  # make sure code is always downcased (old databases might have mixed case codes)
  if code.present?
    event_code = payload[:coupon_code]
    return unless event_code == self.code.downcase.strip
  end

  if path.present?
    return unless path == payload[:path]
  end

  actions.each do |action|
    action.perform(payload)
  end
end

#adjusted_credits_count(order) ⇒ Object


88
89
90
91
# File 'app/models/spree/promotion.rb', line 88

def adjusted_credits_count(order)
  return credits_count if order.nil?
  credits_count - (order.promotion_credit_exists?(self) ? 1 : 0)
end

#code=(coupon_code) ⇒ Object


101
102
103
# File 'app/models/spree/promotion.rb', line 101

def code=(coupon_code)
  write_attribute(:code, (coupon_code.downcase.strip rescue nil))
end

#creditsObject


93
94
95
# File 'app/models/spree/promotion.rb', line 93

def credits
  Adjustment.promotion.where(originator_id: actions.map(&:id))
end

#credits_countObject


97
98
99
# File 'app/models/spree/promotion.rb', line 97

def credits_count
  credits.count
end

#eligible?(order) ⇒ Boolean

called anytime order.update! happens


58
59
60
61
# File 'app/models/spree/promotion.rb', line 58

def eligible?(order)
  return false if expired? || usage_limit_exceeded?(order)
  rules_are_eligible?(order, {})
end

#order_activatable?(order) ⇒ Boolean


73
74
75
# File 'app/models/spree/promotion.rb', line 73

def order_activatable?(order)
  order && !UNACTIVATABLE_ORDER_STATES.include?(order.state)
end

#productsObject

Products assigned to all product rules


78
79
80
81
82
# File 'app/models/spree/promotion.rb', line 78

def products
  @products ||= self.rules.to_a.inject([]) do |products, rule|
    rule.respond_to?(:products) ? products << rule.products : products
  end.flatten.uniq
end

#rules_are_eligible?(order, options = {}) ⇒ Boolean


63
64
65
66
67
68
69
70
71
# File 'app/models/spree/promotion.rb', line 63

def rules_are_eligible?(order, options = {})
  return true if rules.none?
  eligible = lambda { |r| r.eligible?(order, options) }
  if match_policy == 'all'
    rules.all?(&eligible)
  else
    rules.any?(&eligible)
  end
end

#save_rules_and_actionsObject


27
28
29
# File 'app/models/spree/promotion.rb', line 27

def save_rules_and_actions
  (rules + actions).each &:save
end

#usage_limit_exceeded?(order = nil) ⇒ Boolean


84
85
86
# File 'app/models/spree/promotion.rb', line 84

def usage_limit_exceeded?(order = nil)
  usage_limit.present? && usage_limit > 0 && adjusted_credits_count(order) >= usage_limit
end