2016-09-22 20:06:46 -04:00
|
|
|
namespace :votes do
|
|
|
|
|
desc 'Migrate old votes to new voting system'
|
|
|
|
|
task migrate: :environment do
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
Conference.all.each do |conf|
|
|
|
|
|
VotableField.create(title: 'Overall', conference_id: conf.id, for_admin: true, stars: conf.program.rating, votable_type: 'Event')
|
|
|
|
|
Event.all.each do |event|
|
2017-04-28 10:22:41 +00:00
|
|
|
votes = Vote.where(event_id: event.id)
|
|
|
|
|
break if votes.blank?
|
2016-09-22 20:06:46 -04:00
|
|
|
avg_votes = votes.pluck(:rating).sum / votes.count
|
|
|
|
|
votes.each do |vote|
|
2017-04-28 10:22:41 +00:00
|
|
|
Rate.create(dimension: 'Overall', rater_id: vote.user_id, rateable_type: 'Event', stars: vote.rating, rateable_id: event.id)
|
2016-09-22 20:06:46 -04:00
|
|
|
end
|
|
|
|
|
RatingCache.create(cacheable_id: event.id, cacheable_type: 'Event', avg: avg_votes, qty: votes.count, dimension: 'Overall')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2017-04-28 10:22:41 +00:00
|
|
|
puts 'All done!'
|
2016-09-22 20:06:46 -04:00
|
|
|
end
|
|
|
|
|
end
|