santey_vote 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Alexander Shamne
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ = santey_vote
2
+
3
+ rails generate santey_vote
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2010 Alexander Shamne. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "santey_vote"
8
+ gem.summary = %Q{Vote gem}
9
+ gem.description = %Q{longer description of your gem}
10
+ gem.email = "alexander.shamne@gmail.com"
11
+ gem.homepage = "http://github.com/shamne/santey_vote"
12
+ gem.authors = ["Alexander Shamne"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "santey_vote #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,25 @@
1
+ class VotesController < ApplicationController
2
+
3
+ def new
4
+ current_user_id = 0
5
+ if !Vote.already_voted?(current_user_id, params[:type], params[:id], session["v#{params[:type][0,2]}#{params[:id]}"], request.remote_ip) && Vote.check_vote_sum(params[:check])
6
+ vote = Vote.new()
7
+ vote.vote = params[:vote]=="+" ? true : false
8
+ vote.user_id = current_user_id
9
+ vote.voteable_id = params[:id]
10
+ vote.voteable_type = params[:type]
11
+ vote.ip_address = request.remote_ip
12
+ vote.save!
13
+ session["v#{params[:type][0,2]}#{params[:id]}"] = "1"
14
+ end
15
+ @voteable = params[:type].constantize.find(params[:id])
16
+ respond_to do |format|
17
+ format.js {
18
+ render :update do |page|
19
+ page.replace_html "vote_links_#{@voteable.class.name.downcase}_#{@voteable.id}", :partial=>"votes/vote_links", :locals=>{:voteable=>@voteable}
20
+ end
21
+ }
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,21 @@
1
+ class Vote < ActiveRecord::Base
2
+
3
+ belongs_to :voteable, :polymorphic => true
4
+ belongs_to :user
5
+
6
+ def self.generate_vote_sum
7
+ Digest::SHA1.hexdigest("--fggdfd5ghggf--#{Time.now.strftime("%Y-%m-%d-%H")}--")
8
+ end
9
+
10
+ def self.check_vote_sum check
11
+ check == self.generate_vote_sum ? true : false
12
+ end
13
+
14
+ def self.already_voted? current_user_id, v_type, v_id, session, ip
15
+ voted_by_user_or_ip = !Vote.find(:first,
16
+ :conditions => ["(user_id=? AND voteable_id = ? AND voteable_type=?) OR (voteable_id = ? AND voteable_type=? AND ip_address=?)",
17
+ current_user_id, v_id, v_type, v_id, v_type, ip]).nil?
18
+ return (session || voted_by_user_or_ip)
19
+ end
20
+
21
+ end
@@ -0,0 +1,2 @@
1
+ %div.vote_panel{:id=>"vote_links_#{voteable.class.name.downcase}_#{voteable.id}"}
2
+ =render :partial=>"votes/vote_links", :locals=>{:voteable=>voteable}
@@ -0,0 +1,18 @@
1
+ - user_id = voteable.class.name != "User" ? voteable.user_id : voteable.id
2
+ - current_user_id = 0
3
+ - not_users_content = true
4
+ - doesnt_vote = !Vote.already_voted?(current_user_id, voteable.class.name, voteable.id, session["v#{voteable.class.name[0,2]}#{voteable.id}"], request.remote_ip)
5
+
6
+ - if doesnt_vote && not_users_content
7
+ =link_to "+", new_vote_path({:id=>voteable.id, :check=>Vote.generate_vote_sum, :type=>voteable.class.name, :vote=>"+"}), :remote=>true, :class=>"plus"
8
+ - else
9
+ %b.plus +
10
+ %span.plus
11
+ =voteable.votes_for.to_s
12
+
13
+ - if doesnt_vote && not_users_content
14
+ =link_to "-", new_vote_path({:id=>voteable.id, :check=>Vote.generate_vote_sum, :type=>voteable.class.name, :vote=>"-"}), :remote=>true, :class=>"minus"
15
+ - else
16
+ %b.minus -
17
+ %span.minus
18
+ =voteable.votes_against.to_s
@@ -0,0 +1,5 @@
1
+ Rails.application.routes.draw do |map|
2
+
3
+ resources :votes
4
+
5
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails/generators'
2
+
3
+ class SanteyVoteGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ def self.source_root
7
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
8
+ end
9
+
10
+ def self.next_migration_number(dirname)
11
+ Time.now.strftime("%Y%m%d%H%M%S")
12
+ end
13
+
14
+ # copy migrations to db/migrate
15
+ def install_migrations
16
+ migration_template "migrations/create_votes.rb", File.join('db/migrate', "create_votes.rb")
17
+ end
18
+
19
+ end
@@ -0,0 +1,19 @@
1
+ class CreateVotes < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :votes do |t|
4
+ t.column :vote, :boolean, :default => false
5
+ t.column :created_at, :datetime, :null => false
6
+ t.column :voteable_type, :string, :limit => 25, :default => "", :null => false
7
+ t.column :voteable_id, :integer, :default => 0, :null => false
8
+ t.column :user_id, :int
9
+ t.column :ip_address, :string, :limit => 25
10
+ end
11
+
12
+ add_index :votes, [:user_id, :voteable_id, :voteable_type], :name => "index_votes"
13
+ add_index :votes, [:ip_address], :name => "index_votes_ip_address"
14
+ end
15
+
16
+ def self.down
17
+ drop_table :votes
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ module SanteyVote
2
+
3
+ autoload :Voteable, 'santey_vote/voteable'
4
+
5
+ end
6
+
7
+ require 'santey_vote/engine'
@@ -0,0 +1,20 @@
1
+ require "active_record"
2
+
3
+ require "santey_vote"
4
+ require "rails"
5
+
6
+
7
+ module SanteyVote
8
+ class Engine < Rails::Engine
9
+
10
+ #config.after_initialize do
11
+ # ActiveRecord::Base.send :include, SanteyVote::Voteable
12
+ #end
13
+
14
+ end
15
+ end
16
+
17
+
18
+ if defined?(ActiveRecord::Base)
19
+ ActiveRecord::Base.send :include, SanteyVote::Voteable
20
+ end
@@ -0,0 +1,67 @@
1
+ module SanteyVote
2
+ module Voteable
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def santey_vote
10
+ has_many :votes, :as => :voteable, :dependent => :destroy
11
+ include SanteyVote::Voteable::InstanceMethods
12
+ extend SanteyVote::Voteable::SingletonMethods
13
+ end
14
+ end
15
+
16
+ module SingletonMethods
17
+ def find_votes_cast_by_user(user)
18
+ voteable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
19
+ Vote.find(:all,
20
+ :conditions => ["user_id = ? and voteable_type = ?", user.id, voteable],
21
+ :order => "created_at DESC"
22
+ )
23
+ end
24
+ end
25
+
26
+ module InstanceMethods
27
+ def votes_for
28
+ votes = Vote.find(:all, :conditions => [
29
+ "voteable_id = ? AND voteable_type = ? AND vote = TRUE",
30
+ id, self.class.name
31
+ ])
32
+ votes.size
33
+ end
34
+
35
+ def votes_against
36
+ votes = Vote.find(:all, :conditions => [
37
+ "voteable_id = ? AND voteable_type = ? AND vote = FALSE",
38
+ id, self.class.name
39
+ ])
40
+ votes.size
41
+ end
42
+
43
+ # Same as voteable.votes.size
44
+ def votes_count
45
+ self.votes.size
46
+ end
47
+
48
+ def users_who_voted
49
+ users = []
50
+ self.votes.each { |v|
51
+ users << v.user
52
+ }
53
+ users
54
+ end
55
+
56
+ def voted_by_user?(user)
57
+ rtn = false
58
+ if user
59
+ self.votes.each { |v|
60
+ rtn = true if user.id == v.user_id
61
+ }
62
+ end
63
+ rtn
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{santey_vote}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Alexander Shamne"]
12
+ s.date = %q{2010-06-20}
13
+ s.description = %q{longer description of your gem}
14
+ s.email = %q{alexander.shamne@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "app/controllers/votes_controller.rb",
27
+ "app/models/vote.rb",
28
+ "app/views/votes/_vote.html.haml",
29
+ "app/views/votes/_vote_links.html.haml",
30
+ "config/routes.rb",
31
+ "lib/generators/santey_vote_generator.rb",
32
+ "lib/generators/templates/migrations/create_votes.rb",
33
+ "lib/santey_vote.rb",
34
+ "lib/santey_vote/engine.rb",
35
+ "lib/santey_vote/voteable.rb",
36
+ "santey_vote.gemspec",
37
+ "test/santey_vote_test.rb",
38
+ "test/test_helper.rb"
39
+ ]
40
+ s.homepage = %q{http://github.com/shamne/santey_vote}
41
+ s.rdoc_options = ["--charset=UTF-8"]
42
+ s.require_paths = ["lib"]
43
+ s.rubygems_version = %q{1.3.6}
44
+ s.summary = %q{Vote gem}
45
+ s.test_files = [
46
+ "test/santey_vote_test.rb",
47
+ "test/test_helper.rb"
48
+ ]
49
+
50
+ if s.respond_to? :specification_version then
51
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
52
+ s.specification_version = 3
53
+
54
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
55
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
61
+ end
62
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class SanteyVoteTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'santey_vote'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: santey_vote
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Alexander Shamne
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-20 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thoughtbot-shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: longer description of your gem
33
+ email: alexander.shamne@gmail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ - README.rdoc
41
+ files:
42
+ - .document
43
+ - .gitignore
44
+ - LICENSE
45
+ - README.rdoc
46
+ - Rakefile
47
+ - VERSION
48
+ - app/controllers/votes_controller.rb
49
+ - app/models/vote.rb
50
+ - app/views/votes/_vote.html.haml
51
+ - app/views/votes/_vote_links.html.haml
52
+ - config/routes.rb
53
+ - lib/generators/santey_vote_generator.rb
54
+ - lib/generators/templates/migrations/create_votes.rb
55
+ - lib/santey_vote.rb
56
+ - lib/santey_vote/engine.rb
57
+ - lib/santey_vote/voteable.rb
58
+ - santey_vote.gemspec
59
+ - test/santey_vote_test.rb
60
+ - test/test_helper.rb
61
+ has_rdoc: true
62
+ homepage: http://github.com/shamne/santey_vote
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.6
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Vote gem
91
+ test_files:
92
+ - test/santey_vote_test.rb
93
+ - test/test_helper.rb