Sha256: ca3c8f4606474f32e6dc23badfb70d7a7ee9e588d0117d256beb8bad0af8db87

Contents?: true

Size: 903 Bytes

Versions: 35

Compression:

Stored size: 903 Bytes

Contents

class ApiToken < ActiveRecord::Base
  include ActiveUUID::UUID

  attr_accessible :tokenable, :api_type, :description

  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

35 entries across 35 versions & 1 rubygems

Version Path
iugusdk-1.0.0.alpha.23 app/models/api_token.rb
iugusdk-1.0.0.alpha.22 app/models/api_token.rb
iugusdk-1.0.0.alpha.21 app/models/api_token.rb
iugusdk-1.0.0.alpha.20 app/models/api_token.rb
iugusdk-1.0.0.alpha.19 app/models/api_token.rb
iugusdk-1.0.0.alpha.18 app/models/api_token.rb
iugusdk-1.0.0.alpha.17 app/models/api_token.rb
iugusdk-1.0.0.alpha.16 app/models/api_token.rb
iugusdk-1.0.0.alpha.14 app/models/api_token.rb
iugusdk-1.0.0.alpha.13 app/models/api_token.rb
iugusdk-1.0.0.alpha.12 app/models/api_token.rb
iugusdk-1.0.0.alpha.11 app/models/api_token.rb
iugusdk-1.0.0.alpha.10 app/models/api_token.rb
iugusdk-1.0.0.alpha.9 app/models/api_token.rb
iugusdk-1.0.0.alpha.7 app/models/api_token.rb