1
0
Fork 0

Add model MembershipPoolAccount

This commit is contained in:
Alex Kotov 2018-12-07 02:03:12 +05:00
parent cc037519b8
commit 56df5e5030
No known key found for this signature in database
GPG key ID: 4E831250F47DE154
9 changed files with 105 additions and 1 deletions

View file

@ -9,6 +9,10 @@ class Account < ApplicationRecord
has_many :passport_confirmations, dependent: :restrict_with_exception
has_many :membership_pool_accounts, dependent: :restrict_with_exception
has_many :membership_pools, through: :membership_pool_accounts
scope :guests, -> { includes(:user).where(users: { id: nil }) }
def guest?

View file

@ -5,5 +5,9 @@ class MembershipPool < ApplicationRecord
has_many :membership_apps, through: :membership_pool_apps
has_many :membership_pool_accounts, dependent: :restrict_with_exception
has_many :accounts, through: :membership_pool_accounts
validates :name, presence: true
end

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
class MembershipPoolAccount < ApplicationRecord
belongs_to :membership_pool
belongs_to :account
validates :account_id, uniqueness: { scope: :membership_pool_id }
end

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
class CreateMembershipPoolAccounts < ActiveRecord::Migration[5.2]
def change
create_table :membership_pool_accounts do |t|
t.timestamps null: false
t.references :membership_pool, null: false, foreign_key: true
t.references :account, null: false, foreign_key: true
t.index %i[membership_pool_id account_id],
name: 'index_membership_pool_accounts_on_pool_and_account',
unique: true
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_06_194527) do
ActiveRecord::Schema.define(version: 2018_12_06_205529) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -77,6 +77,16 @@ ActiveRecord::Schema.define(version: 2018_12_06_194527) do
t.index ["country_state_id"], name: "index_membership_apps_on_country_state_id"
end
create_table "membership_pool_accounts", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "membership_pool_id", null: false
t.bigint "account_id", null: false
t.index ["account_id"], name: "index_membership_pool_accounts_on_account_id"
t.index ["membership_pool_id", "account_id"], name: "index_membership_pool_accounts_on_pool_and_account", unique: true
t.index ["membership_pool_id"], name: "index_membership_pool_accounts_on_membership_pool_id"
end
create_table "membership_pool_apps", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
@ -187,6 +197,8 @@ ActiveRecord::Schema.define(version: 2018_12_06_194527) do
add_foreign_key "account_roles", "roles"
add_foreign_key "membership_apps", "accounts"
add_foreign_key "membership_apps", "country_states"
add_foreign_key "membership_pool_accounts", "accounts"
add_foreign_key "membership_pool_accounts", "membership_pools"
add_foreign_key "membership_pool_apps", "membership_apps"
add_foreign_key "membership_pool_apps", "membership_pools"
add_foreign_key "passport_confirmations", "accounts"

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
FactoryBot.define do
factory :membership_pool_account do
association :membership_pool
association :account, factory: :account_with_user
end
end

View file

@ -23,6 +23,18 @@ RSpec.describe Account do
.dependent(:restrict_with_exception)
end
it do
is_expected.to \
have_many(:membership_pool_accounts)
.dependent(:restrict_with_exception)
end
it do
is_expected.to \
have_many(:membership_pools)
.through(:membership_pool_accounts)
end
it { is_expected.not_to validate_presence_of :user }
pending '.guests'

View file

@ -0,0 +1,28 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe MembershipPoolAccount do
subject { create :membership_pool_account }
it { is_expected.to belong_to :membership_pool }
it { is_expected.to belong_to :account }
it do
is_expected.to \
validate_presence_of(:membership_pool)
.with_message(:required)
end
it do
is_expected.to \
validate_presence_of(:account)
.with_message(:required)
end
it do
is_expected.to \
validate_uniqueness_of(:account_id)
.scoped_to(:membership_pool_id)
end
end

View file

@ -17,5 +17,17 @@ RSpec.describe MembershipPool do
.through(:membership_pool_apps)
end
it do
is_expected.to \
have_many(:membership_pool_accounts)
.dependent(:restrict_with_exception)
end
it do
is_expected.to \
have_many(:accounts)
.through(:membership_pool_accounts)
end
it { is_expected.to validate_presence_of :name }
end