Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Accounts that share the same contact can be merged safely now.

  • Loading branch information...
commit 24d47abccfd97c70894f5e73c22dd492b40065a6 1 parent 5073e66
@steveyken steveyken authored
Showing with 18 additions and 2 deletions.
  1. +9 −2 lib/ffcrm_merge/accounts.rb
  2. +9 −0 spec/lib/accounts_spec.rb
View
11 lib/ffcrm_merge/accounts.rb
@@ -38,9 +38,16 @@ def merge_with(master, ignored_attr = [])
master.send(attr + "=", self.send(attr))
end
end
-
+
+ # ------ Merge contacts
+ self.contacts.each do |contact|
+ # Check if contact belongs to master already? Can happen in CRM's where contacts can belong to multiple accounts
+ if AccountContact.where(:contact_id => contact.id).where(:account_id => master.id).size == 0
+ AccountContact.create(:contact_id => contact.id, :account_id => master.id)
+ end
+ end
+
# ------ Merge 'has_many' associations
- self.contacts.each { |t| t.account = master; t.save! }
self.tasks.each { |t| t.asset = master; t.save! }
self.emails.each { |e| e.mediator = master; e.save! }
self.comments.each { |c| c.commentable = master; c.save! }
View
9 spec/lib/accounts_spec.rb
@@ -156,6 +156,15 @@
expect(@master.assigned_to).to eql(duplicate_attributes['assigned_to'])
end
+
+ it "should successful merge a contact that belong to both accounts without failing" do
+ original_master_contacts = @master.contacts.map(&:id)
+ original_duplicate_contacts = @duplicate.contacts.map(&:id)
+ expected_contacts = Set.new(original_master_contacts) + Set.new(original_duplicate_contacts)
+ @master.contacts << @duplicate.contacts
+ @duplicate.merge_with(@master)
+ expect(@master.contacts.map(&:id)).to eql(expected_contacts.to_a)
+ end
describe "account alias" do
Please sign in to comment.
Something went wrong with that request. Please try again.