Maintenance: Removed updating of member ids for organization and moved assosciation deletion to own hook.

This commit is contained in:
Rolf Schmidt 2020-11-11 11:07:41 +01:00
parent ca88877bc8
commit f766dc7164
3 changed files with 20 additions and 3 deletions

View file

@ -1,5 +1,5 @@
class App.Organization extends App.Model class App.Organization extends App.Model
@configure 'Organization', 'name', 'shared', 'note', 'member_ids', 'active', 'updated_at' @configure 'Organization', 'name', 'shared', 'note', 'active', 'updated_at'
@extend Spine.Model.Ajax @extend Spine.Model.Ajax
@url: @apiPath + '/organizations' @url: @apiPath + '/organizations'
@configure_attributes = [ @configure_attributes = [

View file

@ -15,11 +15,12 @@ class Organization < ApplicationModel
include Organization::Search include Organization::Search
include Organization::SearchIndex include Organization::SearchIndex
has_many :members, class_name: 'User', dependent: :destroy has_many :members, class_name: 'User'
has_many :tickets, class_name: 'Ticket', dependent: :destroy has_many :tickets, class_name: 'Ticket'
before_create :domain_cleanup before_create :domain_cleanup
before_update :domain_cleanup before_update :domain_cleanup
before_destroy :delete_associations
validates :name, presence: true validates :name, presence: true
validates :domain, presence: { message: 'required when Domain Based Assignment is enabled' }, if: :domain_assignment validates :domain, presence: { message: 'required when Domain Based Assignment is enabled' }, if: :domain_assignment
@ -42,4 +43,8 @@ class Organization < ApplicationModel
true true
end end
def delete_associations
User.where(organization_id: id).find_each(&:destroy)
Ticket.where(organization_id: id).find_each(&:destroy)
end
end end

View file

@ -47,6 +47,18 @@ RSpec.describe Organization, type: :model do
organization.destroy organization.destroy
expect { ticket.reload }.to raise_exception(ActiveRecord::RecordNotFound) expect { ticket.reload }.to raise_exception(ActiveRecord::RecordNotFound)
end end
describe 'when changes for member_ids' do
let(:agent1) { create(:agent) }
let(:agent2) { create(:agent) }
let(:agent3) { create(:agent) }
let(:organization_agents) { create(:organization, member_ids: [agent1.id, agent2.id, agent3.id]) }
it 'does not delete users' do
organization_agents.update(member_ids: [agent1.id, agent2.id])
expect { agent3.reload }.not_to raise_error
end
end
end end
end end