Commit 509ae63b798bb59ad87c90f778b182e900b047dc

Authored by Leandro Santos
2 parents f9f5164a f6c9fd61

Merge branch 'vote_infra' into 'master'

Move vote infra from plugin to core

Move vote infra from plugin to core to avoid dependencies between plugins and allow features that uses the vote_fu plugin to be added in noosfero core.

See merge request !589
app/models/article.rb
... ... @@ -96,6 +96,8 @@ class Article < ActiveRecord::Base
96 96 belongs_to :translation_of, :class_name => 'Article', :foreign_key => :translation_of_id
97 97 before_destroy :rotate_translations
98 98  
  99 + acts_as_voteable
  100 +
99 101 before_create do |article|
100 102 article.published_at ||= Time.now
101 103 if article.reference_article && !article.parent
... ...
app/models/comment.rb
... ... @@ -37,6 +37,8 @@ class Comment < ActiveRecord::Base
37 37  
38 38 xss_terminate :only => [ :body, :title, :name ], :on => 'validation'
39 39  
  40 + acts_as_voteable
  41 +
40 42 def comment_root
41 43 (reply_of && reply_of.comment_root) || self
42 44 end
... ...
app/models/person.rb
... ... @@ -103,6 +103,8 @@ roles] }
103 103  
104 104 belongs_to :user, :dependent => :delete
105 105  
  106 + acts_as_voter
  107 +
