Class: Spree::VirtualGiftCard

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
app/models/spree/virtual_gift_card.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.active_by_redemption_code(redemption_code) ⇒ Object



88
89
90
# File 'app/models/spree/virtual_gift_card.rb', line 88

def self.active_by_redemption_code(redemption_code)
  Spree::VirtualGiftCard.unredeemed.by_redemption_code(redemption_code).first
end

Instance Method Details

#detailsObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/spree/virtual_gift_card.rb', line 47

def details
  {
    amount: formatted_amount,
    redemption_code: formatted_redemption_code,
    recipient_email: recipient_email,
    recipient_name: recipient_name,
    purchaser_name: purchaser_name,
    gift_message: gift_message,
    send_email_at: send_email_at,
    formatted_send_email_at: formatted_send_email_at
  }
end

#formatted_amountObject



64
65
66
# File 'app/models/spree/virtual_gift_card.rb', line 64

def formatted_amount
  number_to_currency(amount, precision: 0)
end

#formatted_created_atObject



76
77
78
# File 'app/models/spree/virtual_gift_card.rb', line 76

def formatted_created_at
  created_at.localtime.strftime("%F %I:%M%p")
end

#formatted_redeemed_atObject



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

def formatted_redeemed_at
  redeemed_at.localtime.strftime("%F %I:%M%p") if redeemed_at
end

#formatted_redemption_codeObject



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

def formatted_redemption_code
  redemption_code.present? ? redemption_code.scan(/.{4}/).join('-') : ""
end

#formatted_send_email_atObject



68
69
70
# File 'app/models/spree/virtual_gift_card.rb', line 68

def formatted_send_email_at
  send_email_at.strftime("%-m/%-d/%y") if send_email_at
end

#formatted_sent_atObject



72
73
74
# File 'app/models/spree/virtual_gift_card.rb', line 72

def formatted_sent_at
  sent_at.strftime("%-m/%-d/%y") if sent_at
end

#make_redeemable!(purchaser:) ⇒ Object



39
40
41
# File 'app/models/spree/virtual_gift_card.rb', line 39

def make_redeemable!(purchaser:)
  update_attributes!(redeemable: true, purchaser: purchaser, redemption_code: (self.redemption_code || generate_unique_redemption_code))
end

#memoObject



43
44
45
# File 'app/models/spree/virtual_gift_card.rb', line 43

def memo
  "Gift Card ##{self.redemption_code}"
end

#redeem(redeemer) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/spree/virtual_gift_card.rb', line 25

def redeem(redeemer)
  return false if redeemed? || !redeemable?
  create_store_credit!({
    amount: amount,
    currency: currency,
    memo: memo,
    user: redeemer,
    created_by: redeemer,
    action_originator: self,
    category: store_credit_category,
  })
  self.update_attributes( redeemed_at: Time.now, redeemer: redeemer )
end

#redeemed?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/models/spree/virtual_gift_card.rb', line 21

def redeemed?
  redeemed_at.present?
end

#send_emailObject



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

def send_email
  Spree::GiftCardMailer.gift_card_email(self).deliver_later
  update_attributes!(sent_at: DateTime.now)
end

#store_credit_categoryObject



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

def store_credit_category
  Spree::StoreCreditCategory.where(name: Spree::StoreCreditCategory::GIFT_CARD_CATEGORY_NAME).first
end