From 655c9fd36ff283a9c39372e8c0297fc52245a7ed Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 7 Dec 2018 05:41:07 +0500 Subject: [PATCH] Add attribute TelegramBot#username --- app/models/telegram_bot.rb | 7 +++++++ app/views/telegram_bots/index.html.erb | 5 +++++ config/locales/activerecord/en.yml | 1 + config/locales/activerecord/ru.yml | 1 + ...1207002553_add_username_to_telegram_bots.rb | 7 +++++++ db/schema.rb | 3 ++- spec/models/telegram_bot_spec.rb | 18 ++++++++++++++++++ 7 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20181207002553_add_username_to_telegram_bots.rb diff --git a/app/models/telegram_bot.rb b/app/models/telegram_bot.rb index da6654a..40057c5 100644 --- a/app/models/telegram_bot.rb +++ b/app/models/telegram_bot.rb @@ -1,6 +1,13 @@ # frozen_string_literal: true class TelegramBot < ApplicationRecord + USERNAME_RE = /\A[a-z_][a-z0-9_]*\z/i.freeze + validates :secret, presence: true validates :api_token, presence: true + + validates :username, + allow_nil: true, + presence: true, + format: USERNAME_RE end diff --git a/app/views/telegram_bots/index.html.erb b/app/views/telegram_bots/index.html.erb index e4c4088..bb207e9 100644 --- a/app/views/telegram_bots/index.html.erb +++ b/app/views/telegram_bots/index.html.erb @@ -11,6 +11,9 @@ <%= TelegramBot.human_attribute_name :api_token %> + + <%= TelegramBot.human_attribute_name :username %> + @@ -21,6 +24,8 @@ <%= telegram_bot.id %> <%= telegram_bot.secret %> <%= telegram_bot.api_token %> + <%= truncate telegram_bot.username, length: 20 %> + <% end %> diff --git a/config/locales/activerecord/en.yml b/config/locales/activerecord/en.yml index 1f07bca..cf57dfe 100644 --- a/config/locales/activerecord/en.yml +++ b/config/locales/activerecord/en.yml @@ -72,6 +72,7 @@ en: id: ID secret: Secret api_token: API token + username: Username user: id: ID confirmation_sent_at: Confirmation sent at diff --git a/config/locales/activerecord/ru.yml b/config/locales/activerecord/ru.yml index e466fbe..63d40dc 100644 --- a/config/locales/activerecord/ru.yml +++ b/config/locales/activerecord/ru.yml @@ -72,6 +72,7 @@ ru: id: ID secret: Секрет api_token: Токен API + username: Имя пользователя user: id: ID confirmation_sent_at: Дата отправки подтверждения diff --git a/db/migrate/20181207002553_add_username_to_telegram_bots.rb b/db/migrate/20181207002553_add_username_to_telegram_bots.rb new file mode 100644 index 0000000..db0637d --- /dev/null +++ b/db/migrate/20181207002553_add_username_to_telegram_bots.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddUsernameToTelegramBots < ActiveRecord::Migration[5.2] + def change + add_column :telegram_bots, :username, :string, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index f44d723..06483b7 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: 2018_12_06_224446) do +ActiveRecord::Schema.define(version: 2018_12_07_002553) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -133,6 +133,7 @@ ActiveRecord::Schema.define(version: 2018_12_06_224446) do t.datetime "updated_at", null: false t.string "secret", null: false t.string "api_token", null: false + t.string "username" end create_table "user_omniauths", force: :cascade do |t| diff --git a/spec/models/telegram_bot_spec.rb b/spec/models/telegram_bot_spec.rb index 7ffd56a..90917f7 100644 --- a/spec/models/telegram_bot_spec.rb +++ b/spec/models/telegram_bot_spec.rb @@ -7,4 +7,22 @@ RSpec.describe TelegramBot do it { is_expected.to validate_presence_of :secret } it { is_expected.to validate_presence_of :api_token } + + describe '#username' do + def allow_value(*) + super.for :username + end + + it { is_expected.to allow_value nil } + + it { is_expected.not_to allow_value '' } + it { is_expected.not_to allow_value ' ' } + it { is_expected.not_to allow_value ' ' * rand(2..10) } + + it { is_expected.to allow_value Faker::Internet.username nil, %w[_] } + it { is_expected.to allow_value 'foo123_456_BAR' } + + it { is_expected.not_to allow_value Faker::Internet.email } + it { is_expected.not_to allow_value Faker::PhoneNumber.phone_number } + end end