Class: Spree::GiftCard

Inherits:
Object
  • Object
show all
Extended by:
DisplayMoney
Includes:
Metafields, Security::GiftCards, SingleStoreResource
Defined in:
app/models/spree/gift_card.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Class Method Details

.json_api_columnsObject



92
93
94
# File 'app/models/spree/gift_card.rb', line 92

def self.json_api_columns
  %w[code amount expires_at]
end

Instance Method Details

#active?Boolean

Checks if the gift card is active, i.e. not expired and not redeemed

Returns:



122
123
124
# File 'app/models/spree/gift_card.rb', line 122

def active?
  super && !expired?
end

#amount=(amount) ⇒ Object

Sets the amount

Parameters:

  • amount (String)


80
81
82
# File 'app/models/spree/gift_card.rb', line 80

def amount=(amount)
  self[:amount] = Spree::LocalizedNumber.parse(amount)
end

#amount_remainingDecimal

Calculates the remaining amount

Returns:

  • (Decimal)


86
87
88
# File 'app/models/spree/gift_card.rb', line 86

def amount_remaining
  amount - amount_used - amount_authorized
end

#can_be_deleted?Boolean

Checks if the gift card can be deleted

Returns:



104
105
106
# File 'app/models/spree/gift_card.rb', line 104

def can_be_deleted?
  !redeemed? && !partially_redeemed?
end

#display_codeString

Displays the code in uppercase, eg. ABC1234

Returns:

  • (String)


110
111
112
# File 'app/models/spree/gift_card.rb', line 110

def display_code
  code.upcase
end

#display_stateString

Displays state as expired if the gift card is expired, otherwise displays the state

Returns:

  • (String)


128
129
130
131
132
133
134
# File 'app/models/spree/gift_card.rb', line 128

def display_state
  if expired?
    :expired
  else
    state
  end.to_s
end

#editable?Boolean

Checks if the gift card is editable

Returns:



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

def editable?
  active?
end

#expired?Boolean

Checks if the gift card is expired

Returns:



116
117
118
# File 'app/models/spree/gift_card.rb', line 116

def expired?
  !redeemed? && expires_at.present? && expires_at <= Date.current
end

#to_csv(_store = nil) ⇒ Object



136
137
138
# File 'app/models/spree/gift_card.rb', line 136

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