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.11 app/models/api_token.rb
iugusdk-1.0.10 app/models/api_token.rb
iugusdk-1.0.9 app/models/api_token.rb
iugusdk-1.0.8 app/models/api_token.rb
iugusdk-1.0.7 app/models/api_token.rb
iugusdk-1.0.6 app/models/api_token.rb
iugusdk-1.0.5 app/models/api_token.rb
iugusdk-1.0.4 app/models/api_token.rb
iugusdk-1.0.3 app/models/api_token.rb
iugusdk-1.0.2 app/models/api_token.rb
iugusdk-1.0.1 app/models/api_token.rb
iugusdk-1.0.0.alpha.32 app/models/api_token.rb
iugusdk-1.0.0.alpha.31 app/models/api_token.rb
iugusdk-1.0.0.alpha.30 app/models/api_token.rb
iugusdk-1.0.0.alpha.29 app/models/api_token.rb
iugusdk-1.0.0.alpha.28 app/models/api_token.rb
iugusdk-1.0.0.alpha.27 app/models/api_token.rb
iugusdk-1.0.0.alpha.26 app/models/api_token.rb
iugusdk-1.0.0.alpha.25 app/models/api_token.rb
iugusdk-1.0.0.alpha.24 app/models/api_token.rb