Add attribute Account#username
This commit is contained in:
parent
37b46cfa60
commit
815e64c383
4 changed files with 52 additions and 1 deletions
|
@ -1,6 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Account < ApplicationRecord
|
||||
USERNAME_RE = /\A[a-z][_a-z0-9]*[a-z0-9]\z/i.freeze
|
||||
|
||||
rolify role_join_table_name: :account_roles
|
||||
|
||||
belongs_to :person, optional: true
|
||||
|
@ -18,10 +20,18 @@ class Account < ApplicationRecord
|
|||
|
||||
scope :guests, -> { includes(:user).where(users: { id: nil }) }
|
||||
|
||||
after_initialize :generate_username
|
||||
|
||||
before_create do
|
||||
self.guest_token = SecureRandom.hex
|
||||
end
|
||||
|
||||
validates :username,
|
||||
presence: true,
|
||||
length: { in: 3..36 },
|
||||
format: USERNAME_RE,
|
||||
uniqueness: { case_sensitive: false }
|
||||
|
||||
validates :person_id, allow_nil: true, uniqueness: true
|
||||
|
||||
def guest?
|
||||
|
@ -37,4 +47,10 @@ class Account < ApplicationRecord
|
|||
def can_access_sidekiq_web_interface?
|
||||
is_superuser?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_username
|
||||
self.username = "noname_#{SecureRandom.hex(8)}" if username.nil?
|
||||
end
|
||||
end
|
||||
|
|
7
db/migrate/20190201002444_add_username_to_accounts.rb
Normal file
7
db/migrate/20190201002444_add_username_to_accounts.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddUsernameToAccounts < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
add_column :accounts, :username, :string, index: { unique: true }
|
||||
end
|
||||
end
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2019_01_31_025820) do
|
||||
ActiveRecord::Schema.define(version: 2019_02_01_002444) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
@ -39,6 +39,7 @@ ActiveRecord::Schema.define(version: 2019_01_31_025820) do
|
|||
t.datetime "updated_at", null: false
|
||||
t.string "guest_token", null: false
|
||||
t.bigint "person_id"
|
||||
t.string "username"
|
||||
t.index ["person_id"], name: "index_accounts_on_person_id", unique: true
|
||||
end
|
||||
|
||||
|
|
|
@ -39,6 +39,33 @@ RSpec.describe Account do
|
|||
pending '#guest?'
|
||||
pending '#can_access_sidekiq_web_interface?'
|
||||
|
||||
describe '#username' do
|
||||
def allow_value(*)
|
||||
super.for :username
|
||||
end
|
||||
|
||||
it { is_expected.to validate_presence_of :username }
|
||||
|
||||
it do
|
||||
is_expected.to validate_length_of(:username).is_at_least(3).is_at_most(36)
|
||||
end
|
||||
|
||||
it { is_expected.to validate_uniqueness_of(:username).case_insensitive }
|
||||
|
||||
it { is_expected.not_to allow_value nil }
|
||||
it { is_expected.not_to allow_value '' }
|
||||
it { is_expected.not_to allow_value ' ' * 3 }
|
||||
|
||||
it { is_expected.to allow_value Faker::Internet.username(3..36, %w[_]) }
|
||||
it { is_expected.to allow_value 'foo_bar' }
|
||||
it { is_expected.to allow_value 'foo123' }
|
||||
|
||||
it { is_expected.not_to allow_value Faker::Internet.email }
|
||||
it { is_expected.not_to allow_value '_foo' }
|
||||
it { is_expected.not_to allow_value 'bar_' }
|
||||
it { is_expected.not_to allow_value '1foo' }
|
||||
end
|
||||
|
||||
describe '#add_role' do
|
||||
context 'to guest account' do
|
||||
subject { create :guest_account }
|
||||
|
|
Reference in a new issue