Skip to content

Commit

Permalink
Add User#take_associations_from to simplify merging two users in the …
Browse files Browse the repository at this point in the history
…console.
  • Loading branch information
kcomandich committed Feb 10, 2015
1 parent 7c3a44d commit c843f33
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/open_conference_ware/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ def sessions
return self.proposals.confirmed
end

# For use in merging duplicate user records
def take_associations_from(dup)
dup.authentications.each { |a| self.authentications << a }
dup.proposals.each { |p| p.add_user(self); p.remove_user(dup) }
dup.user_favorites.each { |f| self.user_favorites << f }
dup.selector_votes.each { |v| self.selector_votes << v }
end

protected

# Does this user require an email to be defined?
Expand Down
12 changes: 12 additions & 0 deletions spec/fixtures/open_conference_ware_users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,15 @@ aaron:
incognito:
created_at: <%= 1.days.ago.to_s :db %>
complete_profile: false

# An almost duplicate of user quentin
quentin2:
proposals: bigtable_session, couchdb_session
email: "[email protected]"
first_name: "Quentin"
last_name: "Tarantino"
created_at: <%= 2.days.ago.to_s :db %>
complete_profile: true
biography: |
I'm called Quentin, French form of the Roman name Quintinus.
56 changes: 56 additions & 0 deletions spec/models/open_conference_ware/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,62 @@
end
end

describe "move all associations from one user account to another" do
fixtures :open_conference_ware_proposals
fixtures :open_conference_ware_users

before(:each) do
@orig = users(:quentin)
@dup = users(:quentin2)

@dup.authentications << build(:authentication)
UserFavorite.add(@dup.id, proposals(:postgresql_session).id)
setup_selector_votes
end

def setup_selector_votes
vote = proposals(:aaron_aardvarks).selector_votes.new(rating: 5, comment: "top choice")
vote.user = @dup
vote.save
end

def take_associations
@orig.take_associations_from(@dup)
end

it "expects duplicate user to start with some associations" do
expect(@dup.authentications.count).to eq 1
expect(@dup.proposals.count).to eq 2
expect(@dup.user_favorites.count).to eq 1
expect(@dup.selector_votes.count).to eq 1
end

it "moves all authentications into original user" do
expect { take_associations }.to change(@orig.authentications, :count).by(1)
end

it "moves all proposals into original user" do
expect { take_associations }.to change(@orig.proposals, :count).by(2)
end

it "moves all user_favorites into original user" do
expect { take_associations }.to change(@orig.user_favorites, :count).by(1)
end

it "moves all selector_votes into original user" do
expect { take_associations }.to change(@orig.selector_votes, :count).by(1)
end

it "removes all associations from duplicate user" do
take_associations
@dup.reload
expect(@dup.authentications.count).to eq 0
expect(@dup.proposals.count).to eq 0
expect(@dup.user_favorites.count).to eq 0
expect(@dup.selector_votes.count).to eq 0
end
end

context "created from an authentication" do
let(:authentication) { build(:authentication) }
subject(:user) { User.create_from_authentication(authentication) }
Expand Down

0 comments on commit c843f33

Please sign in to comment.