1
0
Fork 0

Unique MembershipApp#account

This commit is contained in:
Alex Kotov 2018-12-13 10:36:00 +05:00
parent 22addd9770
commit 2af20a7de2
No known key found for this signature in database
GPG Key ID: 4E831250F47DE154
8 changed files with 40 additions and 14 deletions

View File

@ -10,9 +10,9 @@ class Account < ApplicationRecord
has_many :account_telegram_contacts,
dependent: :restrict_with_exception
has_many :own_membership_apps,
class_name: 'MembershipApp',
dependent: :restrict_with_exception
has_one :own_membership_app,
class_name: 'MembershipApp',
dependent: :restrict_with_exception
has_many :passport_confirmations, dependent: :restrict_with_exception

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
class MembershipApp < ApplicationRecord
belongs_to :account, inverse_of: :own_membership_apps
belongs_to :account, inverse_of: :own_membership_app
belongs_to :country_state, optional: true
has_one :regional_office, through: :country_state
@ -13,6 +13,8 @@ class MembershipApp < ApplicationRecord
validates :date_of_birth, presence: true
validates :phone_number, presence: true
validates :account_id, uniqueness: true
before_validation do
email&.strip!

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
class UniqAccountOnMembershipApps < ActiveRecord::Migration[5.2]
def change
remove_index :membership_apps, :account_id
add_index :membership_apps, :account_id, unique: true
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_12_000903) do
ActiveRecord::Schema.define(version: 2018_12_13_053336) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -85,7 +85,7 @@ ActiveRecord::Schema.define(version: 2018_12_12_000903) do
t.text "comment"
t.bigint "country_state_id"
t.bigint "account_id", null: false
t.index ["account_id"], name: "index_membership_apps_on_account_id"
t.index ["account_id"], name: "index_membership_apps_on_account_id", unique: true
t.index ["country_state_id"], name: "index_membership_apps_on_country_state_id"
end

View File

@ -21,7 +21,7 @@ RSpec.describe Account do
it do
is_expected.to \
have_many(:own_membership_apps)
have_one(:own_membership_app)
.class_name('MembershipApp')
.dependent(:restrict_with_exception)
end

View File

@ -5,7 +5,7 @@ require 'rails_helper'
RSpec.describe MembershipApp do
subject { create :membership_app }
it { is_expected.to belong_to(:account).inverse_of(:own_membership_apps) }
it { is_expected.to belong_to(:account).inverse_of(:own_membership_app) }
it { is_expected.to belong_to(:country_state).optional }
it { is_expected.to have_one(:regional_office).through(:country_state) }
@ -23,6 +23,8 @@ RSpec.describe MembershipApp do
it { is_expected.not_to validate_presence_of :organization_membership }
it { is_expected.not_to validate_presence_of :comment }
it { is_expected.to validate_uniqueness_of :account_id }
describe '#email' do
def allow_value(*)
super.for :email

View File

@ -9,16 +9,14 @@ RSpec.describe MembershipAppPolicy do
described_class::Scope.new(account, MembershipApp.all).resolve
end
let(:record) { create :membership_app, account: owner }
let!(:owner_record) { create :membership_app, account: owner }
let!(:record) { create :membership_app, account: owner }
let!(:other_record) { create :membership_app }
let(:owner) { create %i[guest_account usual_account].sample }
let(:account) { owner }
context 'when owner is authenticated' do
let(:account) { owner }
it { is_expected.to permit_action :show }
it { is_expected.to permit_new_and_create_actions }

View File

@ -9,10 +9,23 @@ RSpec.describe Staff::MembershipAppPolicy do
described_class::Scope.new(account, MembershipApp.all).resolve
end
let(:record) { create :membership_app }
let!(:record) { create :membership_app, account: owner }
let!(:other_record) { create :membership_app }
before { create_list :membership_app, 3 }
let(:owner) { create %i[guest_account usual_account].sample }
context 'when owner is authenticated' do
let(:account) { owner }
it { is_expected.to forbid_actions %i[index show destroy] }
it { is_expected.to forbid_new_and_create_actions }
it { is_expected.to forbid_edit_and_update_actions }
specify { expect(resolved_scope).to be_empty }
end
for_account_types nil, :guest, :usual do
it { is_expected.to forbid_actions %i[index show destroy] }
it { is_expected.to forbid_new_and_create_actions }
@ -29,5 +42,8 @@ RSpec.describe Staff::MembershipAppPolicy do
it { is_expected.to forbid_action :destroy }
specify { expect(resolved_scope).to eq MembershipApp.all }
specify { expect(resolved_scope).to include record }
specify { expect(resolved_scope).to include other_record }
end
end