Skip to content

Commit

Permalink
Renamed to @master and @Duplicate.
Browse files Browse the repository at this point in the history
  • Loading branch information
steveyken committed Jan 19, 2013
1 parent b26d8e0 commit 4e098e3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 87 deletions.
5 changes: 5 additions & 0 deletions spec/factories/tag_factories.rb
@@ -0,0 +1,5 @@
FactoryGirl.define do
factory :tag do
name { Faker::Internet.user_name }
end
end
60 changes: 30 additions & 30 deletions spec/lib/accounts_spec.rb
Expand Up @@ -2,82 +2,82 @@

describe Account do
before :each do
@account = FactoryGirl.create(:account,
@master = FactoryGirl.create(:account,
:name => "Master Name",
:email => "Master Email",
:background_info => "Master Background Info")
@dup_account = FactoryGirl.create(:account,
@duplicate = FactoryGirl.create(:account,
:name => "Duplicate Name",
:email => "Duplicate Email",
:background_info => "Duplicate Background Info")
4.times do
FactoryGirl.create(:email, :mediator => @account)
FactoryGirl.create(:email, :mediator => @dup_account)
FactoryGirl.create(:comment, :commentable => @account)
FactoryGirl.create(:comment, :commentable => @dup_account)
2.times do
FactoryGirl.create(:email, :mediator => @master)
FactoryGirl.create(:email, :mediator => @duplicate)
FactoryGirl.create(:comment, :commentable => @master)
FactoryGirl.create(:comment, :commentable => @duplicate)
end
end

it "should be able to merge itself into another account" do
# Store the attributes we want to match
dup_account_attr = @dup_account.merge_attributes
dup_has_many_hash = {:emails => @dup_account.emails.dup,
:comments => @dup_account.comments.dup,
:contacts => @dup_account.contacts.dup,
:opportunities => @dup_account.opportunities.dup,
:tasks => @dup_account.tasks.dup}
dup_account_attr = @duplicate.merge_attributes
dup_has_many_hash = {:emails => @duplicate.emails.dup,
:comments => @duplicate.comments.dup,
:contacts => @duplicate.contacts.dup,
:opportunities => @duplicate.opportunities.dup,
:tasks => @duplicate.tasks.dup}

@dup_account.merge_with(@account)
@duplicate.merge_with(@master)

# Check that the duplicate account has been merged
@account.merge_attributes.should == dup_account_attr
@account.user.should == @dup_account.user
@account.billing_address.should == @dup_account.billing_address
@account.shipping_address.should == @dup_account.shipping_address
@master.merge_attributes.should == dup_account_attr
@master.user.should == @duplicate.user
@master.billing_address.should == @duplicate.billing_address
@master.shipping_address.should == @duplicate.shipping_address

# Check that all 'has_many' associations have been transferred
# from the duplicate to the master.
dup_has_many_hash.each do |method, collection|
collection.each do |asset|
@account.send(method).include?(asset).should == true
@master.send(method).include?(asset).should == true
end
end

# Duplicate accounts activities should have been destroyed.
#~ @dup_account.activities.should be_empty
#~ @duplicate.activities.should be_empty

# Check that the account alias has been created correctly.
AccountAlias.find_by_destroyed_account_id(@dup_account.id).account.should == @account
AccountAlias.find_by_destroyed_account_id(@duplicate.id).account.should == @master
end

it "should be able to ignore some attributes when merging" do
ignored_attributes = %w(fax website background_info)

# Save the attributes we want to match
dup_account_attr = @dup_account.merge_attributes
dup_account_attr = @duplicate.merge_attributes

@dup_account.merge_with(@account, ignored_attributes)
@duplicate.merge_with(@master, ignored_attributes)

# Check that the duplicate account has ignored some attributes
ignored_attributes.each do |attr|
@account.send(attr.to_sym).should_not == dup_account_attr[attr]
@master.send(attr.to_sym).should_not == dup_account_attr[attr]
end
# Check that other fields have been merged
%w(name email phone toll_free_phone).each do |attr|
@account.send(attr.to_sym).should == dup_account_attr[attr]
@master.send(attr.to_sym).should == dup_account_attr[attr]
end
end

it "should update existing aliases pointing to the duplicate record" do
@aa1 = AccountAlias.create(:account => @dup_account,
@aa1 = AccountAlias.create(:account => @duplicate,
:destroyed_account_id => 12345)
@aa2 = AccountAlias.create(:account => @dup_account,
@aa2 = AccountAlias.create(:account => @duplicate,
:destroyed_account_id => 23456)
@dup_account.merge_with(@account)
@duplicate.merge_with(@master)

@aa1.reload; @aa2.reload
@aa1.account_id.should == @account.id
@aa2.account_id.should == @account.id
@aa1.account_id.should == @master.id
@aa2.account_id.should == @master.id

end
end
72 changes: 34 additions & 38 deletions spec/lib/contacts_spec.rb
Expand Up @@ -2,89 +2,85 @@

describe Contact do
before :each do
@contact = FactoryGirl.create(:contact,
:title => "Master Contact",
:source => "Master Source",
:background_info => "Master Background Info")
@dup_contact = FactoryGirl.create(:contact,
:title => "Duplicate Contact",
:source => "Duplicate Source",
:background_info => "Duplicate Background Info")
4.times do
FactoryGirl.create(:email, :mediator => @contact)
FactoryGirl.create(:email, :mediator => @dup_contact)
FactoryGirl.create(:comment, :commentable => @contact)
FactoryGirl.create(:comment, :commentable => @dup_contact)
@master = FactoryGirl.create(:contact, :title => "Master Contact",
:source => "Master Source", :background_info => "Master Background Info")
@duplicate = FactoryGirl.create(:contact, :title => "Duplicate Contact",
:source => "Duplicate Source", :background_info => "Duplicate Background Info")
2.times do
FactoryGirl.create(:email, :mediator => @master)
FactoryGirl.create(:email, :mediator => @duplicate)
FactoryGirl.create(:comment, :commentable => @master)
FactoryGirl.create(:comment, :commentable => @duplicate)
end
end

it "should be able to merge itself into another contact" do
# Store the attributes we want to match
dup_contact_attr = @dup_contact.merge_attributes
dup_has_many_hash = {:emails => @dup_contact.emails.dup,
:comments => @dup_contact.comments.dup,
:opportunities => @dup_contact.opportunities.dup,
:tasks => @dup_contact.tasks.dup}
dup_contact_attr = @duplicate.merge_attributes
dup_has_many_hash = {:emails => @duplicate.emails.dup,
:comments => @duplicate.comments.dup,
:opportunities => @duplicate.opportunities.dup,
:tasks => @duplicate.tasks.dup}

@dup_contact.merge_with(@contact)
@duplicate.merge_with(@master)

# Check that the duplicate contact has been merged
@contact.merge_attributes.should == dup_contact_attr
@contact.user.should == @dup_contact.user
@contact.lead.should == @dup_contact.lead
@contact.account.should == @dup_contact.account
@master.merge_attributes.should == dup_contact_attr
@master.user.should == @duplicate.user
@master.lead.should == @duplicate.lead
@master.account.should == @duplicate.account

# Check that all 'has_many' associations have been transferred
# from the duplicate to the master.
dup_has_many_hash.each do |method, collection|
collection.each do |asset|
@contact.send(method).include?(asset).should == true
@master.send(method).include?(asset).should == true
end
end

# Check that the contact alias has been created correctly.
ContactAlias.find_by_destroyed_contact_id(@dup_contact.id).contact.should == @contact
ContactAlias.find_by_destroyed_contact_id(@duplicate.id).contact.should == @master
end

it "should be able to ignore some attributes when merging" do
ignored_attributes = {"_self" => %w(title source background_info)}

# Save the attributes we want to match
dup_contact_attr = @dup_contact.merge_attributes
dup_contact_attr = @duplicate.merge_attributes

@dup_contact.merge_with(@contact, ignored_attributes)
@duplicate.merge_with(@master, ignored_attributes)

# Check that the duplicate contact has ignored some attributes
ignored_attributes["_self"].each do |attr|
@contact.send(attr.to_sym).should_not == dup_contact_attr[attr]
@master.send(attr.to_sym).should_not == dup_contact_attr[attr]
end
# Check that other fields have been merged
%w(first_name last_name access facebook twitter).each do |attr|
@contact.send(attr.to_sym).should == dup_contact_attr[attr]
@master.send(attr.to_sym).should == dup_contact_attr[attr]
end
end

it "should update existing aliases pointing to the duplicate record" do
@ca1 = ContactAlias.create(:contact => @dup_contact,
@ca1 = ContactAlias.create(:contact => @duplicate,
:destroyed_contact_id => 12345)
@ca2 = ContactAlias.create(:contact => @dup_contact,
@ca2 = ContactAlias.create(:contact => @duplicate,
:destroyed_contact_id => 23456)
@dup_contact.merge_with(@contact)
@duplicate.merge_with(@master)

@ca1.reload; @ca2.reload
@ca1.contact_id.should == @contact.id
@ca2.contact_id.should == @contact.id
@ca1.contact_id.should == @master.id
@ca2.contact_id.should == @master.id
end

# it "should delete any aliases pointing to a record when that record is deleted" do
# @ca1 = ContactAlias.create(:contact => @dup_contact,
# @ca1 = ContactAlias.create(:contact => @duplicate,
# :destroyed_contact_id => 12345)
# @ca2 = ContactAlias.create(:contact => @dup_contact,
# @ca2 = ContactAlias.create(:contact => @duplicate,
# :destroyed_contact_id => 23456)

# @dup_contact.destroy
# @duplicate.destroy
#
# ContactAlias.find_all_by_contact_id(@dup_contact.id).should be_empty
# ContactAlias.find_all_by_contact_id(@duplicate.id).should be_empty
#
# end
end
38 changes: 19 additions & 19 deletions spec/lib/custom_fields_spec.rb
Expand Up @@ -3,8 +3,8 @@
describe Contact do
before :each do

@tag_one = FactoryGirl.create(:tag, :taggable_type => "Contact", :name => "TagOne")
@tag_two = FactoryGirl.create(:tag, :taggable_type => "Contact", :name => "TagTwo")
@tag_one = FactoryGirl.create(:tag)
@tag_two = FactoryGirl.create(:tag)

@custom_field_one = Customfield.create!(
:field_name => "field_one",
Expand All @@ -22,60 +22,60 @@
:tag => @tag_two
)

@contact = FactoryGirl.create(:contact,
@master = FactoryGirl.create(:contact,
:title => "Master Contact",
:tag_list => @tag_one.name,
"tag#{@tag_one.id}_attributes".to_sym => {"field_one" => "test value one"})
end

it "should merge different custom fields from both records" do
@dup_contact = FactoryGirl.create(:contact,
@duplicate = FactoryGirl.create(:contact,
:title => "Duplicate Contact",
:tag_list => @tag_two.name,
"tag#{@tag_two.id}_attributes".to_sym => {"field_two" => "test value two"})

@dup_contact.merge_with(@contact)
@duplicate.merge_with(@master)

@contact.reload
@master.reload

[@tag_one, @tag_two].each do |tag|
@contact.tags.should include(tag)
@master.tags.should include(tag)
end
@contact.send("tag#{@tag_one.id}").field_one.should == "test value one"
@contact.send("tag#{@tag_two.id}").field_two.should == "test value two"
@master.send("tag#{@tag_one.id}").field_one.should == "test value one"
@master.send("tag#{@tag_two.id}").field_two.should == "test value two"
end

it "should merge shared custom fields with customfields" do
@dup_contact = FactoryGirl.create(:contact,
@duplicate = FactoryGirl.create(:contact,
:title => "Duplicate Contact",
:tag_list => "#{@tag_one.name}, #{@tag_two.name}",
"tag#{@tag_one.id}_attributes".to_sym => {"field_one" => "duplicate test value one"},
"tag#{@tag_two.id}_attributes".to_sym => {"field_two" => "test value two"}
)

@dup_contact.merge_with(@contact)
@duplicate.merge_with(@master)

@contact.reload
@master.reload

@contact.send("tag#{@tag_one.id}").field_one.should == "duplicate test value one"
@contact.send("tag#{@tag_two.id}").field_two.should == "test value two"
@master.send("tag#{@tag_one.id}").field_one.should == "duplicate test value one"
@master.send("tag#{@tag_two.id}").field_two.should == "test value two"
end

it "should ignore given attributes when merging shared custom fields with customfields" do
@dup_contact = FactoryGirl.create(:contact,
@duplicate = FactoryGirl.create(:contact,
:title => "Duplicate Contact",
:tag_list => "#{@tag_one.name}, #{@tag_two.name}",
"tag#{@tag_one.id}_attributes".to_sym => {"field_one" => "duplicate test value one"},
"tag#{@tag_two.id}_attributes".to_sym => {"field_two" => "test value two"}
)
ignored_attr = {"tags" => {"tagone" => ["field_one"]}}

@dup_contact.merge_with(@contact, ignored_attr)
@duplicate.merge_with(@master, ignored_attr)

@contact.reload
@master.reload

@contact.send("tag#{@tag_one.id}").field_one.should == "test value one"
@contact.send("tag#{@tag_two.id}").field_two.should == "test value two"
@master.send("tag#{@tag_one.id}").field_one.should == "test value one"
@master.send("tag#{@tag_two.id}").field_two.should == "test value two"
end

end

0 comments on commit 4e098e3

Please sign in to comment.