mongoid_vote 0.0.1.1

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/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mongoid_vote.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "mongoid_vote/version"
2
+ require 'mongoid_vote/voteable'
@@ -0,0 +1,3 @@
1
+ module MongoidVote
2
+ VERSION = "0.0.1.1"
3
+ end
@@ -0,0 +1,59 @@
1
+ module MongoidVote
2
+ module Voteable
3
+ extend ActiveSupport::Concern
4
+
5
+ DEFAULT_VOTES = {
6
+ "up" => [],
7
+ "down" => [],
8
+ "up_count" => 0,
9
+ "down_count" => 0,
10
+ "count" => 0,
11
+ }
12
+
13
+ included do
14
+ field :votes, :type => Hash, :default => DEFAULT_VOTES
15
+ end
16
+
17
+ module InstanceMethods
18
+ def vote(user, vote = nil)
19
+ vote_adjust(self.current_vote(user), vote, user)
20
+ end
21
+
22
+ def current_vote(user)
23
+ if self.votes["up"].include? user.id.to_s
24
+ :up
25
+ elsif self.votes["down"].include? user.id.to_s
26
+ :down
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def vote_adjust(current_vote, vote, user)
33
+ unless current_vote.to_s == vote
34
+ if !current_vote && vote == "up"
35
+ self.votes["up_count"] += 1
36
+ self.votes["count"] += 1
37
+ self.votes["up"] << user.id.to_s
38
+ elsif !current_vote && vote == "down"
39
+ self.votes["down_count"] -= 1
40
+ self.votes["count"] -= 1
41
+ self.votes["down"] << user.id.to_s
42
+ elsif current_vote == :up && vote == "down"
43
+ self.votes["up_count"] -= 1
44
+ self.votes["down_count"] -= 1
45
+ self.votes["count"] -= 1
46
+ self.votes["up"].delete_if {|x| x == user.id.to_s}
47
+ self.votes["down"] << user.id.to_s
48
+ elsif current_vote == :down && vote == "up"
49
+ self.votes["down_count"] += 1
50
+ self.votes["up_count"] += 1
51
+ self.votes["count"] += 1
52
+ self.votes["down"].delete_if {|x| x == user.id.to_s}
53
+ self.votes["up"] << user.id.to_s
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mongoid_vote/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mongoid_vote"
7
+ s.version = MongoidVote::VERSION
8
+ s.authors = ["incorvia"]
9
+ s.email = ["incorvia@gmail.com"]
10
+ s.homepage = "http://www.kevinincorvia.com"
11
+ s.summary = %q{Mongoid Vote}
12
+ s.description = %q{A basic Mongoid voting gem which allows voting on embedded documents.}
13
+
14
+ s.rubyforge_project = "mongoid_vote"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_vote
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - incorvia
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-03 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: A basic Mongoid voting gem which allows voting on embedded documents.
15
+ email:
16
+ - incorvia@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .DS_Store
22
+ - .gitignore
23
+ - Gemfile
24
+ - Rakefile
25
+ - lib/mongoid_vote.rb
26
+ - lib/mongoid_vote/version.rb
27
+ - lib/mongoid_vote/voteable.rb
28
+ - mongoid_vote.gemspec
29
+ homepage: http://www.kevinincorvia.com
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project: mongoid_vote
49
+ rubygems_version: 1.8.10
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Mongoid Vote
53
+ test_files: []