~acesuares/qspell/trunk

« back to all changes in this revision

Viewing changes to vendor/plugins/vote_fu/README.markdown

  • Committer: Ace Suares
  • Date: 2009-10-07 06:16:20 UTC
  • Revision ID: ace@suares.an-20091007061620-j39st30s42edy61t
 * voting with vote_fu

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
vote_fu
 
2
=======
 
3
 
 
4
Allows an arbitrary number of entites (including Users) to vote on models. 
 
5
 
 
6
### Mixins
 
7
This plugin introduces three mixins to your recipe book: 
 
8
 
 
9
1. **acts\_as\_voteable** : Intended for content objects like Posts, Comments, etc. 
 
10
2. **acts\_as\_voter** : Intended for voting entities, like Users. 
 
11
3. **has\_karma**  : Intended for voting entities, or other objects that own the things you're voting on.
 
12
 
 
13
### Inspiration
 
14
 
 
15
This plugin started as an adaptation / update of act\_as\_voteable. It has grown different from that plugin in several ways: 
 
16
 
 
17
1. You can specify the model name that initiates votes. 
 
18
2. You can, with a little tuning, have more than one entity type vote on more than one model type. 
 
19
3. Adds "acts\_as\_voter" behavior to the initiator of votes.
 
20
4. Introduces some newer Rails features like named\_scope and :polymorphic keywords
 
21
5. Adds "has\_karma" mixin for identifying key content contributors
 
22
 
 
23
Installation
 
24
============
 
25
Use either the plugin or the gem installation method depending on your preference. If you're not sure, the plugin method is simpler. Whichever you choose, create the migration afterward and run it to create the required model.
 
26
 
 
27
### Via plugin
 
28
    ./script/plugin install git://github.com/peteonrails/vote_fu.git 
 
29
 
 
30
### Via gem
 
31
Add the following to your application's environment.rb:
 
32
    config.gem "peteonrails-vote_fu", :lib => 'vote_fu', :source => 'http://gems.github.com'
 
33
 
 
34
Install the gem:
 
35
    rake gems:install
 
36
 
 
37
### Create vote_fu migration
 
38
Create a new rails migration using your new vote_fu generator (Note: "VoteableModel" is the name of the model on which you would like votes to be cast, e.g. Comment):
 
39
    ./script/generate vote_fu VoteableModel
 
40
 
 
41
Run the migration:
 
42
    rake db:migrate
 
43
 
 
44
Usage
 
45
=====
 
46
 
 
47
## Getting Started
 
48
 
 
49
### Make your ActiveRecord model act as voteable.
 
50
 
 
51
 
 
52
    class Model < ActiveRecord::Base
 
53
          acts_as_voteable
 
54
    end
 
55
 
 
56
 
 
57
### Make your ActiveRecord model(s) that vote act as voter.
 
58
 
 
59
    class User < ActiveRecord::Base
 
60
          acts_as_voter
 
61
    end
 
62
 
 
63
    class Robot < ActiveRecord::Base
 
64
          acts_as_voter
 
65
    end
 
66
 
 
67
### To cast a vote for a Model you can do the following:
 
68
 
 
69
#### Shorthand syntax
 
70
        voter.vote_for(voteable)     # Adds a +1 vote
 
71
        voter.vote_against(voteable) # Adds a -1 vote
 
72
        voter.vote(voteable, t_or_f) # Adds either +1 or -1 vote true => +1, false => -1
 
73
        
 
74
#### ActsAsVoteable syntax
 
75
The old acts\_as\_voteable syntax is still supported:
 
76
 
 
77
    vote = Vote.new(:vote => true)
 
78
    m    = Model.find(params[:id])
 
79
    m.votes    << vote
 
80
    user.votes << vote
 
81
 
 
82
### Querying votes
 
83
 
 
84
#### Tallying Votes
 
85
 
 
86
You can easily retrieve voteable object collections based on the properties of their votes:     
 
87
 
 
88
    @items = Item.tally(
 
89
      {  :at_least => 1, 
 
90
          :at_most => 10000,  
 
91
          :start_at => 2.weeks.ago,
 
92
          :end_at => 1.day.ago,
 
93
          :limit => 10,
 
94
          :order => "items.name desc"
 
95
      })
 
96
 
 
97
This will select the Items with between 1 and 10,000 votes, the votes having been cast within the last two weeks (not including today), then display the 10 last items in an alphabetical list.
 
98
 
 
99
##### Tally Options:
 
100
    :start_at    - Restrict the votes to those created after a certain time
 
101
    :end_at      - Restrict the votes to those created before a certain time
 
102
    :conditions  - A piece of SQL conditions to add to the query
 
103
    :limit       - The maximum number of voteables to return
 
104
    :order       - A piece of SQL to order by. Eg 'votes.count desc' or 'voteable.created_at desc'
 
105
    :at_least    - Item must have at least X votes
 
106
    :at_most     - Item may not have more than X votes
 
107
 
 
108
#### Lower level queries
 
109
ActiveRecord models that act as voteable can be queried for the positive votes, negative votes, and a total vote count by using the votes\_for, votes\_against, and votes\_count methods respectively. Here is an example:
 
