hummingbird-me/kitsu-server

View on GitHub
app/models/pro_gift.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
92%
class ProGift < ApplicationRecord
  belongs_to :from, class_name: 'User', required: true
  belongs_to :to, class_name: 'User', required: true
  enum tier: {
    pro: 1,
    patron: 2
  }

  validates :message, length: { maximum: 500 }

  after_create :send_email
  after_create :extend_pro

  def send_email
    ProMailer.gift_email(self).deliver_later
  end

  def extend_pro
    ProRenewalService.new(to).renew_for(Time.now, 1.year.from_now)
  end

  def as_json(*args)
    {
      to: to.name,
      message: message,
      tier: tier
    }.as_json(*args)
  end
end