Sha256: e6303ed01bd21efc5cdc7c769dcb32cf66ed562a5a91fe395b2bf4b1e2b44dc1

Contents?: true

Size: 820 Bytes

Versions: 2

Compression:

Stored size: 820 Bytes

Contents

class ApiToken < ActiveRecord::Base
  belongs_to :tokenable, :polymorphic => true

  before_create :set_first_token

  validates :token, :uniqueness => true
  validates :description, :uniqueness => { :scope => [:tokenable_id, :tokenable_type] }
  validates :description, :tokenable, :api_type, :presence => true
  validate :valid_account_api_type, :if => Proc.new { tokenable_type == "Account" }

  def refresh
    self.token = generate_api_token
    save
  end


  private

  def set_first_token
    self.token = generate_api_token
  end 

  def generate_api_token
    Digest::MD5.hexdigest("#{SecureRandom.hex(10)}-#{DateTime.now.to_s}")
  end 

  def valid_account_api_type
    errors.add(:api_type, I18n.t('errors.messages.not_supported_api_type')) unless IuguSDK::account_api_tokens.include? api_type
  end

  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iugusdk-1.0.0.alpha.5 app/models/api_token.rb
iugusdk-1.0.0.alpha.4 app/models/api_token.rb