voteable_mongoid 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,16 +2,28 @@ module Mongoid
2
2
  module Voteable
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ VOTE_POINT = {}
6
+
5
7
  included do
6
8
  field :up_voter_ids, :type => Array, :default => []
7
9
  field :down_voter_ids, :type => Array, :default => []
8
10
 
9
11
  field :votes_count, :type => Integer, :default => 0
10
12
  field :votes_point, :type => Integer, :default => 0
13
+
11
14
 
12
15
  index :votes_count
13
16
  index :votes_point
14
17
 
18
+ scope :most_voted, order_by(:votes_count.desc)
19
+ scope :best_voted, order_by(:votes_point.desc)
20
+
21
+
22
+ def self.vote_point(klass, options = nil)
23
+ VOTE_POINT[self] ||= {}
24
+ VOTE_POINT[self][klass] ||= options
25
+ end
26
+
15
27
 
16
28
  # We usually need to show current_user his voting value on voteable object
17
29
  # voting value can be nil (not voted yet), :up or :down
@@ -28,17 +40,19 @@ module Mongoid
28
40
  votee_id = BSON::ObjectID(votee_id) if votee_id.is_a?(String)
29
41
  voter_id = BSON::ObjectID(voter_id) if voter_id.is_a?(String)
30
42
 
43
+ value = value.to_sym
44
+
31
45
  if options[:revote]
32
- if value.to_sym == :up
46
+ if value == :up
33
47
  push_field = :up_voter_ids
34
48
  pull_field = :down_voter_ids
35
- point_delta = +2
49
+ point_delta = VOTE_POINT[self][self][:up] - VOTE_POINT[self][self][:down]
36
50
  else
37
51
  push_field = :down_voter_ids
38
52
  pull_field = :up_voter_ids
39
- point_delta = -2
53
+ point_delta = -VOTE_POINT[self][self][:up] + VOTE_POINT[self][self][:down]
40
54
  end
41
-
55
+
42
56
  collection.update({
43
57
  :_id => votee_id,
44
58
  push_field => { '$ne' => voter_id },
@@ -54,10 +68,8 @@ module Mongoid
54
68
  else # new vote
55
69
  if value.to_sym == :up
56
70
  push_field = :up_voter_ids
57
- point_delta = +1
58
71
  else
59
72
  push_field = :down_voter_ids
60
- point_delta = -1
61
73
  end
62
74
 
63
75
  collection.update({
@@ -68,13 +80,43 @@ module Mongoid
68
80
  '$push' => { push_field => voter_id },
69
81
  '$inc' => {
70
82
  :votes_count => +1,
71
- :votes_point => point_delta
83
+ :votes_point => VOTE_POINT[self][self][value]
84
+ }
85
+ })
86
+ end
87
+
88
+ VOTE_POINT[self].each do |klass, value_point|
89
+ next unless association = associations[klass.name.underscore]
90
+ next unless foreign_key = options[association.options[:foreign_key].to_sym]
91
+ klass.collection.update({ :_id => foreign_key }, {
92
+ '$inc' => options[:revote] ? {
93
+ :votes_point => ( value == :up ?
94
+ value_point[:up] - value_point[:down] :
95
+ -value_point[:up] + value_point[:down] )
96
+ } : {
97
+ :votes_count => 1,
98
+ :votes_point => value_point[value]
72
99
  }
73
100
  })
74
101
  end
75
102
  end
76
103
  end
77
104
 
105
+
106
+ def vote(options)
107
+ VOTE_POINT[self.class].each do |klass, value_point|
108
+ next unless association = self.class.associations[klass.name.underscore]
109
+ next unless foreign_key = association.options[:foreign_key]
110
+ options[foreign_key] = read_attribute(foreign_key)
111
+ end
112
+
113
+ options[:votee_id] ||= _id
114
+ options[:revote] ||= !vote_value(options[:voter_id]).nil?
115
+
116
+ self.class.vote(options)
117
+ end
118
+
119
+
78
120
  def vote_value(x)
79
121
  voter_id = case x
80
122
  when String
data/lib/mongoid/voter.rb CHANGED
@@ -18,7 +18,7 @@ module Mongoid
18
18
  votee_class = votee.class
19
19
  votee_id = votee.id
20
20
  else
21
- votee_class = options[:votee_type].constantize
21
+ votee_class = options[:votee_type].classify.constantize
22
22
  votee_id = options[:votee_id]
23
23
  end
24
24
  end
@@ -31,7 +31,7 @@ module Mongoid
31
31
  votee = unless options.is_a?(Hash)
32
32
  options
33
33
  else
34
- options[:votee_type].constantize.only(:up_vote_ids, :down_vote_ids).where(
34
+ options[:votee_type].classify.constantize.only(:up_vote_ids, :down_vote_ids).where(
35
35
  :_id => options[:votee_id]
36
36
  ).first
37
37
  end
@@ -53,7 +53,7 @@ module Mongoid
53
53
  options[:votee_id] = votee.id
54
54
  votee_class = votee.class
55
55
  else
56
- votee_class = options[:votee_type].constantize
56
+ votee_class = options[:votee_type].classify.constantize
57
57
  end
58
58
 
59
59
  options[:revote] = if options.has_key?(:revote)
@@ -64,7 +64,7 @@ module Mongoid
64
64
 
65
65
  options[:voter_id] = _id
66
66
 
67
- votee_class.vote(options)
67
+ ( votee || votee_class ).vote(options)
68
68
  end
69
69
  end
70
70
  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: 17
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
8
  - 3
10
- version: 0.2.3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Nguyen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-03 00:00:00 +07:00
18
+ date: 2010-09-05 00:00:00 +07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency