Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: ryanto/acts_as_votable
base: master
...
head fork: keithepley/acts_as_votable
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 7 commits
  • 5 files changed
  • 0 commit comments
  • 2 contributors
View
7 lib/acts_as_votable/votable.rb
@@ -34,7 +34,6 @@ def self.included base
base.class_eval do
- belongs_to :votable, :polymorphic => true
has_many :votes, :class_name => "ActsAsVotable::Vote", :as => :votable do
def voters
includes(:voter).map(&:voter)
@@ -79,7 +78,7 @@ def vote args = {}
# find the vote
_votes_ = find_votes({
:voter_id => options[:voter].id,
- :voter_type => options[:voter].class.name
+ :voter_type => options[:voter].class.base_class.name
})
if _votes_.count == 0
@@ -110,7 +109,7 @@ def vote args = {}
def unvote args = {}
return false if args[:voter].nil?
- _votes_ = find_votes(:voter_id => args[:voter].id, :voter_type => args[:voter].class.name)
+ _votes_ = find_votes(:voter_id => args[:voter].id, :voter_type => args[:voter].class.base_class.name)
return true if _votes_.size == 0
_votes_.each(&:destroy)
@@ -187,7 +186,7 @@ def count_votes_down skip_cache = false
# voters
def voted_on_by? voter
- votes = find_votes :voter_id => voter.id, :voter_type => voter.class.name
+ votes = find_votes :voter_id => voter.id, :voter_type => voter.class.base_class.name
votes.count > 0
end
View
22 lib/acts_as_votable/vote.rb
@@ -5,16 +5,18 @@ class Vote < ::ActiveRecord::Base
include Helpers::Words
- attr_accessible :votable_id, :votable_type,
- :voter_id, :voter_type,
- :votable, :voter,
- :vote_flag
-
- belongs_to :votable, :polymorphic => true
- belongs_to :voter, :polymorphic => true
-
- scope :up, where(:vote_flag => true)
- scope :down, where(:vote_flag => false)
+ if Rails.version.to_i < 4
+ attr_accessible :votable_id, :votable_type,
+ :voter_id, :voter_type,
+ :votable, :voter,
+ :vote_flag
+ end
+
+ belongs_to :votable, :polymorphic => true, :touch => true
+ belongs_to :voter, :polymorphic => true, :touch => true
+
+ scope :up, -> { where vote_flag: true }
+ scope :down, -> { where vote_flag: false }
scope :for_type, lambda{ |klass| where(:votable_type => klass) }
scope :by_type, lambda{ |klass| where(:voter_type => klass) }
View
1  lib/acts_as_votable/voter.rb
@@ -12,7 +12,6 @@ def self.included(base)
base.class_eval do
- belongs_to :voter, :polymorphic => true
has_many :votes, :class_name => "ActsAsVotable::Vote", :as => :voter do
def votables
includes(:votable).map(&:votable)
View
16 spec/spec_helper.rb
@@ -18,6 +18,12 @@
create_table :voters do |t|
t.string :name
+ t.timestamps
+ end
+
+ create_table :sti_voters do |t|
+ t.string :name
+ t.string :type
end
create_table :not_voters do |t|
@@ -26,6 +32,7 @@
create_table :votables do |t|
t.string :name
+ t.timestamps
end
create_table :sti_votables do |t|
@@ -56,6 +63,13 @@ class Voter < ActiveRecord::Base
acts_as_voter
end
+class StiVoter < ActiveRecord::Base
+ acts_as_voter
+end
+
+class ChildOfStiVoter < StiVoter
+end
+
class NotVoter < ActiveRecord::Base
end
@@ -96,7 +110,7 @@ def self.hw
def clean_database
- models = [ActsAsVotable::Vote, Voter, NotVoter, Votable, NotVotable, VotableCache]
+ models = [ActsAsVotable::Vote, Voter, StiVoter, NotVoter, Votable, StiVotable, NotVotable, StiNotVotable, VotableCache]
models.each do |model|
ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
end
View
17 spec/votable_spec.rb
@@ -135,6 +135,12 @@
@votable.vote_registered?.should be true
votable2.vote_registered?.should be false
end
+
+ it "should update its updated_at timestamp" do
+ original_updated_at = @votable.updated_at
+ @votable.vote :voter => @voter
+ @votable.updated_at.should_not equal original_updated_at
+ end
describe "with cached votes" do
@@ -221,7 +227,8 @@
before(:each) do
clean_database
- @voter = Voter.create(:name => 'i can vote!')
+ @voter = StiVoter.create(:name => 'i can vote!')
+ @sti_voter = ChildOfStiVoter.create(:name => "I still live with my parents.")
end
it "should be able to vote on a votable child of a non votable sti model" do
@@ -242,6 +249,14 @@
votable.votes.size.should == 1
end
+ it "should only allow a child voter to vote once" do
+ votable = ChildOfStiVotable.create(:name => 'sti child')
+
+ votable.vote :voter => @sti_voter
+ votable.vote :voter => @sti_voter
+ votable.votes.size.should == 1
+ end
+
end
end

No commit comments for this range

Something went wrong with that request. Please try again.