forked from nabewise/hanging_chad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
71 lines (44 loc) · 1.75 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
HangingChad
===========
HangingChad is a voting plugin for Rails. It solves a lot of issues I had with
acts_as_voteable.
Install
=======
script/plugin install git://github.com/nabewise/hanging_chad.git
script/generate hanging_chad
The generator creates the Vote and VoteTotal models which keep track of the
votes
Usage
=====
To set up a model that can be voted for just use 'has_votes'
class Review < ActiveRecord::Base
has_votes
end
HangingChad supports models having votes for multiple named attributes
class Comment < ActiveRecord::Base
has_votes_for :insightfulness
has_votes_for :controversy
end
Recording a vote for a Review would work like
@review.record_vote(user, true)
or
@review.record_vote(user, false)
Recording a vote for a Comment would work like
@comment.record_vote(user, true, :insightfulness)
Other methods of interest are
Instance Methods:
total_votes(kind=nil) -- number of votes aye_votes(kind=nil) -- number of aye
votes
nay_votes(kind=nil) -- number of nay votes
percent_aye_votes(kind=nil)
percent_nay_votes(kind=nil)
vote_from_user(user, kind=nil) -- true = user has voted aye, false = user has
voted nay, nil = user has not voted
user_voted?(user, kind=nil) -- has the user voted?
toggle_user_vote(user, kind=nil) -- true = toggle user vote to true, false = toggled user vote to false
Class Methods:
sort_by_votes(kind=nil) -- returns collection sorted by votes
include_votes_by_user(user) -- returns a collection with all the users votes
joined in to prevent excess database queries on vote_from_user or user_voted?
(useful for displaying lists of things with the current_user's votes visible).
Copyright (c) 2010 Ben Hughes, released under the MIT license