voterable 0.0.9 → 0.0.10

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/TODO.md ADDED
@@ -0,0 +1,6 @@
1
+ # Bugs
2
+
3
+ # Features
4
+
5
+ # Questionable Behavior
6
+ 1. Prevent Voteable#update_tally from making multiple database
@@ -1,3 +1,3 @@
1
1
  module Voterable # :nodoc:
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -19,14 +19,13 @@ module Voterable
19
19
  embeds_many :tallys, as: :tallyable, class_name: "Voterable::Tally"
20
20
 
21
21
  #Create Indexes
22
- for i in 0..4
23
- index "tallys.#{i}.point"
24
- index "tallys.#{i}.count"
25
- index "tallys.#{i}.up"
26
- index "tallys.#{i}.down"
27
- end
22
+ index "tallys.point"
23
+ index "tallys.count"
24
+ index "tallys.up"
25
+ index "tallys.down"
28
26
 
29
- after_initialize :update_tally
27
+ after_initialize :update_tally # TODO Shouldn't need to update tallys after save
28
+ # after_initialize :setup
30
29
 
31
30
  VOTEABLE = {}
32
31
  VOTEBACK = {}
@@ -68,15 +67,35 @@ module Voterable
68
67
 
69
68
  #Need to make sure these only return kind of voteable
70
69
  def self.up_voted_by(voter)
71
- votes = Vote.where(voter_id:voter.id, vote: :up)
70
+ votes = Vote.where(voter_id:voter.id, voteable_type:self.name, vote: :up)
72
71
  votes.collect{|x| x.voteable}.compact
73
72
  end
74
73
 
75
74
  def self.down_voted_by(voter)
76
- votes = Vote.where(voter_id:voter.id, vote: :down)
75
+ votes = Vote.where(voter_id:voter.id, voteable_type:self.name, vote: :down)
77
76
  votes.collect{|x| x.voteable}.compact
78
77
  end
79
78
 
79
+ # Returns hash with up and down votes this should be faster than
80
+ # getting up and down voteables seperatly
81
+ #
82
+ # @example getting up and down voted voteables
83
+ # Voteable.voted_on_by(voter) # => {:up => [<up_voted>], :down => [<down_voted>]}
84
+ #
85
+ def self.voted_on_by(voter)
86
+ votes = Vote.where(voter_id:voter.id, voteable_type:self.name)
87
+ up_voted = [] ; down_voted = []
88
+ votes.each do |vt| #Sort voteables in to up and down voted
89
+ if vt.vote == :up
90
+ up_voted << vt.voteable
91
+ elsif vt.vote == :down
92
+ down_voted << vt.voteable
93
+ end
94
+ end
95
+ {:up => up_voted, :down => down_voted}
96
+ end
97
+
98
+
80
99
  def self.sort_by(hsh = {})
81
100
 
82
101
  hsh[:page] ||= 1
@@ -112,7 +131,7 @@ module Voterable
112
131
 
113
132
  # Array into the class and add necessary methods for pagination
114
133
  sorted.instance_variable_set("@current_page", page)
115
- sorted.instance_variable_set("@num_pages", (self.count.to_f/hsh[:limit]).ceil )
134
+ sorted.instance_variable_set("@num_pages", (self.count.to_f/hsh[:limit]).ceil ) # TODO eliminate self.count
116
135
  sorted.instance_variable_set("@limit_value", hsh[:limit])
117
136
  sorted.instance_eval do
118
137
  def current_page
@@ -132,10 +151,6 @@ module Voterable
132
151
 
133
152
  #Instance Methods
134
153
 
135
- def votes_point
136
- point || 0
137
- end
138
-
139
154
  # Vote the voteable thing up or down
140
155
  #
141
156
  # @parma [ Voterable::Voter ] voter Voter that will be doing the voting
@@ -182,18 +197,18 @@ module Voterable
182
197
  return vt
183
198
  end
184
199
 
185
- def unvote(vtr, vote = nil)
200
+ def unvote(vtr, vt = nil)
186
201
  original_points = self.point #Record original points to update user
187
202
 
188
- vote ||= Vote.find(voter_id:vtr.id, voteable_id:self.id)
189
- return if not vote # Return if vote doens't exist
203
+ vt ||= Vote.find(voter_id:vtr.id, voteable_id:self.id)
204
+ return nil unless vt # Return if vote doens't exist
190
205
 
191
- self.point -= self.class.options(vote.vote)
192
- vtr.reputation -= self.class.vtback(vote.vote)
206
+ self.point -= self.class.options(vt.vote)
207
+ vtr.reputation -= self.class.vtback(vt.vote)
193
208
 
194
209
  self.count -= 1
195
210
 
196
- value = vote.vote
211
+ value = vt.vote
197
212
  case value
198
213
  when :up ; self.up -= 1
199
214
  when :down ; self.down -= 1
@@ -204,7 +219,7 @@ module Voterable
204
219
  self.voter.save
205
220
  vtr.save
206
221
 
207
- votes.destroy_all(vote_id:vote.id)
222
+ vt.destroy
208
223
  self.save
209
224
  end
210
225
 
@@ -226,7 +241,7 @@ module Voterable
226
241
  if bracket_votes
227
242
  bracket_votes = bracket_votes.where(:updated_at.lte => time_2).and(:updated_at.gte => time_1)
228
243
  else
