1
0
Fork 0

Add attribute TelegramBot#username

This commit is contained in:
Alex Kotov 2018-12-07 05:41:07 +05:00
parent c77bfd9864
commit 655c9fd36f
No known key found for this signature in database
GPG key ID: 4E831250F47DE154
7 changed files with 41 additions and 1 deletions

View file

@ -1,6 +1,13 @@
# frozen_string_literal: true # frozen_string_literal: true
class TelegramBot < ApplicationRecord class TelegramBot < ApplicationRecord
USERNAME_RE = /\A[a-z_][a-z0-9_]*\z/i.freeze
validates :secret, presence: true validates :secret, presence: true
validates :api_token, presence: true validates :api_token, presence: true
validates :username,
allow_nil: true,
presence: true,
format: USERNAME_RE
end end

View file

@ -11,6 +11,9 @@
<th scope="col"> <th scope="col">
<%= TelegramBot.human_attribute_name :api_token %> <%= TelegramBot.human_attribute_name :api_token %>
</th> </th>
<th scope="col">
<%= TelegramBot.human_attribute_name :username %>
</th>
<th scope="col"></th> <th scope="col"></th>
</tr> </tr>
</thead> </thead>
@ -21,6 +24,8 @@
<td scope="row"><%= telegram_bot.id %></td> <td scope="row"><%= telegram_bot.id %></td>
<td><%= telegram_bot.secret %></td> <td><%= telegram_bot.secret %></td>
<td><%= telegram_bot.api_token %></td> <td><%= telegram_bot.api_token %></td>
<td><%= truncate telegram_bot.username, length: 20 %></td>
<td></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>

View file

@ -72,6 +72,7 @@ en:
id: ID id: ID
secret: Secret secret: Secret
api_token: API token api_token: API token
username: Username
user: user:
id: ID id: ID
confirmation_sent_at: Confirmation sent at confirmation_sent_at: Confirmation sent at

View file

@ -72,6 +72,7 @@ ru:
id: ID id: ID
secret: Секрет secret: Секрет
api_token: Токен API api_token: Токен API
username: Имя пользователя
user: user:
id: ID id: ID
confirmation_sent_at: Дата отправки подтверждения confirmation_sent_at: Дата отправки подтверждения

View file

@ -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

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -133,6 +133,7 @@ ActiveRecord::Schema.define(version: 2018_12_06_224446) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "secret", null: false t.string "secret", null: false
t.string "api_token", null: false t.string "api_token", null: false
t.string "username"
end end
create_table "user_omniauths", force: :cascade do |t| create_table "user_omniauths", force: :cascade do |t|

View file

@ -7,4 +7,22 @@ RSpec.describe TelegramBot do
it { is_expected.to validate_presence_of :secret } it { is_expected.to validate_presence_of :secret }
it { is_expected.to validate_presence_of :api_token } 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 end