From 734bfe1dc102349e20c36f40d6482dedd6ab6794 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 30 Nov 2018 13:13:54 +0500 Subject: [PATCH] Add model PassportConfirmation --- app/models/passport_confirmation.rb | 8 ++++++++ ...20181130075817_create_passport_confirmations.rb | 13 +++++++++++++ db/schema.rb | 14 +++++++++++++- factories/passport_confirmations.rb | 8 ++++++++ spec/models/passport_confirmation_spec.rb | 14 ++++++++++++++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 app/models/passport_confirmation.rb create mode 100644 db/migrate/20181130075817_create_passport_confirmations.rb create mode 100644 factories/passport_confirmations.rb create mode 100644 spec/models/passport_confirmation_spec.rb diff --git a/app/models/passport_confirmation.rb b/app/models/passport_confirmation.rb new file mode 100644 index 0000000..fef5af2 --- /dev/null +++ b/app/models/passport_confirmation.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class PassportConfirmation < ApplicationRecord + belongs_to :passport + belongs_to :user + + validates :user_id, uniqueness: { scope: :passport_id } +end diff --git a/db/migrate/20181130075817_create_passport_confirmations.rb b/db/migrate/20181130075817_create_passport_confirmations.rb new file mode 100644 index 0000000..4bd0ea8 --- /dev/null +++ b/db/migrate/20181130075817_create_passport_confirmations.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class CreatePassportConfirmations < ActiveRecord::Migration[5.2] + def change + create_table :passport_confirmations do |t| + t.timestamps null: false + t.references :passport, null: false, foreign_key: true + t.references :user, null: false, foreign_key: true + + t.index %i[passport_id user_id], unique: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ec42544..a0f29fd 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_11_30_040846) do +ActiveRecord::Schema.define(version: 2018_11_30_075817) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -51,6 +51,16 @@ ActiveRecord::Schema.define(version: 2018_11_30_040846) do t.text "comment" end + create_table "passport_confirmations", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.bigint "passport_id", null: false + t.bigint "user_id", null: false + t.index ["passport_id", "user_id"], name: "index_passport_confirmations_on_passport_id_and_user_id", unique: true + t.index ["passport_id"], name: "index_passport_confirmations_on_passport_id" + t.index ["user_id"], name: "index_passport_confirmations_on_user_id" + end + create_table "passports", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -120,6 +130,8 @@ ActiveRecord::Schema.define(version: 2018_11_30_040846) do t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true end + add_foreign_key "passport_confirmations", "passports" + add_foreign_key "passport_confirmations", "users" add_foreign_key "user_roles", "roles" add_foreign_key "user_roles", "users" end diff --git a/factories/passport_confirmations.rb b/factories/passport_confirmations.rb new file mode 100644 index 0000000..6b7012a --- /dev/null +++ b/factories/passport_confirmations.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :passport_confirmation do + passport + user + end +end diff --git a/spec/models/passport_confirmation_spec.rb b/spec/models/passport_confirmation_spec.rb new file mode 100644 index 0000000..b510bf1 --- /dev/null +++ b/spec/models/passport_confirmation_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe PassportConfirmation do + subject { create :passport_confirmation } + + it { is_expected.to belong_to(:passport).required } + it { is_expected.to belong_to(:user).required } + + it do + is_expected.to validate_uniqueness_of(:user_id).scoped_to(:passport_id) + end +end