mirror of https://github.com/docusealco/docuseal
CP-11138 partnership refactor (#22)
* account group to partnership rename * this is mostly converting the name account_group => partnership * partnership user relationships are API request based * so we don't need to maintain relational information in two databases, this many to many relationship is now handled via API context * rubocop and test fixespull/544/head
parent
a1ed992ee4
commit
7d592d4761
@ -1,36 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: account_groups
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# name :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# external_account_group_id :integer not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_account_groups_on_external_account_group_id (external_account_group_id) UNIQUE
|
||||
#
|
||||
class AccountGroup < ApplicationRecord
|
||||
has_many :accounts, dependent: :nullify
|
||||
has_many :users, dependent: :nullify
|
||||
has_many :templates, dependent: :destroy
|
||||
has_many :template_folders, dependent: :destroy
|
||||
|
||||
validates :external_account_group_id, presence: true, uniqueness: true
|
||||
validates :name, presence: true
|
||||
|
||||
def self.find_or_create_by_external_id(external_id, attributes = {})
|
||||
find_by(external_account_group_id: external_id) ||
|
||||
create!(attributes.merge(external_account_group_id: external_id))
|
||||
end
|
||||
|
||||
def default_template_folder
|
||||
template_folders.find_by(name: TemplateFolder::DEFAULT_NAME) ||
|
||||
template_folders.create!(name: TemplateFolder::DEFAULT_NAME,
|
||||
author_id: users.minimum(:id))
|
||||
end
|
||||
end
|
||||
@ -1,19 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module AccountGroupValidation
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
validate :must_belong_to_account_or_account_group
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def must_belong_to_account_or_account_group
|
||||
if account.blank? && account_group.blank?
|
||||
errors.add(:base, 'Must belong to either an account or account group')
|
||||
elsif account.present? && account_group.present?
|
||||
errors.add(:base, 'Cannot belong to both account and account group')
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,19 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PartnershipValidation
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
validate :must_belong_to_account_or_partnership
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def must_belong_to_account_or_partnership
|
||||
if account.blank? && partnership.blank?
|
||||
errors.add(:base, 'Must belong to either an account or partnership')
|
||||
elsif account.present? && partnership.present?
|
||||
errors.add(:base, 'Cannot belong to both account and partnership')
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: partnerships
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# name :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# external_partnership_id :integer not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_partnerships_on_external_partnership_id (external_partnership_id) UNIQUE
|
||||
#
|
||||
class Partnership < ApplicationRecord
|
||||
has_many :templates, dependent: :destroy
|
||||
has_many :template_folders, dependent: :destroy
|
||||
|
||||
validates :external_partnership_id, presence: true, uniqueness: true
|
||||
validates :name, presence: true
|
||||
|
||||
def self.find_or_create_by_external_id(external_id, name, attributes = {})
|
||||
find_by(external_partnership_id: external_id) ||
|
||||
create!(attributes.merge(external_partnership_id: external_id, name: name))
|
||||
end
|
||||
|
||||
def default_template_folder(author)
|
||||
raise ArgumentError, 'Author is required for partnership template folders' unless author
|
||||
|
||||
template_folders.find_by(name: TemplateFolder::DEFAULT_NAME) ||
|
||||
template_folders.create!(name: TemplateFolder::DEFAULT_NAME,
|
||||
author: author)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,18 @@
|
||||
class RenameAccountGroupsToPartnerships < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
# Rename the table
|
||||
rename_table :account_groups, :partnerships
|
||||
|
||||
# Rename the foreign key columns in other tables
|
||||
rename_column :templates, :account_group_id, :partnership_id
|
||||
rename_column :template_folders, :account_group_id, :partnership_id
|
||||
rename_column :export_locations, :global_account_group_id, :global_partnership_id
|
||||
|
||||
# Remove partnership relationships since both users and accounts use API context now
|
||||
remove_column :users, :account_group_id, :bigint
|
||||
remove_column :accounts, :account_group_id, :bigint
|
||||
|
||||
# Rename the external ID column to match new naming
|
||||
rename_column :partnerships, :external_account_group_id, :external_partnership_id
|
||||
end
|
||||
end
|
||||
@ -1,8 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :account_group do
|
||||
external_account_group_id { Faker::Number.unique.number(digits: 8) }
|
||||
name { Faker::Company.name }
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :partnership do
|
||||
external_partnership_id { Faker::Number.unique.number(digits: 8) }
|
||||
name { Faker::Company.name }
|
||||
end
|
||||
end
|
||||
@ -1,56 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: account_groups
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# name :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# external_account_group_id :integer not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_account_groups_on_external_account_group_id (external_account_group_id) UNIQUE
|
||||
#
|
||||
describe AccountGroup do
|
||||
let(:account_group) { create(:account_group) }
|
||||
|
||||
describe 'associations' do
|
||||
it 'has many accounts' do
|
||||
expect(account_group).to respond_to(:accounts)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
it 'validates presence of external_account_group_id' do
|
||||
account_group = build(:account_group, external_account_group_id: nil)
|
||||
expect(account_group).not_to be_valid
|
||||
expect(account_group.errors[:external_account_group_id]).to include("can't be blank")
|
||||
end
|
||||
|
||||
it 'validates uniqueness of external_account_group_id' do
|
||||
create(:account_group, external_account_group_id: 123)
|
||||
duplicate = build(:account_group, external_account_group_id: 123)
|
||||
expect(duplicate).not_to be_valid
|
||||
expect(duplicate.errors[:external_account_group_id]).to include('has already been taken')
|
||||
end
|
||||
|
||||
it 'validates presence of name' do
|
||||
account_group = build(:account_group, name: nil)
|
||||
expect(account_group).not_to be_valid
|
||||
expect(account_group.errors[:name]).to include("can't be blank")
|
||||
end
|
||||
end
|
||||
|
||||
describe 'when account group is destroyed' do
|
||||
it 'nullifies accounts account_group_id' do
|
||||
account = create(:account, account_group: account_group)
|
||||
|
||||
account_group.destroy
|
||||
|
||||
expect(account.reload.account_group).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,38 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AccountGroupValidation do
|
||||
# Test with User model since it includes the concern
|
||||
describe 'validation' do
|
||||
context 'with account only' do
|
||||
it 'is valid' do
|
||||
user = build(:user, account: create(:account), account_group: nil)
|
||||
expect(user).to be_valid
|
||||
end
|
||||
end
|
||||
|
||||
context 'with account_group only' do
|
||||
it 'is valid' do
|
||||
user = build(:user, account: nil, account_group: create(:account_group))
|
||||
expect(user).to be_valid
|
||||
end
|
||||
end
|
||||
|
||||
context 'with neither account nor account_group' do
|
||||
it 'is invalid' do
|
||||
user = build(:user, account: nil, account_group: nil)
|
||||
expect(user).not_to be_valid
|
||||
expect(user.errors[:base]).to include('Must belong to either an account or account group')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with both account and account_group' do
|
||||
it 'is invalid' do
|
||||
user = build(:user, account: create(:account), account_group: create(:account_group))
|
||||
expect(user).not_to be_valid
|
||||
expect(user.errors[:base]).to include('Cannot belong to both account and account group')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe PartnershipValidation do
|
||||
# Test with User model since it includes the concern
|
||||
describe 'validation' do
|
||||
context 'with account only' do
|
||||
it 'is valid' do
|
||||
user = build(:user, account: create(:account))
|
||||
expect(user).to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,40 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: partnerships
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# name :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# external_partnership_id :integer not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_partnerships_on_external_partnership_id (external_partnership_id) UNIQUE
|
||||
#
|
||||
describe Partnership do
|
||||
let(:partnership) { create(:partnership) }
|
||||
|
||||
describe 'validations' do
|
||||
it 'validates presence of external_partnership_id' do
|
||||
partnership = build(:partnership, external_partnership_id: nil)
|
||||
expect(partnership).not_to be_valid
|
||||
expect(partnership.errors[:external_partnership_id]).to include("can't be blank")
|
||||
end
|
||||
|
||||
it 'validates uniqueness of external_partnership_id' do
|
||||
create(:partnership, external_partnership_id: 123)
|
||||
duplicate = build(:partnership, external_partnership_id: 123)
|
||||
expect(duplicate).not_to be_valid
|
||||
expect(duplicate.errors[:external_partnership_id]).to include('has already been taken')
|
||||
end
|
||||
|
||||
it 'validates presence of name' do
|
||||
partnership = build(:partnership, name: nil)
|
||||
expect(partnership).not_to be_valid
|
||||
expect(partnership.errors[:name]).to include("can't be blank")
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in new issue