Class: Spree::CouponCode

Inherits:
Object
  • Object
show all
Includes:
Security::CouponCodes
Defined in:
app/models/spree/coupon_code.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.used?(code) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/spree/coupon_code.rb', line 38

def self.used?(code)
  used_with_code(code).any?
end

Instance Method Details

#apply_order!(owner) ⇒ Boolean

Attaches the code to the cart or order presenting it. The code is not spent here: it stays unused until an order is placed with it, so a code left on an abandoned cart can still be presented by someone else.

Parameters:

Returns:

  • (Boolean)


48
49
50
51
# File 'app/models/spree/coupon_code.rb', line 48

def apply_order!(owner)
  cart = owner.is_a?(Spree::Cart) ? owner : nil
  update(cart: cart, order: cart ? nil : owner)
end

#display_code ⇒ Object



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

def display_code
  code.upcase
end

#holder ⇒ Spree::Cart, ...

Returns the record currently holding this code.

Returns:



54
55
56
# File 'app/models/spree/coupon_code.rb', line 54

def holder
  cart || order
end

#order=(record) ⇒ Object

Bridge for legacy callers assigning current_order (now a Spree::Cart) to the order association — routes carts to the cart FK instead.



23
24
25
26
27
28
29
30
# File 'app/models/spree/coupon_code.rb', line 23

def order=(record)
  if record.is_a?(Spree::Cart)
    self.cart = record
    super(nil)
  else
    super
  end
end

#remove_from_order ⇒ Object



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

def remove_from_order
  update(order: nil, cart: nil, state: 'unused')
end

#to_csv(_store = nil) ⇒ Object



66
67
68
# File 'app/models/spree/coupon_code.rb', line 66

def to_csv(_store = nil)
  Spree::CSV::CouponCodePresenter.new(self).call
end