1
0
Fork 0

Add model TelegramChat

This commit is contained in:
Alex Kotov 2018-12-07 08:11:18 +05:00
parent e1856eef0b
commit 8bfad75513
No known key found for this signature in database
GPG Key ID: 4E831250F47DE154
5 changed files with 74 additions and 1 deletions

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
class TelegramChat < ApplicationRecord
CHAT_TYPES = %w[private group supergroup channel].freeze
validates :remote_id, presence: true, uniqueness: true
validates :chat_type, presence: true, inclusion: { in: CHAT_TYPES }
end

View File

@ -0,0 +1,17 @@
# frozen_string_literal: true
class CreateTelegramChats < ActiveRecord::Migration[5.2]
def change
create_table :telegram_chats do |t|
t.timestamps null: false
t.integer :remote_id, null: false, index: { unique: true }
t.string :chat_type, null: false
t.string :title
t.string :username
t.string :first_name
t.string :last_name
end
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_12_07_002553) do
ActiveRecord::Schema.define(version: 2018_12_07_030332) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -136,6 +136,18 @@ ActiveRecord::Schema.define(version: 2018_12_07_002553) do
t.string "username"
end
create_table "telegram_chats", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "remote_id", null: false
t.string "chat_type", null: false
t.string "title"
t.string "username"
t.string "first_name"
t.string "last_name"
t.index ["remote_id"], name: "index_telegram_chats_on_remote_id", unique: true
end
create_table "user_omniauths", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
FactoryBot.define do
factory :telegram_chat do
remote_id { rand(-100_000_000..100_000_000) }
chat_type { TelegramChat::CHAT_TYPES.sample }
title { Faker::Lorem.sentence }
username { Faker::Internet.username }
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
end
end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe TelegramChat do
subject { create :telegram_chat }
it { is_expected.to validate_presence_of :remote_id }
it { is_expected.to validate_presence_of :chat_type }
it { is_expected.not_to validate_presence_of :title }
it { is_expected.not_to validate_presence_of :username }
it { is_expected.not_to validate_presence_of :first_name }
it { is_expected.not_to validate_presence_of :last_name }
it { is_expected.to validate_uniqueness_of :remote_id }
it do
is_expected.to \
validate_inclusion_of(:chat_type)
.in_array(%w[private group supergroup channel])
end
end