lib/voteable_mongoid/voteable/votes.rb in voteable_mongoid-0.6.4 vs lib/voteable_mongoid/voteable/votes.rb in voteable_mongoid-0.7.0
- old
+ new
@@ -1,60 +1,58 @@
module Mongoid
module Voteable
class Votes
include Mongoid::Document
- field :u, :type => Array, :default => []
- field :d, :type => Array, :default => []
- field :uc, :type => Integer, :default => 0
- field :dc, :type => Integer, :default => 0
- field :c, :type => Integer, :default => 0
- field :p, :type => Integer, :default => 0
+ field :up, :type => Array, :default => []
+ field :down, :type => Array, :default => []
+ field :up_count, :type => Integer, :default => 0
+ field :down_count, :type => Integer, :default => 0
+ field :count, :type => Integer, :default => 0
+ field :point, :type => Integer, :default => 0
end
-
- UP_VOTER_IDS = 'votes.u'
- DOWN_VOTER_IDS = 'votes.d'
- UP_VOTES_COUNT = 'votes.uc'
- DOWN_VOTES_COUNT = 'votes.dc'
- VOTES_COUNT = 'votes.c'
- VOTES_POINT = 'votes.p'
VOTES_DEFAULT_ATTRIBUTES = Votes.new.attributes
VOTES_DEFAULT_ATTRIBUTES.delete('_id')
def self.migrate_old_votes(log = false)
- VOTEABLE.each do |class_name, value_point|
+ VOTEABLE.each do |class_name, voteable|
klass = class_name.constantize
- klass_value_point = value_point[class_name]
+ klass_voteable = voteable[class_name]
puts "* Migrating old vote data for #{class_name} ..." if log
- count = 0
+
klass.all.each do |doc|
- next if doc['votes']
- count += 1
- up_voter_ids = doc['up_voter_ids'] || []
- down_voter_ids = doc['down_voter_ids'] || []
+ # Version 0.6.x use very short field names (u, d, uc, dc, c, p) to minimize
+ # votes storage but it's not human friendly
+ # Version >= 0.7.0 use readable field names (up, down, up_count, down_count,
+ # count, point)
+ votes = doc['votes'] || doc['voteable'] || {}
+ up_voter_ids = votes['u'] || votes['up'] || votes['up_voter_ids'] || doc['up_voter_ids'] || []
+ down_voter_ids = votes['d'] || votes['down'] || votes['down_voter_ids'] || doc['down_voter_ids'] || []
+
up_count = up_voter_ids.size
down_count = down_voter_ids.size
+
klass.collection.update({ :_id => doc.id }, {
'$set' => {
- :votes => {
- :u => doc.up_voter_ids,
- :d => doc.down_voter_ids,
- :uc => up_count,
- :dc => down_count,
- :c => up_count + down_count,
- :p => klass_value_point[:up]*up_count + klass_value_point[:down]*down_count
+ 'votes' => {
+ 'up' => up_voter_ids,
+ 'down' => down_voter_ids,
+ 'up_count' => up_count,
+ 'down_count' => down_count,
+ 'count' => up_count + down_count,
+ 'point' => klass_voteable[:up]*up_count + klass_voteable[:down]*down_count
}
},
'$unset' => {
- :up_voter_ids => true,
- :down_voter_ids => true,
- :votes_count => true,
- :votes_point => true
+ 'up_voter_ids' => true,
+ 'down_voter_ids' => true,
+ 'votes_count' => true,
+ 'votes_point' => true,
+ 'voteable' => true
}
})
end
- puts " #{count} objects migrated." if log
end
end
end
end