From 2af20a7de2dfbeb5a331c19fb079575847c1a9d1 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Thu, 13 Dec 2018 10:36:00 +0500 Subject: [PATCH] Unique MembershipApp#account --- app/models/account.rb | 6 +++--- app/models/membership_app.rb | 4 +++- ...13053336_uniq_account_on_membership_apps.rb | 8 ++++++++ db/schema.rb | 4 ++-- spec/models/account_spec.rb | 2 +- spec/models/membership_app_spec.rb | 4 +++- spec/policies/membership_app_policy_spec.rb | 8 +++----- .../staff/membership_app_policy_spec.rb | 18 +++++++++++++++++- 8 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20181213053336_uniq_account_on_membership_apps.rb diff --git a/app/models/account.rb b/app/models/account.rb index 2c40688..89670c2 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -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 diff --git a/app/models/membership_app.rb b/app/models/membership_app.rb index 6207e73..e4dac85 100644 --- a/app/models/membership_app.rb +++ b/app/models/membership_app.rb @@ -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! diff --git a/db/migrate/20181213053336_uniq_account_on_membership_apps.rb b/db/migrate/20181213053336_uniq_account_on_membership_apps.rb new file mode 100644 index 0000000..577654f --- /dev/null +++ b/db/migrate/20181213053336_uniq_account_on_membership_apps.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index eaad9ff..cc20ad7 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_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 diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index a092065..5ff7b91 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -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 diff --git a/spec/models/membership_app_spec.rb b/spec/models/membership_app_spec.rb index 96689ef..574fcea 100644 --- a/spec/models/membership_app_spec.rb +++ b/spec/models/membership_app_spec.rb @@ -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 diff --git a/spec/policies/membership_app_policy_spec.rb b/spec/policies/membership_app_policy_spec.rb index aaadd1b..b8f3af8 100644 --- a/spec/policies/membership_app_policy_spec.rb +++ b/spec/policies/membership_app_policy_spec.rb @@ -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 } diff --git a/spec/policies/staff/membership_app_policy_spec.rb b/spec/policies/staff/membership_app_policy_spec.rb index ca47ffb..def30bf 100644 --- a/spec/policies/staff/membership_app_policy_spec.rb +++ b/spec/policies/staff/membership_app_policy_spec.rb @@ -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