1
0
Fork 0

Add model MembershipPoolApp

This commit is contained in:
Alex Kotov 2018-12-07 00:54:24 +05:00
parent 381ea0d687
commit 6397ae0b17
No known key found for this signature in database
GPG Key ID: 4E831250F47DE154
9 changed files with 105 additions and 1 deletions

View File

@ -4,6 +4,10 @@ class MembershipApp < ApplicationRecord
belongs_to :account
belongs_to :country_state, optional: true
has_many :membership_pool_apps, dependent: :restrict_with_exception
has_many :membership_pools, through: :membership_pool_apps
validates :email, presence: true, format: Devise.email_regexp
validates :first_name, presence: true

View File

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

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
class MembershipPoolApp < ApplicationRecord
belongs_to :membership_pool
belongs_to :membership_app
validates :membership_pool_id, uniqueness: { scope: :membership_app_id }
end

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
class CreateMembershipPoolApps < ActiveRecord::Migration[5.2]
def change
create_table :membership_pool_apps do |t|
t.timestamps null: false
t.references :membership_pool, null: false, foreign_key: true
t.references :membership_app, null: false, foreign_key: true
t.index %i[membership_app_id membership_pool_id],
name: 'index_membership_pool_apps_on_app_and_pool',
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_193802) do
ActiveRecord::Schema.define(version: 2018_12_06_194527) 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_193802) do
t.index ["country_state_id"], name: "index_membership_apps_on_country_state_id"
end
create_table "membership_pool_apps", 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 "membership_app_id", null: false
t.index ["membership_app_id", "membership_pool_id"], name: "index_membership_pool_apps_on_app_and_pool", unique: true
t.index ["membership_app_id"], name: "index_membership_pool_apps_on_membership_app_id"
t.index ["membership_pool_id"], name: "index_membership_pool_apps_on_membership_pool_id"
end
create_table "membership_pools", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
@ -177,6 +187,8 @@ ActiveRecord::Schema.define(version: 2018_12_06_193802) 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_apps", "membership_apps"
add_foreign_key "membership_pool_apps", "membership_pools"
add_foreign_key "passport_confirmations", "accounts"
add_foreign_key "passport_confirmations", "passports"
add_foreign_key "passport_maps", "passports"

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
FactoryBot.define do
factory :membership_pool_app do
association :membership_pool
association :membership_app
end
end

View File

@ -8,6 +8,18 @@ RSpec.describe MembershipApp do
it { is_expected.to belong_to :account }
it { is_expected.to belong_to(:country_state).optional }
it do
is_expected.to \
have_many(:membership_pool_apps)
.dependent(:restrict_with_exception)
end
it do
is_expected.to \
have_many(:membership_pools)
.through(:membership_pool_apps)
end
it { is_expected.to validate_presence_of(:account).with_message(:required) }
it { is_expected.not_to validate_presence_of :country_state }
it { is_expected.to validate_presence_of :first_name }

View File

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

View File

@ -5,5 +5,17 @@ require 'rails_helper'
RSpec.describe MembershipPool do
subject { create :membership_pool }
it do
is_expected.to \
have_many(:membership_pool_apps)
.dependent(:restrict_with_exception)
end
it do
is_expected.to \
have_many(:membership_apps)
.through(:membership_pool_apps)
end
it { is_expected.to validate_presence_of :name }
end