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.



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

def initialize(order)
  @order = order
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



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

def error
  @error
end

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'app/models/spree/promotion_handler/coupon.rb', line 4

def order
  @order
end

#status_codeObject

Returns the value of attribute status_code.



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

def status_code
  @status_code
end

#successObject

Returns the value of attribute success.



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

def success
  @success
end

Instance Method Details

#applyObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/spree/promotion_handler/coupon.rb', line 11

def apply
  if order.coupon_code.present?
    if promotion.present? && promotion.actions.exists?
      handle_present_promotion
    elsif Promotion.with_coupon_code(order.coupon_code).try(:expired?)
      set_error_code :coupon_code_expired
    else
      set_error_code :coupon_code_not_found
    end
  else
    set_error_code :coupon_code_not_found
  end
  self
end

#promotionObject



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

def promotion
  @promotion ||= Promotion.active.includes(
    :promotion_rules, :promotion_actions
  ).with_coupon_code(order.coupon_code)
end

#remove(coupon_code) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/spree/promotion_handler/coupon.rb', line 26

def remove(coupon_code)
  promotion = order.promotions.with_coupon_code(coupon_code)

  if promotion.present?
    # Order promotion has to be destroyed before line item removing
    order.order_promotions.find_by!(promotion_id: promotion.id).destroy

    remove_promotion_adjustments(promotion)
    remove_promotion_line_items(promotion)
    order.update_with_updater!

    set_success_code :adjustments_deleted
  else
    set_error_code :coupon_code_not_found
  end
  self
end

#set_error_code(code) ⇒ Object



49
50
51
52
# File 'app/models/spree/promotion_handler/coupon.rb', line 49

def set_error_code(code)
  @status_code = code
  @error = Spree.t(code)
end

#set_success_code(code) ⇒ Object



44
45
46
47
# File 'app/models/spree/promotion_handler/coupon.rb', line 44

def set_success_code(code)
  @status_code = code
  @success = Spree.t(code)
end

#successful?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/spree/promotion_handler/coupon.rb', line 60

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