106 108 def can_change_homepage?
107 109 !environment.enabled?('cant_change_homepage') || is_admin?
108 110 end
... ...
plugins/vote/lib/ext/article.rb
... ... @@ -1,7 +0,0 @@
1   -require_dependency 'article'
2   -
3   -class Article
4   -
5   - acts_as_voteable
6   -
7   -end
plugins/vote/lib/ext/comment.rb
... ... @@ -1,7 +0,0 @@
1   -require_dependency 'comment'
2   -
3   -class Comment
4   -
5   - acts_as_voteable
6   -
7   -end
plugins/vote/lib/ext/person.rb
... ... @@ -1,7 +0,0 @@
1   -require_dependency 'person'
2   -
3   -class Person
4   -
5   - acts_as_voter
6   -
7   -end
plugins/vote/test/unit/article_test.rb
... ... @@ -1,24 +0,0 @@
1   -require 'test_helper'
2   -
3   -class ArticleTest < ActiveSupport::TestCase
4   -
5   - def setup
6   - @profile = create_user('testing').person
7   - end
8   -
9   - attr_reader :profile
10   -
11   - should 'vote in a article' do
12   - article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil)
13   - profile.vote(article, 5)
14   - assert_equal 1, article.voters_who_voted.length
15   - assert_equal 5, article.votes_total
16   - end
17   -
18   - should 'be able to remove a voted article' do
19   - article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil)
20   - profile.vote(article, 5)
21   - article.destroy
22   - end
23   -
24   -end
plugins/vote/test/unit/comment_test.rb
... ... @@ -1,59 +0,0 @@
1   -require 'test_helper'
2   -
3   -class CommentTest < ActiveSupport::TestCase
4   -
5   - should 'vote in a comment' do
6   - comment = create_comment
7   - person = create_user('voter').person
8   - person.vote(comment, 5)
9   - assert_equal 1, comment.voters_who_voted.length
10   - assert_equal 5, comment.votes_total
11   - end
12   -
13   - should 'like a comment' do
14   - comment = create_comment
15   - person = create_user('voter').person
16   - assert !comment.voted_by?(person, true)
17   - person.vote_for(comment)
18   - assert comment.voted_by?(person, true)
19   - assert !comment.voted_by?(person, false)
20   - end
21   -
22   - should 'count voters for' do
23   - comment = create_comment
24   - person = create_user('voter').person
25   - person2 = create_user('voter2').person
26   - person3 = create_user('voter3').person
27   - person.vote_for(comment)
28   - person2.vote_for(comment)
29   - person3.vote_against(comment)
30   - assert_equal 2, comment.votes_for
31   - end
32   -
33   - should 'count votes againts' do
34   - comment = create_comment
35   - person = create_user('voter').person
36   - person2 = create_user('voter2').person
37   - person3 = create_user('voter3').person
38   - person.vote_against(comment)
39   - person2.vote_against(comment)
40   - person3.vote_for(comment)
41   - assert_equal 2, comment.votes_against
42   - end
43   -
44   - should 'be able to remove a voted comment' do
45   - comment = create_comment
46   - person = create_user('voter').person
47   - person.vote(comment, 5)
48   - comment.destroy
49   - end
50   -
51   - private
52   -
53   - def create_comment(args = {})
54   - owner = create_user('testuser').person
55   - article = create(TextileArticle, :profile_id => owner.id)
56   - create(Comment, { :name => 'foo', :email => 'foo@example.com', :source => article }.merge(args))
57   - end
58   -
59   -end
plugins/vote/test/unit/person_test.rb
... ... @@ -1,134 +0,0 @@
1   -require 'test_helper'
2   -
3   -class PersonTest < ActiveSupport::TestCase
4   -
5   - should 'vote in a comment with value greater than 1' do
6   - comment = fast_create(Comment)
7   - person = fast_create(Person)
8   -
9   - person.vote(comment, 5)
10   - assert_equal 1, person.vote_count
11   - assert_equal 5, person.votes.first.vote
12   - assert person.voted_on?(comment)
13   - end
14   -
15   - should 'vote in a comment with value lesser than -1' do
16   - comment = fast_create(Comment)
17   - person = fast_create(Person)
18   -
19   - person.vote(comment, -5)
20   - assert_equal 1, person.vote_count
21   - assert_equal -5, person.votes.first.vote
22   - end
23   -
24   - should 'vote for a comment' do
25   - comment = fast_create(Comment)
26   - person = fast_create(Person)
27   -
28   - assert !person.voted_for?(comment)
29   - person.vote_for(comment)
30   - assert person.voted_for?(comment)
31   - assert !person.voted_against?(comment)
32   - end
33   -
34   - should 'vote against a comment' do
35   - comment = fast_create(Comment)
36   - person = fast_create(Person)
37   -
38   - assert !person.voted_against?(comment)
39   - person.vote_against(comment)
40   - assert !person.voted_for?(comment)
41   - assert person.voted_against?(comment)
42   - end
43   -
44   - should 'do not vote against a comment twice' do
45   - comment = fast_create(Comment)
46   - person = fast_create(Person)
47   -
48   - assert person.vote_against(comment)
49   - assert !person.vote_against(comment)
50   - end
51   -
52   - should 'do not vote for a comment twice' do
53   - comment = fast_create(Comment)
54   - person = fast_create(Person)
55   -
56   - assert person.vote_for(comment)
57   - assert !person.vote_for(comment)
58   - end
59   -
60   - should 'not vote against a voted for comment' do
61   - comment = fast_create(Comment)
62   - person = fast_create(Person)
63   -
64   - person.vote_for(comment)
65   - person.vote_against(comment)
66   - assert person.voted_for?(comment)
67   - assert !person.voted_against?(comment)
68   - end
69   -
70   - should 'not vote for a voted against comment' do
71   - comment = fast_create(Comment)
72   - person = fast_create(Person)
73   -
74   - person.vote_against(comment)
75   - person.vote_for(comment)
76   - assert !person.voted_for?(comment)
77   - assert person.voted_against?(comment)
78   - end
79   -
80   - should 'undo a vote for a comment' do
81   - comment = fast_create(Comment)
82   - person = fast_create(Person)
83   -
84   - person.vote_for(comment)
85   - assert person.voted_for?(comment)
86   - person.votes.for_voteable(comment).destroy_all
87   - assert !person.voted_for?(comment)
88   - end
89   -
90   - should 'count comments voted' do
91   - comment = fast_create(Comment)
92   - person = fast_create(Person)
93   -
94   - comment2 = fast_create(Comment)
95   - comment3 = fast_create(Comment)
96   - person.vote_for(comment)
97   - person.vote_for(comment2)
98   - person.vote_against(comment3)
99   - assert_equal 3, person.vote_count
100   - assert_equal 2, person.vote_count(true)
101   - assert_equal 1, person.vote_count(false)
102   - end
103   -
104   - should 'vote in a article with value greater than 1' do
105   - article = fast_create(Article)
106   - person = fast_create(Person)
107   -
108   - person.vote(article, 5)
109   - assert_equal 1, person.vote_count
110   - assert_equal 5, person.votes.first.vote
111   - assert person.voted_on?(article)
112   - end
113   -
114   - should 'vote for a article' do
115   - article = fast_create(Article)
116   - person = fast_create(Person)
117   -
118   - assert !person.voted_for?(article)
119   - person.vote_for(article)
120   - assert person.voted_for?(article)
121   - assert !person.voted_against?(article)
122   - end
123   -
124   - should 'vote against a article' do
125   - article = fast_create(Article)
126   - person = fast_create(Person)
127   -
128   - assert !person.voted_against?(article)
129   - person.vote_against(article)
130   - assert !person.voted_for?(article)
131   - assert person.voted_against?(article)
132   - end
133   -
134   -end
test/unit/article_test.rb
... ... @@ -2154,4 +2154,17 @@ class ArticleTest &lt; ActiveSupport::TestCase
2154 2154 assert_equivalent [a1,a2], Article.display_filter(nil, user)
2155 2155 end
2156 2156  
  2157 + should 'vote in a article' do
  2158 + article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil)
  2159 + profile.vote(article, 5)
  2160 + assert_equal 1, article.voters_who_voted.length
  2161 + assert_equal 5, article.votes_total
  2162 + end
  2163 +
  2164 + should 'be able to remove a voted article' do
  2165 + article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil)
  2166 + profile.vote(article, 5)
  2167 + article.destroy
  2168 + end
  2169 +
