mongoid_voteable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +48 -0
- data/lib/mongoid/voteable.rb +44 -0
- metadata +154 -0
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= Mongoid::Voteable
|
2
|
+
|
3
|
+
Dead simple voting functionality for Mongoid models
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
Add to Gemfile:
|
8
|
+
|
9
|
+
gem 'mongoid_voteable'
|
10
|
+
|
11
|
+
== Getting Started
|
12
|
+
|
13
|
+
Include the module in models where you want it:
|
14
|
+
|
15
|
+
class Post
|
16
|
+
include Mongoid::Document
|
17
|
+
include Mongoid::Voteable
|
18
|
+
...
|
19
|
+
end
|
20
|
+
|
21
|
+
== Cast Votes
|
22
|
+
|
23
|
+
You can vote by passing an integer and a voter model to the "vote" method:
|
24
|
+
|
25
|
+
@post = Post.first
|
26
|
+
@user = User.where(:name => 'Bill') # or more likely, current_user
|
27
|
+
|
28
|
+
@post.vote 1, @user # I like this!
|
29
|
+
@post.vote -1, @user # I don't like this!
|
30
|
+
|
31
|
+
Votes don't have to be up or down, they can include emphasis:
|
32
|
+
|
33
|
+
@post.vote 5, @user # I LOVE this!
|
34
|
+
@post.vote -10, @user # Delete it from the Interwebs
|
35
|
+
|
36
|
+
== Additional Functionality
|
37
|
+
|
38
|
+
You'll often want to know if a user can vote. Simple:
|
39
|
+
|
40
|
+
@post.voted? @user # True if they've voted
|
41
|
+
|
42
|
+
You can also get a tally of the number of votes cast:
|
43
|
+
|
44
|
+
@post.vote_count # Just one so far!
|
45
|
+
|
46
|
+
You can get the average vote:
|
47
|
+
|
48
|
+
@post.vote_average # votes / voters.count
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Voteable
|
3
|
+
|
4
|
+
VERSION = "0.1.0"
|
5
|
+
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
field :votes, :type => Integer, :default => 0
|
10
|
+
field :voters, :type => Array, :default => []
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
|
15
|
+
def vote(num, voter)
|
16
|
+
|
17
|
+
unless voted? voter
|
18
|
+
self.votes += num.to_i
|
19
|
+
self.voters << voter._id
|
20
|
+
collection.update( { "_id" => _id, "voters" => { "$ne" => voter._id } },
|
21
|
+
{ "$inc" => { "votes" => num.to_i }, "$push" => { "voters" => voter._id } } )
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def voted?(voter)
|
27
|
+
voters.include? voter._id
|
28
|
+
end
|
29
|
+
|
30
|
+
def vote_count
|
31
|
+
voters.count
|
32
|
+
end
|
33
|
+
|
34
|
+
def vote_average
|
35
|
+
if voters.blank?
|
36
|
+
nil
|
37
|
+
else
|
38
|
+
votes.to_f / voters.count
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_voteable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jason Coene
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-08 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: mongoid
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 62196419
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
- beta
|
35
|
+
- 16
|
36
|
+
version: 2.0.0.beta.16
|
37
|
+
type: :runtime
|
38
|
+
version_requirements: *id001
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: activesupport
|
41
|
+
prerelease: false
|
42
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 7712042
|
48
|
+
segments:
|
49
|
+
- 3
|
50
|
+
- 0
|
51
|
+
- 0
|
52
|
+
- rc
|
53
|
+
version: 3.0.0.rc
|
54
|
+
type: :runtime
|
55
|
+
version_requirements: *id002
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: bson_ext
|
58
|
+
prerelease: false
|
59
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 31
|
65
|
+
segments:
|
66
|
+
- 1
|
67
|
+
- 0
|
68
|
+
- 4
|
69
|
+
version: 1.0.4
|
70
|
+
type: :development
|
71
|
+
version_requirements: *id003
|
72
|
+
- !ruby/object:Gem::Dependency
|
73
|
+
name: rspec
|
74
|
+
prerelease: false
|
75
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 62196421
|
81
|
+
segments:
|
82
|
+
- 2
|
83
|
+
- 0
|
84
|
+
- 0
|
85
|
+
- beta
|
86
|
+
- 19
|
87
|
+
version: 2.0.0.beta.19
|
88
|
+
type: :development
|
89
|
+
version_requirements: *id004
|
90
|
+
- !ruby/object:Gem::Dependency
|
91
|
+
name: database_cleaner
|
92
|
+
prerelease: false
|
93
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
hash: 15
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
- 5
|
102
|
+
- 2
|
103
|
+
version: 0.5.2
|
104
|
+
type: :development
|
105
|
+
version_requirements: *id005
|
106
|
+
description: Provides fields and methods for the manipulation of votes on a Mongoid model.
|
107
|
+
email: jcoene@gmail.com
|
108
|
+
executables: []
|
109
|
+
|
110
|
+
extensions: []
|
111
|
+
|
112
|
+
extra_rdoc_files: []
|
113
|
+
|
114
|
+
files:
|
115
|
+
- lib/mongoid/voteable.rb
|
116
|
+
- README.rdoc
|
117
|
+
has_rdoc: true
|
118
|
+
homepage: http://github.com/jcoene/mongoid_voteable
|
119
|
+
licenses: []
|
120
|
+
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
hash: 3
|
132
|
+
segments:
|
133
|
+
- 0
|
134
|
+
version: "0"
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
none: false
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
hash: 23
|
141
|
+
segments:
|
142
|
+
- 1
|
143
|
+
- 3
|
144
|
+
- 6
|
145
|
+
version: 1.3.6
|
146
|
+
requirements: []
|
147
|
+
|
148
|
+
rubyforge_project:
|
149
|
+
rubygems_version: 1.3.7
|
150
|
+
signing_key:
|
151
|
+
specification_version: 3
|
152
|
+
summary: Voting functionality for Mongoid models
|
153
|
+
test_files: []
|
154
|
+
|