voteable_mongoid 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,6 @@
1
+ == 0.6.3
2
+ * Add rake db:mongoid:voteable:migrate_old_votes to migrate vote data created by version < 0.6.0 to new vote data storage
3
+
1
4
  == 0.6.2
2
5
  * Fix bug: use before_create instead of after_after_initialize
3
6
 
data/README.rdoc CHANGED
@@ -1,10 +1,9 @@
1
1
  = Voteable Mongoid
2
2
 
3
3
  Voteable Mongoid allows you to make your Mongoid::Document objects voteable (up or down)
4
-
5
4
  For instance, in a forum, a user can vote up (or down) on a post or a comment.
6
5
 
7
- When user provided enough information, voteable_mongoid will use only one update-in-place operation per collection.
6
+ Voteable Mongoid is built for speed. It minimizes vote data storage and uses only one database request per collection to do both data validation and data update.
8
7
 
9
8
  Sample app at https://github.com/vinova/simple_qa
10
9
 
@@ -10,6 +10,11 @@ namespace :db do
10
10
  task :init_stats => :environment do
11
11
  Mongoid::Voteable::Stats.init(:log)
12
12
  end
13
+
14
+ desc 'Migrate vote data created by version < 0.6.0 to new vote data storage'
15
+ task :migrate_old_votes => :environment do
16
+ Mongoid::Voteable.migrate_old_votes(:log)
17
+ end
13
18
  end
14
19
  end
15
20
  end
@@ -1,3 +1,3 @@
1
1
  module VoteableMongoid
2
- VERSION = '0.6.2'
2
+ VERSION = '0.6.3'
3
3
  end
@@ -1,28 +1,61 @@
1
1
  module Mongoid
2
2
  module Voteable
3
- UP_VOTER_IDS = 'votes.u'
4
- DOWN_VOTER_IDS = 'votes.d'
5
- UP_VOTES_COUNT = 'votes.uc'
6
- DOWN_VOTES_COUNT = 'votes.dc'
7
- VOTES_COUNT = 'votes.c'
8
- VOTES_POINT = 'votes.p'
9
3
 
10
4
  class Votes
11
5
  include Mongoid::Document
12
-
13
6
  field :u, :type => Array, :default => []
14
7
  field :d, :type => Array, :default => []
15
8
  field :uc, :type => Integer, :default => 0
16
9
  field :dc, :type => Integer, :default => 0
17
10
  field :c, :type => Integer, :default => 0
18
11
  field :p, :type => Integer, :default => 0
19
-
20
- def identity
21
- # To remove _id
22
- end
23
12
  end
13
+
14
+ UP_VOTER_IDS = 'votes.u'
15
+ DOWN_VOTER_IDS = 'votes.d'
16
+ UP_VOTES_COUNT = 'votes.uc'
17
+ DOWN_VOTES_COUNT = 'votes.dc'
18
+ VOTES_COUNT = 'votes.c'
19
+ VOTES_POINT = 'votes.p'
24
20
 
25
21
  VOTES_DEFAULT_ATTRIBUTES = Votes.new.attributes
26
22
  VOTES_DEFAULT_ATTRIBUTES.delete('_id')
23
+
24
+ def self.migrate_old_votes(log = false)
25
+ VOTEABLE.each do |class_name, value_point|
26
+ klass = class_name.constantize
27
+ klass_value_point = value_point[class_name]
28
+ puts "* Migrating old vote data for #{class_name} ..." if log
29
+ count = 0
30
+ klass.all.each do |doc|
31
+ next if doc['votes']
32
+ count += 1
33
+ up_voter_ids = doc['up_voter_ids'] || []
34
+ down_voter_ids = doc['down_voter_ids'] || []
35
+ up_count = up_voter_ids.size
36
+ down_count = down_voter_ids.size
37
+ klass.collection.update({ :_id => doc.id }, {
38
+ '$set' => {
39
+ :votes => {
40
+ :u => doc.up_voter_ids,
41
+ :d => doc.down_voter_ids,
42
+ :uc => up_count,
43
+ :dc => down_count,
44
+ :c => up_count + down_count,
45
+ :p => klass_value_point[:up]*up_count + klass_value_point[:down]*down_count
46
+ }
47
+ },
48
+ '$unset' => {
49
+ :up_voter_ids => true,
50
+ :down_voter_ids => true,
51
+ :votes_count => true,
52
+ :votes_point => true
53
+ }
54
+ })
55
+ end
56
+ puts " #{count} objects migrated." if log
57
+ end
58
+ end
59
+
27
60
  end
28
61
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voteable_mongoid
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 1
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 2
10
- version: 0.6.2
9
+ - 3
10
+ version: 0.6.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Nguyen