Commit 237ad20f7471eef89997898542d2cada37ff3b76
1 parent
e53ce3e5
Exists in
master
and in
1 other branch
add task to invalidate orphaned votes
Showing
1 changed file
with
36 additions
and
0 deletions
Show diff stats
lib/tasks/test_api.rake
| 1 | 1 | namespace :test_api do |
| 2 | 2 | |
| 3 | + desc "Searches questions for orphaned votes (votes with no appearance) and marks them as invalid" | |
| 4 | + task :invalidate_orphaned_votes => :environment do | |
| 5 | + question_ids = ENV["question_ids"].split(/[\s,]+/) | |
| 6 | + question_ids.each do |question_id| | |
| 7 | + question = Question.find(question_id) | |
| 8 | + | |
| 9 | + orphaned_votes = Vote.find(:all, | |
| 10 | + :select => "votes.id", | |
| 11 | + :joins => "LEFT JOIN appearances ON (votes.id = appearances.answerable_id AND answerable_type <> 'Skip')", | |
| 12 | + :conditions => ["answerable_id IS NULL AND votes.valid_record = 1 AND votes.question_id = ?", question.id]) | |
| 13 | + puts "Question ##{question.id} has #{orphaned_votes.count} orphaned votes" | |
| 14 | + orphaned_votes.each do |orphaned_vote_id| | |
| 15 | + orphaned_vote = Vote.find(orphaned_vote_id.id) | |
| 16 | + | |
| 17 | + # attempt to find sibling vote | |
| 18 | + # sibling vote is one that is valid has the same voter and prompt, | |
| 19 | + # is associated with an appearance, and created within 10 seconds | |
| 20 | + sibling_vote = nil | |
| 21 | + votes = Vote.find(:all, :conditions => {:voter_id => orphaned_vote.voter_id, :prompt_id => orphaned_vote.prompt_id}) | |
| 22 | + votes.each do |vote| | |
| 23 | + next if vote.id == orphaned_vote.id | |
| 24 | + next if vote.created_at > orphaned_vote.created_at + 5.seconds | |
| 25 | + next if vote.created_at < orphaned_vote.created_at - 5.seconds | |
| 26 | + next if vote.appearance == nil | |
| 27 | + sibling_vote = vote | |
| 28 | + break | |
| 29 | + end | |
| 30 | + info = "Appearance XXXX already answered" | |
| 31 | + if sibling_vote | |
| 32 | + info = "Appearance #{sibling_vote.appearance.id} already answered" | |
| 33 | + end | |
| 34 | + orphaned_vote.update_attributes!(:valid_record => false, :validity_information => info) | |
| 35 | + end | |
| 36 | + end | |
| 37 | + end | |
| 38 | + | |
| 3 | 39 | desc "Updates cached values for losses and wins for choices." |
| 4 | 40 | task :update_cached_losses_wins => :environment do |
| 5 | 41 | Question.all.each do |question| | ... | ... |