From b80d0b1a460c1718ea5ff438d878121219da29ef Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 13 Apr 2015 13:41:02 -0300 Subject: [PATCH] vote_plugin: allow anonymous votes --- plugins/vote/lib/ext/vote.rb | 6 +++++- plugins/vote/test/unit/article_test.rb | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/plugins/vote/lib/ext/vote.rb b/plugins/vote/lib/ext/vote.rb index 12f78f6..be349ca 100644 --- a/plugins/vote/lib/ext/vote.rb +++ b/plugins/vote/lib/ext/vote.rb @@ -2,6 +2,10 @@ require_dependency 'models/vote' class Vote - validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id] + validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id], :if => :allow_duplicated_vote? + + def allow_duplicated_vote? + voter.present? + end end diff --git a/plugins/vote/test/unit/article_test.rb b/plugins/vote/test/unit/article_test.rb index 8962ac2..d24ae45 100644 --- a/plugins/vote/test/unit/article_test.rb +++ b/plugins/vote/test/unit/article_test.rb @@ -21,4 +21,12 @@ class ArticleTest < ActiveSupport::TestCase article.destroy end + should 'be able to vote in an article without a user' do + article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil) + assert_difference 'article.votes_for', 2 do + Vote.create!(:voteable => article, :vote => 1) + Vote.create!(:voteable => article, :vote => 1) + end + end + end -- libgit2 0.21.2