Class: Spree::PromotionHandler::Coupon

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/promotion_handler/coupon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ Coupon

Returns a new instance of Coupon.



9
10
11
12
# File 'app/models/spree/promotion_handler/coupon.rb', line 9

def initialize(order)
  @order = order
  @coupon_code = order.coupon_code && order.coupon_code.downcase
end

Instance Attribute Details

#coupon_codeObject (readonly)

Returns the value of attribute coupon_code.



6
7
8
# File 'app/models/spree/promotion_handler/coupon.rb', line 6

def coupon_code
  @coupon_code
end

#errorObject

Returns the value of attribute error.



7
8
9
# File 'app/models/spree/promotion_handler/coupon.rb', line 7

def error
  @error
end

#orderObject (readonly)

Returns the value of attribute order.



6
7
8
# File 'app/models/spree/promotion_handler/coupon.rb', line 6

def order
  @order
end

#status_codeObject

Returns the value of attribute status_code.



7
8
9
# File 'app/models/spree/promotion_handler/coupon.rb', line 7

def status_code
  @status_code
end

#successObject

Returns the value of attribute success.



7
8
9
# File 'app/models/spree/promotion_handler/coupon.rb', line 7

def success
  @success
end

Instance Method Details

#applyObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/spree/promotion_handler/coupon.rb', line 18

def apply
  if coupon_code.present?
    if promotion.present? && promotion.active? && promotion.actions.exists?
      handle_present_promotion(promotion)
    elsif promotion_code&.promotion&.expired?
      set_error_code :coupon_code_expired
    else
      set_error_code :coupon_code_not_found
    end
  end

  self
end

#can_apply?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'app/models/spree/promotion_handler/coupon.rb', line 14

def can_apply?
  Spree::Promotion.order_activatable?(order)
end

#promotionObject



56
57
58
59
60
# File 'app/models/spree/promotion_handler/coupon.rb', line 56

def promotion
  @promotion ||= if promotion_code && promotion_code.promotion.active?
    promotion_code.promotion
  end
end

#removeObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/spree/promotion_handler/coupon.rb', line 32

def remove
  if promotion.blank?
    set_error_code :coupon_code_not_found
  elsif !promotion_exists_on_order?(order, promotion)
    set_error_code :coupon_code_not_present
  else
    promotion.remove_from(order)
    order.recalculate
    set_success_code :coupon_code_removed
  end

  self
end

#set_error_code(status_code, options = {}) ⇒ Object



51
52
53
54
# File 'app/models/spree/promotion_handler/coupon.rb', line 51

def set_error_code(status_code, options = {})
  @status_code = status_code
  @error = options[:error] || I18n.t(status_code, scope: 'spree')
end

#set_success_code(status_code) ⇒ Object



46
47
48
49
# File 'app/models/spree/promotion_handler/coupon.rb', line 46

def set_success_code(status_code)
  @status_code = status_code
  @success = I18n.t(status_code, scope: 'spree')
end

#successful?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/spree/promotion_handler/coupon.rb', line 62

def successful?
  success.present? && error.blank?
end