2157 2170 end
... ...
test/unit/comment_test.rb
... ... @@ -703,6 +703,52 @@ class CommentTest &lt; ActiveSupport::TestCase
703 703 assert_equivalent [c1,c4], Comment.without_reply
704 704 end
705 705  
  706 + should 'vote in a comment' do
  707 + comment = create_comment
  708 + person = create_user('voter').person
  709 + person.vote(comment, 5)
  710 + assert_equal 1, comment.voters_who_voted.length
  711 + assert_equal 5, comment.votes_total
  712 + end
  713 +
  714 + should 'like a comment' do
  715 + comment = create_comment
  716 + person = create_user('voter').person
  717 + assert !comment.voted_by?(person, true)
  718 + person.vote_for(comment)
  719 + assert comment.voted_by?(person, true)
  720 + assert !comment.voted_by?(person, false)
  721 + end
  722 +
  723 + should 'count voters for' do
  724 + comment = create_comment
  725 + person = create_user('voter').person
  726 + person2 = create_user('voter2').person
  727 + person3 = create_user('voter3').person
  728 + person.vote_for(comment)
  729 + person2.vote_for(comment)
  730 + person3.vote_against(comment)
  731 + assert_equal 2, comment.votes_for
  732 + end
  733 +
  734 + should 'count votes againts' do
  735 + comment = create_comment
  736 + person = create_user('voter').person
  737 + person2 = create_user('voter2').person
  738 + person3 = create_user('voter3').person
  739 + person.vote_against(comment)
  740 + person2.vote_against(comment)
  741 + person3.vote_for(comment)
  742 + assert_equal 2, comment.votes_against
  743 + end
  744 +
  745 + should 'be able to remove a voted comment' do
  746 + comment = create_comment
  747 + person = create_user('voter').person
  748 + person.vote(comment, 5)
  749 + comment.destroy
  750 + end
  751 +
