diff --git a/app/models/account.rb b/app/models/account.rb index 2c09779..f4a2682 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -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? diff --git a/app/models/membership_pool.rb b/app/models/membership_pool.rb index 4f373c7..5d604f3 100644 --- a/app/models/membership_pool.rb +++ b/app/models/membership_pool.rb @@ -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 diff --git a/app/models/membership_pool_account.rb b/app/models/membership_pool_account.rb new file mode 100644 index 0000000..42f3945 --- /dev/null +++ b/app/models/membership_pool_account.rb @@ -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 diff --git a/db/migrate/20181206205529_create_membership_pool_accounts.rb b/db/migrate/20181206205529_create_membership_pool_accounts.rb new file mode 100644 index 0000000..b6be12e --- /dev/null +++ b/db/migrate/20181206205529_create_membership_pool_accounts.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 2349fa9..7cf0621 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_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" diff --git a/factories/membership_pool_accounts.rb b/factories/membership_pool_accounts.rb new file mode 100644 index 0000000..010e5f1 --- /dev/null +++ b/factories/membership_pool_accounts.rb @@ -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 diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 9741bf1..3467aec 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -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' diff --git a/spec/models/membership_pool_account_spec.rb b/spec/models/membership_pool_account_spec.rb new file mode 100644 index 0000000..12c6bfb --- /dev/null +++ b/spec/models/membership_pool_account_spec.rb @@ -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 diff --git a/spec/models/membership_pool_spec.rb b/spec/models/membership_pool_spec.rb index 271b5a4..f91ff72 100644 --- a/spec/models/membership_pool_spec.rb +++ b/spec/models/membership_pool_spec.rb @@ -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