Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for STI inherited classes

  • Loading branch information...
commit 49f284c0d0cce70816867cee880fe7acc3719624 1 parent 405d49e
@arjun810 authored
View
2  lib/acts_as_voteable.rb
@@ -87,7 +87,7 @@ def voters_who_voted
def voted_by?(voter)
0 < Vote.where(
:voteable_id => self.id,
- :voteable_type => self.class.name,
+ :voteable_type => self.class.base_class.name,
:voter_id => voter.id
).count
end
View
16 lib/acts_as_voter.rb
@@ -32,7 +32,7 @@ module InstanceMethods
# user.vote_count() # All votes
def vote_count(for_or_against = :all)
- v = Vote.where(:voter_id => id).where(:voter_type => self.class.name)
+ v = Vote.where(:voter_id => id).where(:voter_type => self.class.base_class.name)
v = case for_or_against
when :all then v
when :up then v.where(:vote => true)
@@ -52,9 +52,9 @@ def voted_against?(voteable)
def voted_on?(voteable)
0 < Vote.where(
:voter_id => self.id,
- :voter_type => self.class.name,
+ :voter_type => self.class.base_class.name,
:voteable_id => voteable.id,
- :voteable_type => voteable.class.name
+ :voteable_type => voteable.class.base_class.name
).count
end
@@ -86,9 +86,9 @@ def vote(voteable, options = {})
def unvote_for(voteable)
Vote.where(
:voter_id => self.id,
- :voter_type => self.class.name,
+ :voter_type => self.class.base_class.name,
:voteable_id => voteable.id,
- :voteable_type => voteable.class.name
+ :voteable_type => voteable.class.base_class.name
).map(&:destroy)
end
@@ -98,13 +98,13 @@ def voted_which_way?(voteable, direction)
raise ArgumentError, "expected :up or :down" unless [:up, :down].include?(direction)
0 < Vote.where(
:voter_id => self.id,
- :voter_type => self.class.name,
+ :voter_type => self.class.base_class.name,
:vote => direction == :up ? true : false,
:voteable_id => voteable.id,
- :voteable_type => voteable.class.name
+ :voteable_type => voteable.class.base_class.name
).count
end
end
end
-end
+end
View
6 lib/generators/thumbs_up/templates/vote.rb
@@ -1,7 +1,7 @@
class Vote < ActiveRecord::Base
- scope :for_voter, lambda { |*args| where(["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.name]) }
- scope :for_voteable, lambda { |*args| where(["voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.name]) }
+ scope :for_voter, lambda { |*args| where(["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.base_class.name]) }
+ scope :for_voteable, lambda { |*args| where(["voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.base_class.name]) }
scope :recent, lambda { |*args| where(["created_at > ?", (args.first || 2.weeks.ago)]) }
scope :descending, order("created_at DESC")
@@ -14,4 +14,4 @@ class Vote < ActiveRecord::Base
# Comment out the line below to allow multiple votes per user.
validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]
<% end %>
-end
+end
View
6 lib/has_karma.rb
@@ -29,10 +29,10 @@ def find_most_karmic
module InstanceMethods
def karma(options = {})
- self.class.karmic_objects.collect do |object, attr|
- v = object.where(["#{Vote.table_name}.vote = ?", true]).where(["#{self.class.table_name}.#{self.class.primary_key} = ?", self.id])
+ self.class.base_class.karmic_objects.collect do |object, attr|
+ v = object.where(["#{Vote.table_name}.vote = ?", true]).where(["#{self.class.base_class.table_name}.#{self.class.base_class.primary_key} = ?", self.id])
v = v.joins("INNER JOIN #{Vote.table_name} ON #{Vote.table_name}.voteable_type = '#{object.to_s}' AND #{Vote.table_name}.voteable_id = #{object.table_name}.#{object.primary_key}")
- v = v.joins("INNER JOIN #{self.class.table_name} ON #{self.class.table_name}.#{self.class.primary_key} = #{object.table_name}.#{attr[0]}")
+ v = v.joins("INNER JOIN #{self.class.base_class.table_name} ON #{self.class.base_class.table_name}.#{self.class.base_class.primary_key} = #{object.table_name}.#{attr[0]}")
(v.count.to_f * attr[1]).round
end.sum
end
View
2  lib/thumbs_up.rb
@@ -4,4 +4,4 @@
ActiveRecord::Base.send(:include, ThumbsUp::ActsAsVoteable)
ActiveRecord::Base.send(:include, ThumbsUp::ActsAsVoter)
-ActiveRecord::Base.send(:include, ThumbsUp::Karma)
+ActiveRecord::Base.send(:include, ThumbsUp::Karma)
Please sign in to comment.
Something went wrong with that request. Please try again.