Skip to content

destroy zombie likes when likable is invalid - Bounty 456 #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions app/jobs/process_like_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ def perform(process_type, like_id)
case process_type
when 'associate_to_user'
begin
like.user_id = User.find_by_tracking_code(like.tracking_code)
like.save!
rescue ActiveRecord::RecordNotUnique => ex
ap ex
if user = User.find_by_tracking_code(like.tracking_code)
like.user = user
like.save!
else
like.destroy
end
rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid => ex
like.destroy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this line. The like was never saved, destroying it make no sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this job is the clean up zombie Likes. Zombie meaning not having the user_id set on the Like record. So we either want to assign the user_id or destroy it

end
end
Expand Down
45 changes: 45 additions & 0 deletions spec/jobs/process_like_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,49 @@
end
end

describe 'processing' do
let(:user) { Fabricate(:user, tracking_code: 'fake_tracking_code') }
let(:protip) { Fabricate(:protip) }

it 'associates the zombie like to the correct user' do
zombie_like = Fabricate(:like, likable: protip,
tracking_code: user.tracking_code)

ProcessLikeJob.new.perform('associate_to_user', zombie_like.id)

zombie_like.reload

expect(zombie_like.user_id).to eql user.id
end

it 'destroys like that are invalid' do
invalid_like = Like.new(value: 1, tracking_code: user.tracking_code)
invalid_like.save(validate: false)

ProcessLikeJob.new.perform('associate_to_user', invalid_like.id)

expect(Like.where(id: invalid_like.id)).not_to exist
end

it 'destroys likes that are non-unique' do
original_like = Fabricate(:like, user: user, likable: protip)

duplicate_like = Fabricate(:like, likable: protip,
tracking_code: user.tracking_code)

ProcessLikeJob.new.perform('associate_to_user', duplicate_like.id)

expect(Like.where(id: duplicate_like.id)).not_to exist
end

it 'destroys likes if no user with the tracking code exists' do
unassociatable_like = Fabricate(:like, likable: protip,
tracking_code: 'unassociatable_tracking_code')

ProcessLikeJob.new.perform('associate_to_user', unassociatable_like.id)

expect(Like.where(id: unassociatable_like.id)).not_to exist
end
end

end