From 815e64c3834a550210b744808dbb1daf4ef794a7 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 1 Feb 2019 05:47:44 +0500 Subject: [PATCH] Add attribute Account#username --- app/models/account.rb | 16 +++++++++++ ...20190201002444_add_username_to_accounts.rb | 7 +++++ db/schema.rb | 3 ++- spec/models/account_spec.rb | 27 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20190201002444_add_username_to_accounts.rb diff --git a/app/models/account.rb b/app/models/account.rb index 89670c2..fad1f02 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -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 diff --git a/db/migrate/20190201002444_add_username_to_accounts.rb b/db/migrate/20190201002444_add_username_to_accounts.rb new file mode 100644 index 0000000..0e919c1 --- /dev/null +++ b/db/migrate/20190201002444_add_username_to_accounts.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 7df92f7..811dc0c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 5ff7b91..ca6642a 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -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 }