class AccessToken < ActiveRecord::Base
  belongs_to :repository
  attr_accessible :expiration, :token

  scope :expired, ->() { where('expiration <= ?', Time.now) }
  scope :unexpired, ->() { where('expiration > ?', Time.now) }

  def self.destroy_expired
    expired.find_each(&:destroy)
  end

  def self.create_for(ontology_version)
    a = insecure_build_for(ontology_version) until !a.nil? && a.valid?
    a.save!
    a
  end

  def self.fresh_expiration_date
    Settings.access_token.expiration_minutes.minutes.from_now
  end

  def self.insecure_build_for(ontology_version)
    repository = ontology_version.repository
    id = [
      repository.to_param, ontology_version.path,
      Time.now.strftime('%Y-%m-%d-%H-%M-%S-%6N')
    ].join('|')
    AccessToken.new(
      {repository: repository,
       expiration: fresh_expiration_date,
       token: Digest::SHA2.hexdigest(id)},
      {without_protection: true})
  end

  def to_s
    token
  end

  def expired?
    expiration <= Time.now
  end
end

class AccessToken < ActiveRecord::Base
  belongs_to :repository
  attr_accessible :expiration, :token

  scope :expired, ->() { where('expiration <= ?', Time.now) }
  scope :unexpired, ->() { where('expiration > ?', Time.now) }

  def self.destroy_expired
    expired.find_each(&:destroy)
  end

  def self.create_for(ontology_version)
    a = insecure_build_for(ontology_version) until !a.nil? && a.valid?
    a.save!
    a
  end

  def self.fresh_expiration_date
    Settings.access_token.expiration_minutes.minutes.from_now
  end

  def self.insecure_build_for(ontology_version)
    repository = ontology_version.repository
    AccessToken.new(
      {repository: repository,
       expiration: fresh_expiration_date,
       token: generate_token_string(ontology_version, repository)},
      {without_protection: true})
  end

  def self.generate_token_string(ontology_version, repository)
    id = [
      repository.to_param, ontology_version.path,
      Time.now.strftime('%Y-%m-%d-%H-%M-%S-%6N')
    ].join('|')
    Digest::SHA2.hexdigest(id)
  end

  def to_s
    token
  end

  def expired?
    expiration <= Time.now
  end
end

Put hash generation to a separate method.
Put hash generation to a separate method.
