voteable_mongoid 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,6 @@
1
+ == 0.6.1
2
+ * Set counters and point to 0 for uninitialized voteable objects in order sort and query
3
+
1
4
  == 0.6.0
2
5
  * Minimize vote data store (using short field names votes.u, votes.d, votes.c ...)
3
6
  * Add Voter#up_votees, Voter#down_votees
@@ -17,3 +20,12 @@
17
20
 
18
21
  == 0.4.3
19
22
  * Wrap vote data in voteable namespace (voteable.up_voters_id, voteable.down_voters_ids, voteable.votes_count ...)
23
+
24
+ == 0.4.2
25
+ * Bug fixes
26
+
27
+ == 0.4.0
28
+ * Can unvote
29
+
30
+ == 0.3.5
31
+ * Use mongoid 2.0.0.rc
data/README.rdoc CHANGED
@@ -88,7 +88,14 @@ Rails
88
88
 
89
89
  Ruby
90
90
  Mongoid::Voteable::Stats.remake
91
-
91
+
92
+ === Set counters and point to 0 for uninitialized voteable objects in order sort and query
93
+
94
+ Rails
95
+ rake db:mongoid:voteable:init_stats
96
+
97
+ Ruby
98
+ Mongoid::Voteable::Stats.init
92
99
 
93
100
  == Credits
94
101
 
data/TODO CHANGED
@@ -1 +1,2 @@
1
- * Input options hash validations
1
+ * Option hash validations
2
+ * Refactor Voteable#vote function
@@ -5,6 +5,11 @@ namespace :db do
5
5
  task :remake_stats => :environment do
6
6
  Mongoid::Voteable::Stats.remake(:log)
7
7
  end
8
+
9
+ desc 'Set counters and point to 0 for uninitizized voteable objects'
10
+ task :init_stats => :environment do
11
+ Mongoid::Voteable::Stats.init(:log)
12
+ end
8
13
  end
9
14
  end
10
15
  end
@@ -1,3 +1,3 @@
1
1
  module VoteableMongoid
2
- VERSION = '0.6.0'
2
+ VERSION = '0.6.1'
3
3
  end
@@ -22,7 +22,21 @@ module Mongoid
22
22
  def votes_point
23
23
  votes.try(:[], 'p') || 0
24
24
  end
25
-
25
+
26
+ def self.init(log = false)
27
+ VOTEABLE.each do |class_name, value_point|
28
+ klass = class_name.constantize
29
+ klass_value_point = value_point[class_name]
30
+ puts "Init stats for #{class_name}" if log
31
+ klass.collection.update({:votes => nil}, {
32
+ '$set' => { :votes => VOTES_DEFAULT_ATTRIBUTES }
33
+ }, {
34
+ :safe => true,
35
+ :multi => true
36
+ })
37
+ end
38
+ end
39
+
26
40
  # Re-generate vote counters and vote points
27
41
  def self.remake(log = false)
28
42
  remake_stats(log)
@@ -44,8 +58,6 @@ module Mongoid
44
58
  up_count = up_voter_ids.length
45
59
  down_count = down_voter_ids.length
46
60
 
47
- # puts self, votes.inspect # DEBUG
48
-
49
61
  update_attributes(
50
62
  UP_VOTES_COUNT => up_count,
51
63
  DOWN_VOTES_COUNT => down_count,
@@ -91,8 +103,6 @@ module Mongoid
91
103
  )
92
104
  end
93
105
 
94
- # puts parent_id, inc_options.inspect # DEBUG
95
-
96
106
  parent_class.collection.update(
97
107
  { :_id => parent_id },
98
108
  { '$inc' => inc_options }
@@ -10,12 +10,19 @@ module Mongoid
10
10
  class Votes
11
11
  include Mongoid::Document
12
12
 
13
- field :u, :type => Array
14
- field :d, :type => Array
13
+ field :u, :type => Array, :default => []
14
+ field :d, :type => Array, :default => []
15
15
  field :uc, :type => Integer, :default => 0
16
16
  field :dc, :type => Integer, :default => 0
17
17
  field :c, :type => Integer, :default => 0
18
18
  field :p, :type => Integer, :default => 0
19
+
20
+ def identity
21
+ # To remove _id
22
+ end
19
23
  end
24
+
25
+ VOTES_DEFAULT_ATTRIBUTES = Votes.new.attributes
26
+ VOTES_DEFAULT_ATTRIBUTES.delete('_id')
20
27
  end
21
28
  end
@@ -11,6 +11,11 @@ module Mongoid
11
11
  include Mongoid::Voteable::Stats
12
12
  field :votes, :type => Mongoid::Voteable::Votes
13
13
 
14
+ after_initialize do
15
+ # Init votes so that counters and point have numeric values (0)
16
+ self.votes = VOTES_DEFAULT_ATTRIBUTES
17
+ end
18
+
14
19
  # Set vote point for each up (down) vote on an object of this class
15
20
  #
16
21
  # @param [Hash] options a hash containings:
@@ -15,7 +15,7 @@ module Mongoid
15
15
  # @param [Class] klass the voteable class, e.g. `Post` or `Comment`
16
16
  # @return [Array, nil] an array of voteable objects up voted by this voter
17
17
  def up_votees(klass)
18
- klass.any_of(Voteable::UP_VOTER_IDS => _id)
18
+ klass.where(Voteable::UP_VOTER_IDS => _id)
19
19
  end
20
20
 
21
21
  # Get list of down voted votees
@@ -23,7 +23,7 @@ module Mongoid
23
23
  # @param [Class] klass the voteable class, e.g. `Post` or `Comment`
24
24
  # @return [Array, nil] an array of voteable objects down voted by this voter
25
25
  def down_votees(klass)
26
- klass.any_of(Voteable::DOWN_VOTER_IDS => _id)
26
+ klass.where(Voteable::DOWN_VOTER_IDS => _id)
27
27
  end
28
28
 
29
29
  # Check to see if this voter voted on the votee or not
@@ -38,6 +38,25 @@ describe Mongoid::Voteable do
38
38
  @user2.votees(Post).should be_empty
39
39
  end
40
40
 
41
+ it 'test Stats.init' do
42
+ @post1.votes.should == Mongoid::Voteable::VOTES_DEFAULT_ATTRIBUTES
43
+ @post2.votes.should == Mongoid::Voteable::VOTES_DEFAULT_ATTRIBUTES
44
+
45
+ @post1.votes = nil
46
+ @post1.save
47
+
48
+ @post2.votes = nil
49
+ @post2.save
50
+
51
+ Mongoid::Voteable::Stats.init
52
+
53
+ @post1.reload
54
+ @post2.reload
55
+
56
+ @post1.votes.should == Mongoid::Voteable::VOTES_DEFAULT_ATTRIBUTES
57
+ @post2.votes.should == Mongoid::Voteable::VOTES_DEFAULT_ATTRIBUTES
58
+ end
59
+
41
60
  it 'revote has no effect' do
42
61
  Post.vote(:revote => true, :votee_id => @post1.id, :voter_id => @user1.id, :value => 'up')
43
62
  @post1.reload
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: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 0
10
- version: 0.6.0
9
+ - 1
10
+ version: 0.6.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Nguyen