app/models/redemption_code.rb
# == Schema Information
#
# Table name: redemption_codes
#
# id :integer not null, primary key
# order_item_id :integer not null
# token :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_redemption_codes_on_order_item_id (order_item_id)
#
require 'securerandom'
require 'active_model/naming'
require 'active_model/conversion'
class RedemptionCode < ActiveRecord::Base
after_initialize :generate_token
def to_s
token
end
private
def generate_token
return token unless new_record?
self.token ||= SecureRandom.random_number(36**8).to_s(36)
end
end