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



96
97
98
# File 'app/models/spree/gift_card.rb', line 96

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:



126
127
128
# File 'app/models/spree/gift_card.rb', line 126

def active?
  super && !expired?
end

#amount=(amount) ⇒ Object

Sets the amount

Parameters:

  • amount (String)


84
85
86
# File 'app/models/spree/gift_card.rb', line 84

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

#amount_remainingDecimal

Calculates the remaining amount

Returns:

  • (Decimal)


90
91
92
# File 'app/models/spree/gift_card.rb', line 90

def amount_remaining
  amount - amount_used - amount_authorized
end

#can_be_deleted?Boolean

Checks if the gift card can be deleted

Returns:



108
109
110
# File 'app/models/spree/gift_card.rb', line 108

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

#display_codeString

Displays the code in uppercase, eg. ABC1234

Returns:

  • (String)


114
115
116
# File 'app/models/spree/gift_card.rb', line 114

def display_code
  code.upcase
end

#display_stateString

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

Returns:

  • (String)


132
133
134
135
136
137
138
# File 'app/models/spree/gift_card.rb', line 132

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

#editable?Boolean

Checks if the gift card is editable

Returns:



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

def editable?
  active?
end

#expired?Boolean

Checks if the gift card is expired

Returns:



120
121
122
# File 'app/models/spree/gift_card.rb', line 120

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

#to_csv(_store = nil) ⇒ Object



140
141
142
# File 'app/models/spree/gift_card.rb', line 140

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