706 752 private
707 753  
708 754 def create_comment(args = {})
... ...
test/unit/person_test.rb
... ... @@ -1638,4 +1638,133 @@ class PersonTest &lt; ActiveSupport::TestCase
1638 1638 assert_equal false, person.follows?(nil)
1639 1639 end
1640 1640  
  1641 + should 'vote in a comment with value greater than 1' do
  1642 + comment = fast_create(Comment)
  1643 + person = fast_create(Person)
  1644 +
  1645 + person.vote(comment, 5)
  1646 + assert_equal 1, person.vote_count
  1647 + assert_equal 5, person.votes.first.vote
  1648 + assert person.voted_on?(comment)
  1649 + end
  1650 +
  1651 + should 'vote in a comment with value lesser than -1' do
  1652 + comment = fast_create(Comment)
  1653 + person = fast_create(Person)
  1654 +
  1655 + person.vote(comment, -5)
  1656 + assert_equal 1, person.vote_count
  1657 + assert_equal -5, person.votes.first.vote
  1658 + end
  1659 +
  1660 + should 'vote for a comment' do
  1661 + comment = fast_create(Comment)
  1662 + person = fast_create(Person)
  1663 +
  1664 + assert !person.voted_for?(comment)
  1665 + person.vote_for(comment)
  1666 + assert person.voted_for?(comment)
  1667 + assert !person.voted_against?(comment)
  1668 + end
  1669 +
  1670 + should 'vote against a comment' do
  1671 + comment = fast_create(Comment)
  1672 + person = fast_create(Person)
  1673 +
  1674 + assert !person.voted_against?(comment)
  1675 + person.vote_against(comment)
  1676 + assert !person.voted_for?(comment)
  1677 + assert person.voted_against?(comment)
  1678 + end
  1679 +
  1680 + should 'do not vote against a comment twice' do
  1681 + comment = fast_create(Comment)
  1682 + person = fast_create(Person)
  1683 +
  1684 + assert person.vote_against(comment)
  1685 + assert !person.vote_against(comment)
  1686 + end
  1687 +
  1688 + should 'do not vote for a comment twice' do
  1689 + comment = fast_create(Comment)
  1690 + person = fast_create(Person)
  1691 +
  1692 + assert person.vote_for(comment)
  1693 + assert !person.vote_for(comment)
  1694 + end
  1695 +
  1696 + should 'not vote against a voted for comment' do
  1697 + comment = fast_create(Comment)
  1698 + person = fast_create(Person)
  1699 +
  1700 + person.vote_for(comment)
  1701 + person.vote_against(comment)
  1702 + assert person.voted_for?(comment)
  1703 + assert !person.voted_against?(comment)
  1704 + end
  1705 +
  1706 + should 'not vote for a voted against comment' do
  1707 + comment = fast_create(Comment)
  1708 + person = fast_create(Person)
  1709 +
  1710 + person.vote_against(comment)
  1711 + person.vote_for(comment)
  1712 + assert !person.voted_for?(comment)
  1713 + assert person.voted_against?(comment)
  1714 + end
  1715 +
  1716 + should 'undo a vote for a comment' do
  1717 + comment = fast_create(Comment)
  1718 + person = fast_create(Person)
  1719 +
  1720 + person.vote_for(comment)
  1721 + assert person.voted_for?(comment)
  1722 + person.votes.for_voteable(comment).destroy_all
  1723 + assert !person.voted_for?(comment)
  1724 + end
  1725 +
  1726 + should 'count comments voted' do
  1727 + comment = fast_create(Comment)
  1728 + person = fast_create(Person)
  1729 +
  1730 + comment2 = fast_create(Comment)
  1731 + comment3 = fast_create(Comment)
  1732 + person.vote_for(comment)
  1733 + person.vote_for(comment2)
  1734 + person.vote_against(comment3)
  1735 + assert_equal 3, person.vote_count
  1736 + assert_equal 2, person.vote_count(true)
  1737 + assert_equal 1, person.vote_count(false)
  1738 + end
  1739 +
  1740 + should 'vote in a article with value greater than 1' do
  1741 + article = fast_create(Article)
  1742 + person = fast_create(Person)
  1743 +
  1744 + person.vote(article, 5)
  1745 + assert_equal 1, person.vote_count
  1746 + assert_equal 5, person.votes.first.vote
  1747 + assert person.voted_on?(article)
  1748 + end
  1749 +
  1750 + should 'vote for a article' do
  1751 + article = fast_create(Article)
  1752 + person = fast_create(Person)
  1753 +
  1754 + assert !person.voted_for?(article)
  1755 + person.vote_for(article)
  1756 + assert person.voted_for?(article)
  1757 + assert !person.voted_against?(article)
  1758 + end
  1759 +
  1760 + should 'vote against a article' do
  1761 + article = fast_create(Article)
  1762 + person = fast_create(Person)
  1763 +
  1764 + assert !person.voted_against?(article)
  1765 + person.vote_against(article)
  1766 + assert !person.voted_for?(article)
  1767 + assert person.voted_against?(article)
  1768 + end
  1769 +
1641 1770 end
... ...