app/models/client.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

# == Schema Information
#
# Table name: clients
#
#  id         :integer          not null, primary key
#  name       :string           not null
#  key        :string           not null
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Client < ApplicationRecord
  rolify

  has_many :issued_transactions, as: :issuer, class_name: "Transaction", dependent: :restrict_with_exception

  before_create :generate_key

  validates :name, presence: true, uniqueness: true

  private

  def generate_key
    self.key = SecureRandom.base64(16) unless self.key
  end
end