110
 
 
111
    positiveVoteCount = m.votes_for
 
112
    negativeVoteCount = m.votes_against
 
113
    totalVoteCount    = m.votes_count
 
114
 
 
115
And because the Vote Fu plugin will add the has_many votes relationship to your model you can always get all the votes by using the votes property:
 
116
 
 
117
    allVotes = m.votes
 
118
 
 
119
The mixin also provides these methods: 
 
120
 
 
121
    voter.voted_for?(voteable)  # True if the voter voted for this object. 
 
122
        voter.vote_count([true|false|"all"]) # returns the count of +1, -1, or all votes 
 
123
 
 
124
        voteable.voted_by?(voter) # True if the voter voted for this object. 
 
125
        @voters = voteable.voters_who_voted
 
126
 
 
127
 
 
128
#### Named Scopes
 
129
 
 
130
The Vote model has several named scopes you can use to find vote details: 
 
131
 
 
132
    @pete_votes = Vote.for_voter(pete)
 
133
    @post_votes = Vote.for_voteable(post)
 
134
    @recent_votes = Vote.recent(1.day.ago)
 
135
    @descending_votes = Vote.descending
 
136
 
 
137
You can chain these together to make interesting queries: 
 
138
 
 
139
    # Show all of Pete's recent votes for a certain Post, in descending order (newest first)
 
140
    @pete_recent_votes_on_post = Vote.for_voter(pete).for_voteable(post).recent(7.days.ago).descending
 
141
 
 
142
### Experimental: Voteable Object Owner Karma
 
143
I have just introduced the "has\_karma" mixin to this package. It aims to assign a karma score to the owners of voteable objects. This is designed to allow you to see which users are submitting the most highly voted content. Currently, karma is only "positive". That is, +1 votes add to karma, but -1 votes do not detract from it. 
 
144
 
 
145
    class User 
 
146
      has_many :posts
 
147
      has_karma :posts
 
148
    end
 
149
    
 
150
    class Post
 
151
      acts_as_voteable
 
152
    end
 
153
    
 
154
    # in your view, you can then do this: 
 
155
    Karma: <%= @user.karma %>
 
156
  
 
157
This feature is in alpha, but useful enough that I'm releasing it. 
 
158
 
 
159
### One vote per user!
 
160
If you want to limit your users to a single vote on each item, take a look in lib/vote.rb. 
 
161
 
 
162
    # Uncomment this to limit users to a single vote on each item. 
 
163
    # validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]
 
164
 
 
165
And if you want that enforced at the database level, look in the generated migration for your voteable:
 
166
 
 
167
    # If you want to enfore "One Person, One Vote" rules in the database, uncomment the index below
 
168
    # add_index :votes, ["voter_id", "voter_type", "voteable_id", "voteable_type"], :unique => true, :name => "uniq_one_vote_only"
 
169
 
 
170
### Example Application
 
171
 
 
172
There is now a reference application available. Due to overwhelming demand for example 
 
173
code and kickstart guides, I have open-sourced MyQuotable.com in order to provide an 
 
174
easy-to-follow example of how to use VoteFu with RESTful Authentication, JRails, and 
 
175
other popular plugins. To get the example code: 
 
176
 
 
177
    git clone git://github.com/peteonrails/myquotable.git
 
178
 
 
179
There will be a screencast coming soon too. Contact me if you want to help.
 
180
 
 
181
Consideration
 
182
=============
 
183
If you like this software and use it, please consider recommending me on Working With Rails. 
 
184
 
 
185
I don't want donations: a simple up-vote would make my day. My profile is: [http://www.workingwithrails.com/person/12521-peter-jackson][4]
 
186
 
 
187
To go directly to the "Recommend Me" screen: [http://www.workingwithrails.com/recommendation/new/person/12521-peter-jackson][5]
 
188
 
 
189
 
 
190
Credits
 
191
=======
 
192
 
 
193
#### Contributors
 
194
 
 
195
* Bence Nagy, Budapest, Hungary
 
196
* Jon Maddox, Richmond, Virginia, USA
 
197
 
 
198
#### Other works
 
199
 
 
200
[Juixe  - The original ActsAsVoteable plugin inspired this code.][1]
 
201
 
 
202
[Xelipe - This plugin is heavily influenced by Acts As Commentable.][2]
 
203
 
 
204
[1]: http://www.juixe.com/techknow/index.php/2006/06/24/acts-as-voteable-rails-plugin/
 
205
[2]: http://github.com/jackdempsey/acts_as_commentable/tree/master
 
206
 
 
207
More
 
208
====
 
209
 
 
210
Support: [Use my blog for support.][6]
 
211
 
 
212
 
 
213
[Documentation from the original acts\_as\_voteable plugin][3]
 
214
 
 
215
[3]: http://www.juixe.com/techknow/index.php/2006/06/24/acts-as-voteable-rails-plugin/
 
216
[4]: http://www.workingwithrails.com/person/12521-peter-jackson
 
217
[5]: http://www.workingwithrails.com/recommendation/new/person/12521-peter-jackson
 
218
[6]: http://blog.peteonrails.com
 
219
 
 
220
Copyright (c) 2008 Peter Jackson, released under the MIT license