229
- bracket_votes = self.votes.where(:updated_at.lte => time_2).and(:updated_at.gte => time_1)
244
+ bracket_votes = self.votes.where(:updated_at.lte => time_2).and(:updated_at.gte => time_1) #.to_a
230
245
  end
231
246
  up_count = bracket_votes.where(vote: :up).count
232
247
  down_count = bracket_votes.where(vote: :down).count
@@ -33,7 +33,7 @@ module Voterable
33
33
  #
34
34
  # @return [Vote] vote that voter cast
35
35
  def vote_for(vtable)
36
- Vote.where(voter_id:self.id, voteable_id:vtable.id).first
36
+ Vote.first(conditions: { voter_id: self.id, voteable_id: vtable.id })
37
37
  end
38
38
 
39
39
  ##
@@ -63,7 +63,7 @@ module Voterable
63
63
  #Things that got votes
64
64
  self.voteables.each do |t|
65
65
  sum+=t.class.options(:init) # Voteable initial value
66
- sum+=t.votes_point
66
+ sum+=t.point
67
67
  end
68
68
 
69
69
  #Vote Back
@@ -69,7 +69,26 @@ describe Voterable::Voteable do #:nodoc: all
69
69
  it{Voteable.first.tallys.count.should == 5}
70
70
  it{Voteable.last.tallys.count.should == 1}
71
71
  end
72
-
73
72
 
73
+ describe ".voted_on_by(voter)" do
74
+ before(:each) do
75
+ @voter = Factory(:voter)
76
+ @up = Factory(:voteable)
77
+ @down = Factory(:voteable)
78
+ @voter.vote(@up,:up)
79
+ @voter.vote(@down,:down)
80
+ end
81
+
82
+ it{Voteable.up_voted_by(@voter).should == [@up]}
83
+ it{Voteable.down_voted_by(@voter).should == [@down]}
84
+
85
+ it "should return up voted voteable" do
86
+ Voteable.voted_on_by(@voter)[:up].should == [@up]
87
+ end
88
+
89
+ it "should return down voted voteable" do
90
+ Voteable.voted_on_by(@voter)[:down].should == [@down]
91
+ end
92
+ end
74
93
 
75
94
  end
@@ -9,7 +9,6 @@ describe "Voting" do # :nodoc: all
9
9
  @owner.voteables << @voteable
10
10
  end
11
11
 
12
- it{@voteable.should respond_to(:votes_point)}
13
12
  it{@voter.should respond_to(:reputation)}
14
13
 
15
14
  context "when up voted by voter" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voterable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-15 00:00:00.000000000Z
12
+ date: 2012-02-21 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
16
- requirement: &70122211454760 !ruby/object:Gem::Requirement
16
+ requirement: &70179104006900 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.3.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70122211454760
24
+ version_requirements: *70179104006900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bson
27
- requirement: &70122211453520 !ruby/object:Gem::Requirement
27
+ requirement: &70179104005780 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - =
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.4.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70122211453520
35
+ version_requirements: *70179104005780
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bson_ext
38
- requirement: &70122211452700 !ruby/object:Gem::Requirement
38
+ requirement: &70179103908660 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - =
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.4.0
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70122211452700
46
+ version_requirements: *70179103908660
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70122211435280 !ruby/object:Gem::Requirement
49
+ requirement: &70179103908260 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70122211435280
57
+ version_requirements: *70179103908260
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: factory_girl
60
- requirement: &70122211432600 !ruby/object:Gem::Requirement
60
+ requirement: &70179103907540 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 2.1.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70122211432600
68
+ version_requirements: *70179103907540
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard
71
- requirement: &70122211431600 !ruby/object:Gem::Requirement
71
+ requirement: &70179103906880 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70122211431600
79
+ version_requirements: *70179103906880
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: guard-rspec
82
- requirement: &70122211429900 !ruby/object:Gem::Requirement
82
+ requirement: &70179103906160 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70122211429900
90
+ version_requirements: *70179103906160
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: guard-spork
93
- requirement: &70122211428600 !ruby/object:Gem::Requirement
93
+ requirement: &70179103905520 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70122211428600
101
+ version_requirements: *70179103905520
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: database_cleaner
104
- requirement: &70122211427780 !ruby/object:Gem::Requirement
104
+ requirement: &70179103904820 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70122211427780
112
+ version_requirements: *70179103904820
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rb-fsevent
115
- requirement: &70122211426480 !ruby/object:Gem::Requirement
115
+ requirement: &70179103904120 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70122211426480
123
+ version_requirements: *70179103904120
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: growl
126
- requirement: &70122211425760 !ruby/object:Gem::Requirement
126
+ requirement: &70179103903660 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *70122211425760
134
+ version_requirements: *70179103903660
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: rdoc
137
- requirement: &70122211424900 !ruby/object:Gem::Requirement
137
+ requirement: &70179103903220 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,7 +142,7 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *70122211424900
145
+ version_requirements: *70179103903220
146
146
  description: Hackish implementaiton of voting on of voteable objects by a voter
147
147
  email:
148
148
  - benguest@gmail.com
@@ -157,6 +157,7 @@ files:
157
157
  - Guardfile
158
158
  - README.md
159
159
  - Rakefile
160
+ - TODO.md
160
161
  - lib/voterable.rb
161
162
  - lib/voterable/functions.rb
162
163
  - lib/voterable/tally.rb