diff --git a/app/models/article.rb b/app/models/article.rb index 04516a7..719a7cd 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -96,6 +96,8 @@ class Article < ActiveRecord::Base belongs_to :translation_of, :class_name => 'Article', :foreign_key => :translation_of_id before_destroy :rotate_translations + acts_as_voteable + before_create do |article| article.published_at ||= Time.now if article.reference_article && !article.parent diff --git a/app/models/comment.rb b/app/models/comment.rb index d22d978..c9be027 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -37,6 +37,8 @@ class Comment < ActiveRecord::Base xss_terminate :only => [ :body, :title, :name ], :on => 'validation' + acts_as_voteable + def comment_root (reply_of && reply_of.comment_root) || self end diff --git a/app/models/person.rb b/app/models/person.rb index 9973c1e..15582e9 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -103,6 +103,8 @@ roles] } belongs_to :user, :dependent => :delete + acts_as_voter + def can_change_homepage? !environment.enabled?('cant_change_homepage') || is_admin? end diff --git a/plugins/vote/lib/ext/article.rb b/plugins/vote/lib/ext/article.rb deleted file mode 100644 index dab91a0..0000000 --- a/plugins/vote/lib/ext/article.rb +++ /dev/null @@ -1,7 +0,0 @@ -require_dependency 'article' - -class Article - - acts_as_voteable - -end diff --git a/plugins/vote/lib/ext/comment.rb b/plugins/vote/lib/ext/comment.rb deleted file mode 100644 index 2cb6be6..0000000 --- a/plugins/vote/lib/ext/comment.rb +++ /dev/null @@ -1,7 +0,0 @@ -require_dependency 'comment' - -class Comment - - acts_as_voteable - -end diff --git a/plugins/vote/lib/ext/person.rb b/plugins/vote/lib/ext/person.rb deleted file mode 100644 index 0b3f6a9..0000000 --- a/plugins/vote/lib/ext/person.rb +++ /dev/null @@ -1,7 +0,0 @@ -require_dependency 'person' - -class Person - - acts_as_voter - -end diff --git a/plugins/vote/test/unit/article_test.rb b/plugins/vote/test/unit/article_test.rb deleted file mode 100644 index 8962ac2..0000000 --- a/plugins/vote/test/unit/article_test.rb +++ /dev/null @@ -1,24 +0,0 @@ -require 'test_helper' - -class ArticleTest < ActiveSupport::TestCase - - def setup - @profile = create_user('testing').person - end - - attr_reader :profile - - should 'vote in a article' do - article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil) - profile.vote(article, 5) - assert_equal 1, article.voters_who_voted.length - assert_equal 5, article.votes_total - end - - should 'be able to remove a voted article' do - article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil) - profile.vote(article, 5) - article.destroy - end - -end diff --git a/plugins/vote/test/unit/comment_test.rb b/plugins/vote/test/unit/comment_test.rb deleted file mode 100644 index 355ee64..0000000 --- a/plugins/vote/test/unit/comment_test.rb +++ /dev/null @@ -1,59 +0,0 @@ -require 'test_helper' - -class CommentTest < ActiveSupport::TestCase - - should 'vote in a comment' do - comment = create_comment - person = create_user('voter').person - person.vote(comment, 5) - assert_equal 1, comment.voters_who_voted.length - assert_equal 5, comment.votes_total - end - - should 'like a comment' do - comment = create_comment - person = create_user('voter').person - assert !comment.voted_by?(person, true) - person.vote_for(comment) - assert comment.voted_by?(person, true) - assert !comment.voted_by?(person, false) - end - - should 'count voters for' do - comment = create_comment - person = create_user('voter').person - person2 = create_user('voter2').person - person3 = create_user('voter3').person - person.vote_for(comment) - person2.vote_for(comment) - person3.vote_against(comment) - assert_equal 2, comment.votes_for - end - - should 'count votes againts' do - comment = create_comment - person = create_user('voter').person - person2 = create_user('voter2').person - person3 = create_user('voter3').person - person.vote_against(comment) - person2.vote_against(comment) - person3.vote_for(comment) - assert_equal 2, comment.votes_against - end - - should 'be able to remove a voted comment' do - comment = create_comment - person = create_user('voter').person - person.vote(comment, 5) - comment.destroy - end - - private - - def create_comment(args = {}) - owner = create_user('testuser').person - article = create(TextileArticle, :profile_id => owner.id) - create(Comment, { :name => 'foo', :email => 'foo@example.com', :source => article }.merge(args)) - end - -end diff --git a/plugins/vote/test/unit/person_test.rb b/plugins/vote/test/unit/person_test.rb deleted file mode 100644 index 99bb4e2..0000000 --- a/plugins/vote/test/unit/person_test.rb +++ /dev/null @@ -1,134 +0,0 @@ -require 'test_helper' - -class PersonTest < ActiveSupport::TestCase - - should 'vote in a comment with value greater than 1' do - comment = fast_create(Comment) - person = fast_create(Person) - - person.vote(comment, 5) - assert_equal 1, person.vote_count - assert_equal 5, person.votes.first.vote - assert person.voted_on?(comment) - end - - should 'vote in a comment with value lesser than -1' do - comment = fast_create(Comment) - person = fast_create(Person) - - person.vote(comment, -5) - assert_equal 1, person.vote_count - assert_equal -5, person.votes.first.vote - end - - should 'vote for a comment' do - comment = fast_create(Comment) - person = fast_create(Person) - - assert !person.voted_for?(comment) - person.vote_for(comment) - assert person.voted_for?(comment) - assert !person.voted_against?(comment) - end - - should 'vote against a comment' do - comment = fast_create(Comment) - person = fast_create(Person) - - assert !person.voted_against?(comment) - person.vote_against(comment) - assert !person.voted_for?(comment) - assert person.voted_against?(comment) - end - - should 'do not vote against a comment twice' do - comment = fast_create(Comment) - person = fast_create(Person) - - assert person.vote_against(comment) - assert !person.vote_against(comment) - end - - should 'do not vote for a comment twice' do - comment = fast_create(Comment) - person = fast_create(Person) - - assert person.vote_for(comment) - assert !person.vote_for(comment) - end - - should 'not vote against a voted for comment' do - comment = fast_create(Comment) - person = fast_create(Person) - - person.vote_for(comment) - person.vote_against(comment) - assert person.voted_for?(comment) - assert !person.voted_against?(comment) - end - - should 'not vote for a voted against comment' do - comment = fast_create(Comment) - person = fast_create(Person) - - person.vote_against(comment) - person.vote_for(comment) - assert !person.voted_for?(comment) - assert person.voted_against?(comment) - end - - should 'undo a vote for a comment' do - comment = fast_create(Comment) - person = fast_create(Person) - - person.vote_for(comment) - assert person.voted_for?(comment) - person.votes.for_voteable(comment).destroy_all - assert !person.voted_for?(comment) - end - - should 'count comments voted' do - comment = fast_create(Comment) - person = fast_create(Person) - - comment2 = fast_create(Comment) - comment3 = fast_create(Comment) - person.vote_for(comment) - person.vote_for(comment2) - person.vote_against(comment3) - assert_equal 3, person.vote_count - assert_equal 2, person.vote_count(true) - assert_equal 1, person.vote_count(false) - end - - should 'vote in a article with value greater than 1' do - article = fast_create(Article) - person = fast_create(Person) - - person.vote(article, 5) - assert_equal 1, person.vote_count - assert_equal 5, person.votes.first.vote - assert person.voted_on?(article) - end - - should 'vote for a article' do - article = fast_create(Article) - person = fast_create(Person) - - assert !person.voted_for?(article) - person.vote_for(article) - assert person.voted_for?(article) - assert !person.voted_against?(article) - end - - should 'vote against a article' do - article = fast_create(Article) - person = fast_create(Person) - - assert !person.voted_against?(article) - person.vote_against(article) - assert !person.voted_for?(article) - assert person.voted_against?(article) - end - -end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 658a735..164537e 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -2154,4 +2154,17 @@ class ArticleTest < ActiveSupport::TestCase assert_equivalent [a1,a2], Article.display_filter(nil, user) end + should 'vote in a article' do + article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil) + profile.vote(article, 5) + assert_equal 1, article.voters_who_voted.length + assert_equal 5, article.votes_total + end + + should 'be able to remove a voted article' do + article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil) + profile.vote(article, 5) + article.destroy + end + end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index bdaae6f..802045a 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -703,6 +703,52 @@ class CommentTest < ActiveSupport::TestCase assert_equivalent [c1,c4], Comment.without_reply end + should 'vote in a comment' do + comment = create_comment + person = create_user('voter').person + person.vote(comment, 5) + assert_equal 1, comment.voters_who_voted.length + assert_equal 5, comment.votes_total + end + + should 'like a comment' do + comment = create_comment + person = create_user('voter').person + assert !comment.voted_by?(person, true) + person.vote_for(comment) + assert comment.voted_by?(person, true) + assert !comment.voted_by?(person, false) + end + + should 'count voters for' do + comment = create_comment + person = create_user('voter').person + person2 = create_user('voter2').person + person3 = create_user('voter3').person + person.vote_for(comment) + person2.vote_for(comment) + person3.vote_against(comment) + assert_equal 2, comment.votes_for + end + + should 'count votes againts' do + comment = create_comment + person = create_user('voter').person + person2 = create_user('voter2').person + person3 = create_user('voter3').person + person.vote_against(comment) + person2.vote_against(comment) + person3.vote_for(comment) + assert_equal 2, comment.votes_against + end + + should 'be able to remove a voted comment' do + comment = create_comment + person = create_user('voter').person + person.vote(comment, 5) + comment.destroy + end + private def create_comment(args = {}) diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 7a13921..a2cb697 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -1638,4 +1638,133 @@ class PersonTest < ActiveSupport::TestCase assert_equal false, person.follows?(nil) end + should 'vote in a comment with value greater than 1' do + comment = fast_create(Comment) + person = fast_create(Person) + + person.vote(comment, 5) + assert_equal 1, person.vote_count + assert_equal 5, person.votes.first.vote + assert person.voted_on?(comment) + end + + should 'vote in a comment with value lesser than -1' do + comment = fast_create(Comment) + person = fast_create(Person) + + person.vote(comment, -5) + assert_equal 1, person.vote_count + assert_equal -5, person.votes.first.vote + end + + should 'vote for a comment' do + comment = fast_create(Comment) + person = fast_create(Person) + + assert !person.voted_for?(comment) + person.vote_for(comment) + assert person.voted_for?(comment) + assert !person.voted_against?(comment) + end + + should 'vote against a comment' do + comment = fast_create(Comment) + person = fast_create(Person) + + assert !person.voted_against?(comment) + person.vote_against(comment) + assert !person.voted_for?(comment) + assert person.voted_against?(comment) + end + + should 'do not vote against a comment twice' do + comment = fast_create(Comment) + person = fast_create(Person) + + assert person.vote_against(comment) + assert !person.vote_against(comment) + end + + should 'do not vote for a comment twice' do + comment = fast_create(Comment) + person = fast_create(Person) + + assert person.vote_for(comment) + assert !person.vote_for(comment) + end + + should 'not vote against a voted for comment' do + comment = fast_create(Comment) + person = fast_create(Person) + + person.vote_for(comment) + person.vote_against(comment) + assert person.voted_for?(comment) + assert !person.voted_against?(comment) + end + + should 'not vote for a voted against comment' do + comment = fast_create(Comment) + person = fast_create(Person) + + person.vote_against(comment) + person.vote_for(comment) + assert !person.voted_for?(comment) + assert person.voted_against?(comment) + end + + should 'undo a vote for a comment' do + comment = fast_create(Comment) + person = fast_create(Person) + + person.vote_for(comment) + assert person.voted_for?(comment) + person.votes.for_voteable(comment).destroy_all + assert !person.voted_for?(comment) + end + + should 'count comments voted' do + comment = fast_create(Comment) + person = fast_create(Person) + + comment2 = fast_create(Comment) + comment3 = fast_create(Comment) + person.vote_for(comment) + person.vote_for(comment2) + person.vote_against(comment3) + assert_equal 3, person.vote_count + assert_equal 2, person.vote_count(true) + assert_equal 1, person.vote_count(false) + end + + should 'vote in a article with value greater than 1' do + article = fast_create(Article) + person = fast_create(Person) + + person.vote(article, 5) + assert_equal 1, person.vote_count + assert_equal 5, person.votes.first.vote + assert person.voted_on?(article) + end + + should 'vote for a article' do + article = fast_create(Article) + person = fast_create(Person) + + assert !person.voted_for?(article) + person.vote_for(article) + assert person.voted_for?(article) + assert !person.voted_against?(article) + end + + should 'vote against a article' do + article = fast_create(Article) + person = fast_create(Person) + + assert !person.voted_against?(article) + person.vote_against(article) + assert !person.voted_for?(article) + assert person.voted_against?(article) + end + end -- libgit2 0.21.2