Module: Mongoid::Voteable::Tasks
- Defined in:
- lib/voteable_mongoid/voteable/tasks.rb
Class Method Summary (collapse)
-
+ (Object) init_stats(log = false)
Set counters and point to 0 for uninitialized voteable objects in order sort and query.
-
+ (Object) migrate_old_votes(log = false)
Convert votes from from version < 0.7.0 to new data store.
- + (Object) migrate_old_votes_for(klass, voteable)
-
+ (Object) remake_stats(log = false)
Re-generate vote counters and vote points.
- + (Object) remake_stats_for(doc, voteable)
- + (Object) remake_stats_for_all_voteable_classes(log)
- + (Object) update_parent_stats(log)
- + (Object) update_parent_stats_for(doc, parent_class, foreign_key, voteable)
Class Method Details
+ (Object) init_stats(log = false)
Set counters and point to 0 for uninitialized voteable objects in order sort and query
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/voteable_mongoid/voteable/tasks.rb', line 7 def self.init_stats(log = false) VOTEABLE.each do |class_name, voteable| klass = class_name.constantize klass_voteable = voteable[class_name] puts "Init stats for #{class_name}" if log klass.collection.update({:votes => nil}, { '$set' => { :votes => Votes::DEFAULT_ATTRIBUTES } }, { :safe => true, :multi => true }) end end |
+ (Object) migrate_old_votes(log = false)
Convert votes from from version < 0.7.0 to new data store
28 29 30 31 32 33 34 35 |
# File 'lib/voteable_mongoid/voteable/tasks.rb', line 28 def self.migrate_old_votes(log = false) VOTEABLE.each do |class_name, voteable| klass = class_name.constantize klass_voteable = voteable[class_name] puts "* Migrating old vote data for #{class_name} ..." if log migrate_old_votes_for(klass, klass_voteable) end end |
+ (Object) migrate_old_votes_for(klass, voteable)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/voteable_mongoid/voteable/tasks.rb', line 37 def self.migrate_old_votes_for(klass, voteable) klass.all.each do |doc| # 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' => { 'up' => up_voter_ids, 'down' => down_voter_ids, 'up_count' => up_count, 'down_count' => down_count, 'count' => up_count + down_count, 'point' => voteable[:up]*up_count + voteable[:down]*down_count } }, '$unset' => { 'up_voter_ids' => true, 'down_voter_ids' => true, 'votes_count' => true, 'votes_point' => true, 'voteable' => true } }, { :safe => true }) end end |
+ (Object) remake_stats(log = false)
Re-generate vote counters and vote points
22 23 24 25 |
# File 'lib/voteable_mongoid/voteable/tasks.rb', line 22 def self.remake_stats(log = false) remake_stats_for_all_voteable_classes(log) update_parent_stats(log) end |
+ (Object) remake_stats_for(doc, voteable)
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/voteable_mongoid/voteable/tasks.rb', line 89 def self.remake_stats_for(doc, voteable) up_count = doc.up_voter_ids.length down_count = doc.down_voter_ids.length doc.update_attributes( 'votes.up_count' => up_count, 'votes.down_count' => down_count, 'votes.count' => up_count + down_count, 'votes.point' => voteable[:up]*up_count + voteable[:down]*down_count ) end |
+ (Object) remake_stats_for_all_voteable_classes(log)
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/voteable_mongoid/voteable/tasks.rb', line 77 def self.remake_stats_for_all_voteable_classes(log) VOTEABLE.each do |class_name, voteable| klass = class_name.constantize klass_voteable = voteable[class_name] puts "Generating stats for #{class_name}" if log klass.all.each{ |doc| remake_stats_for(doc, klass_voteable) } end end |
+ (Object) update_parent_stats(log)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/voteable_mongoid/voteable/tasks.rb', line 102 def self.update_parent_stats(log) VOTEABLE.each do |class_name, voteable| klass = class_name.constantize voteable.each do |parent_class_name, parent_voteable| = klass.relations[parent_class_name.underscore] if parent_class = parent_class_name.constantize foreign_key = .foreign_key puts "Updating stats for #{class_name} > #{parent_class_name}" if log klass.all.each{ |doc| update_parent_stats_for(doc, parent_class, foreign_key, parent_voteable) } end end end end |
+ (Object) update_parent_stats_for(doc, parent_class, foreign_key, voteable)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/voteable_mongoid/voteable/tasks.rb', line 120 def self.update_parent_stats_for(doc, parent_class, foreign_key, voteable) parent_id = doc.read_attribute(foreign_key.to_sym) if parent_id up_count = doc.up_voter_ids.length down_count = doc.down_voter_ids.length return if up_count == 0 && down_count == 0 = { 'votes.point' => voteable[:up]*up_count + voteable[:down]*down_count } unless voteable[:update_counters] == false .merge!( 'votes.count' => up_count + down_count, 'votes.up_count' => up_count, 'votes.down_count' => down_count ) end parent_class.collection.update( { '_id' => parent_id }, { '$inc' => }, { :safe => true } ) end end |