Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial release of acts_as_voteable

git-svn-id: http://juixe.com/svn/acts_as_voteable@1 9d6b40db-9003-db11-a6d4-0014220fd315
  • Loading branch information...
commit 2913cd7d3eb2d88c65d8777c17256f1b6506dbaf 0 parents
juixesvn authored
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2006 Cosmin Radoi
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 README
@@ -0,0 +1,4 @@
+ActsAsVoteable
+==============
+
+Description goes here
3  init.rb
@@ -0,0 +1,3 @@
+# Include hook code here
+require 'acts_as_voteable'
+ActiveRecord::Base.send(:include, Juixe::Acts::Voteable)
1  install.rb
@@ -0,0 +1 @@
+# Install hook code here
70 lib/acts_as_voteable.rb
@@ -0,0 +1,70 @@
+# ActsAsVoteable
+module Juixe
+ module Acts #:nodoc:
+ module Voteable #:nodoc:
+
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def acts_as_voteable
+ has_many :votes, :as => :voteable, :dependent => true
+ include Juixe::Acts::Voteable::InstanceMethods
+ extend Juixe::Acts::Voteable::SingletonMethods
+ end
+ end
+
+ # This module contains class methods
+ module SingletonMethods
+ def find_votes_cast_by_user(user)
+ voteable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
+ Vote.find(:all,
+ :conditions => ["user_id = ? and voteable_type = ?", user.id, voteable],
+ :order => "submitted DESC"
+ )
+ end
+ end
+
+ # This module contains instance methods
+ module InstanceMethods
+ def votes_for
+ votes = Vote.find(:all, :conditions => [
+ "voteable_id = ? AND voteable_type = ? AND vote = TRUE",
+ id, self.type.name
+ ])
+ votes.size
+ end
+
+ def votes_against
+ votes = Vote.find(:all, :conditions => [
+ "voteable_id = ? AND voteable_type = ? AND vote = FALSE",
+ id, self.type.name
+ ])
+ votes.size
+ end
+
+ # Same as voteable.votes.size
+ def votes_count
+ self.votes.size
+ end
+
+ def users_who_voted
+ users = []
+ self.votes.each { |v|
+ users << v.user
+ }
+ users
+ end
+
+ def voted_by_user?(user)
+ rtn = false
+ self.votes.each { |v|
+ rtn = true if user.id == v.user.id
+ }
+ rtn
+ end
+ end
+ end
+ end
+end
12 lib/vote.rb
@@ -0,0 +1,12 @@
+class Vote < ActiveRecord::Base
+
+ # NOTE: Votes belong to a user
+ belongs_to :user
+
+ def self.find_votes_cast_by_user(user)
+ find(:all,
+ :conditions => ["user_id = ?", user.id],
+ :order => "submitted DESC"
+ )
+ end
+end
4 tasks/acts_as_voteable_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :acts_as_voteable do
+# # Task goes here
+# end
8 test/acts_as_voteable_test.rb
@@ -0,0 +1,8 @@
+require 'test/unit'
+
+class ActsAsVoteableTest < Test::Unit::TestCase
+ # Replace this with your real tests.
+ def test_this_plugin
+ flunk
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.