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

Class Method Details

.advertisedObject



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

def self.advertised
  where(:advertise => true)
end

.with_codeObject



40
41
42
# File 'app/models/spree/promotion.rb', line 40

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

Instance Method Details

#activate(payload) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/spree/promotion.rb', line 44

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

  if code.present?
     = Spree::Promotion.normalize_coupon_code(payload[:coupon_code])
    normalized_expected_code = Spree::Promotion.normalize_coupon_code(self.code)
    return unless  == normalized_expected_code
  end

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

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

#adjusted_credits_count(order) ⇒ Object



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

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

#creditsObject



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

def credits
  Adjustment.promotion.where(:originator_id => actions.pluck(:id))
end

#credits_countObject



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

def credits_count
  credits.count
end

#eligible?(order) ⇒ Boolean

called anytime order.update! happens

Returns:

  • (Boolean)


63
64
65
66
# File 'app/models/spree/promotion.rb', line 63

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

#order_activatable?(order) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#productsObject

Products assigned to all product rules



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

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

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

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
# File 'app/models/spree/promotion.rb', line 68

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



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

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

#usage_limit_exceeded?(order = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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