Commit a7066af576c7320d219abe8ddeb3c954271a7c61

Authored by Evandro Junior
1 parent 1b279971

Code refactoring

plugins/relevant_content/lib/ext/article.rb
... ... @@ -2,7 +2,11 @@ require_dependency 'article'
2 2  
3 3 class Article
4 4  
5   - named_scope :relevant_content, :conditions => ["(articles.type != 'UploadedFile' and articles.type != 'Blog' and articles.type != 'RssFeed') OR articles.type is NULL"]
  5 + named_scope :relevant_content, :conditions => ["articles.published = true and (articles.type != 'UploadedFile' and articles.type != 'Blog' and articles.type != 'RssFeed') OR articles.type is NULL"]
  6 +
  7 + def self.articles_columns
  8 + Article.column_names.map {|c| "articles.#{c}"} .join(",")
  9 + end
6 10  
7 11 def self.most_accessed(owner, limit = nil)
8 12 conditions = owner.kind_of?(Environment) ? ["hits > 0"] : ["profile_id = ? and hits > 0", owner.id]
... ... @@ -15,169 +19,77 @@ class Article
15 19 end
16 20  
17 21 def self.most_commented_relevant_content(owner, limit)
18   -
19   - if owner.kind_of?(Environment)
  22 + conditions = owner.kind_of?(Environment) ? ["comments_count > 0"] : ["profile_id = ? and comments_count > 0", owner.id]
20 23 result = Article.relevant_content.find(
21 24 :all,
22 25 :order => 'comments_count desc',
23 26 :limit => limit,
24   - :conditions => ["comments_count > 0"]
25   - )
  27 + :conditions => conditions)
26 28 result.paginate({:page => 1, :per_page => limit})
27   - else
28   - #Owner is a profile
29   - result = Article.relevant_content.find(
30   - :all,
31   - :order => 'comments_count desc',
32   - :limit => limit,
33   - :conditions => ["profile_id = ? and comments_count > 0", owner.id]
34   - )
35   - result.paginate({:page => 1, :per_page => limit})
36   - end
37   - end
38   -
39   - def self.articles_columns
40   - Article.column_names.map {|c| "articles.#{c}"} .join(",")
41 29 end
42 30  
43 31 def self.more_positive_votes(owner, limit = nil)
44   - if owner.kind_of?(Environment)
45   - result = Article.find(
46   - :all,
47   - :select => articles_columns,
48   - :order => 'sum(vote) desc',
49   - :group => 'voteable_id, ' + articles_columns,
50   - :limit => limit,
51   - :having => ['sum(vote) > 0'],
52   - :conditions => {'votes.voteable_type' => 'Article'},
53   - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id'
54   - )
55   - result.paginate({:page => 1, :per_page => limit})
56   - else
57   - #Owner is a profile
58   - result = Article.find(
  32 + conditions = owner.kind_of?(Environment) ? {'votes.voteable_type' => 'Article'} : ["profile_id = ? and votes.voteable_type = ? ", owner.id, 'Article']
  33 + result = Article.relevant_content.find(
59 34 :all,
60   - :select => articles_columns,
61 35 :order => 'sum(vote) desc',
62 36 :group => 'voteable_id, ' + articles_columns,
63 37 :limit => limit,
64   - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id',
65 38 :having => ['sum(vote) > 0'],
66   - :conditions => ["profile_id = ? and votes.voteable_type = ? ", owner.id, 'Article']
67   - )
  39 + :conditions => conditions,
  40 + :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id')
68 41 result.paginate({:page => 1, :per_page => limit})
69   - end
70 42 end
71 43  
72 44 def self.more_negative_votes(owner, limit = nil)
73   - if owner.kind_of?(Environment)
74   - result = Article.find(
  45 + conditions = owner.kind_of?(Environment) ? {'votes.voteable_type' => 'Article'} : ["profile_id = ? and votes.voteable_type = 'Article' ", owner.id]
  46 + result = Article.relevant_content.find(
75 47 :all,
76   - :select => articles_columns,
77 48 :order => 'sum(vote) asc',
78 49 :group => 'voteable_id, ' + articles_columns,
79 50 :limit => limit,
80 51 :having => ['sum(vote) < 0'],
81   - :conditions => {'votes.voteable_type' => 'Article'},
  52 + :conditions => conditions,
82 53 :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id'
83 54 )
84   - result.paginate({:page => 1, :per_page => limit})
85   - else
86   - #Owner is a profile
87   - result = Article.find(
88   - :all,
89   - :select => articles_columns,
90   - :order => 'sum(vote) asc',
91   - :group => 'voteable_id, ' + articles_columns,
92   - :limit => limit,
93   - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id',
94   - :having => ['sum(vote) < 0'],
95   - :conditions => ["profile_id = ? and votes.voteable_type = 'Article' ", owner.id]
96   - )
97 55 result.paginate({:page => 1, :per_page => limit})
98   - end
99 56 end
100 57  
101 58 def self.most_liked(owner, limit = nil)
102   - if owner.kind_of?(Environment)
103   - result = Article.find(
104   - :all,
105   - :select => articles_columns,
106   - :order => 'count(voteable_id) desc',
107   - :group => 'voteable_id, ' + articles_columns,
108   - :limit => limit,
109   - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id',
110   - :conditions => ["votes.voteable_type = 'Article' and vote > 0"]
111   - )
112   - result.paginate({:page => 1, :per_page => limit})
113   - else
114   - #Owner is a profile
115   - result = Article.find(
  59 + conditions = owner.kind_of?(Environment) ? ["votes.voteable_type = 'Article' and vote > 0"] : ["votes.voteable_type = 'Article' and vote > 0 and profile_id = ? ", owner.id]
  60 + result = Article.relevant_content.find(
116 61 :all,
117 62 :select => articles_columns,
118 63 :order => 'count(voteable_id) desc',
119 64 :group => 'voteable_id, ' + articles_columns,
120 65 :limit => limit,
121   - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id',
122   - :conditions => ["votes.voteable_type = 'Article' and vote > 0 and profile_id = ? ", owner.id]
123   - )
  66 + :conditions => conditions,
  67 + :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id')
124 68 result.paginate({:page => 1, :per_page => limit})
125   - end
126 69 end
127 70  
128 71 def self.most_disliked(owner, limit = nil)
129   - if owner.kind_of?(Environment)
130   - result = Article.find(
  72 + conditions = owner.kind_of?(Environment) ? ["votes.voteable_type = 'Article' and vote < 0"] : ["votes.voteable_type = 'Article' and vote < 0 and profile_id = ? ", owner.id]
  73 + result = Article.relevant_content.find(
131 74 :all,
132 75 :order => 'count(voteable_id) desc',
133 76 :group => 'voteable_id, ' + articles_columns,
134 77 :limit => limit,
135   - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id',
136   - :conditions => ["votes.voteable_type = 'Article' and vote < 0"]
137   - )
  78 + :conditions => conditions,
  79 + :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id')
138 80 result.paginate({:page => 1, :per_page => limit})
139   - else
140   - #Owner is a profile
141   - result = Article.find(
142   - :all,
143   - :order => 'count(voteable_id) desc',
144   - :group => 'voteable_id, ' + articles_columns,
145   - :limit => limit,
146   - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id',
147   - :conditions => ["votes.voteable_type = 'Article' and vote < 0 and profile_id = ? ", owner.id]
148   - )
149   - result.paginate({:page => 1, :per_page => limit})
150   - end
151 81 end
152 82  
153 83 def self.most_voted(owner, limit = nil)
154   - if owner.kind_of?(Environment)
155   - result = Article.find(
156   - :all,
157   - :select => articles_columns,
158   - :order => 'count(voteable_id) desc',
159   - :group => 'voteable_id, ' + articles_columns,
160   - :limit => limit,
161   - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id',
162   - :conditions => ["votes.voteable_type = 'Article'"]
163   - )
164   - result.paginate({:page => 1, :per_page => limit})
165   - else
166   - #Owner is a profile
167   - result = Article.find(
  84 + conditions = owner.kind_of?(Environment) ? ["votes.voteable_type = 'Article'"] : ["votes.voteable_type = 'Article' and profile_id = ? ", owner.id]
  85 + result = Article.relevant_content.find(
168 86 :all,
169 87 :select => articles_columns,
170 88 :order => 'count(voteable_id) desc',
171 89 :group => 'voteable_id, ' + articles_columns,
172 90 :limit => limit,
173   - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id',
174   - :conditions => ["votes.voteable_type = 'Article' and profile_id = ? ", owner.id]
175   - )
  91 + :conditions => conditions,
  92 + :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id')
176 93 result.paginate({:page => 1, :per_page => limit})
177   - end
178 94 end
179   -
180   -
181   -
182   -
183 95 end
... ...
plugins/relevant_content/test/unit/article.rb 0 → 100644
... ... @@ -0,0 +1,148 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +require 'comment_controller'
  4 +# Re-raise errors caught by the controller.
  5 +class CommentController; def rescue_action(e) raise e end; end
  6 +
  7 +class RelevantContentBlockTest < ActiveSupport::TestCase
  8 +
  9 + include AuthenticatedTestHelper
  10 + fixtures :users, :environments
  11 +
  12 + def setup
  13 + @controller = CommentController.new
  14 + @request = ActionController::TestRequest.new
  15 + @response = ActionController::TestResponse.new
  16 + @profile = create_user('testinguser').person
  17 + @environment = @profile.environment
  18 + end
  19 + attr_reader :profile, :environment
  20 +
  21 + def enable_vote_plugin
  22 + enabled = false
  23 + environment=Environment.default
  24 + if Noosfero::Plugin.all.include?('VotePlugin')
  25 + if not environment.enabled_plugins.include?(:vote)
  26 + environment.enable_plugin(Vote)
  27 + environment.save!
  28 + end
  29 + enabled = true
  30 + end
  31 + enabled
  32 + end
  33 +
  34 + should 'list most commented articles' do
  35 + Article.delete_all
  36 + a1 = create(TextileArticle, :name => "art 1", :profile_id => profile.id)
  37 + a2 = create(TextileArticle, :name => "art 2", :profile_id => profile.id)
  38 + a3 = create(TextileArticle, :name => "art 3", :profile_id => profile.id)
  39 +
  40 + 2.times { Comment.create(:title => 'test', :body => 'asdsad', :author => profile, :source => a2).save! }
  41 + 4.times { Comment.create(:title => 'test', :body => 'asdsad', :author => profile, :source => a3).save! }
  42 +
  43 + # should respect the order (more commented comes first)
  44 + assert_equal a3.name, profile.articles.most_commented_relevant_content(Environment.default, 3).first.name
  45 + # It is a2 instead of a1 since it does not list articles without comments
  46 + assert_equal a2.name, profile.articles.most_commented_relevant_content(Environment.default, 3).last.name
  47 + end
  48 +
  49 +
  50 + should 'find the most voted' do
  51 + if not enable_vote_plugin
  52 + return
  53 + end
  54 + article = fast_create(Article, {:name=>'2 votes'})
  55 + 2.times{
  56 + person = fast_create(Person)
  57 + person.vote_for(article)
  58 + }
  59 + article = fast_create(Article, {:name=>'10 votes'})
  60 + 10.times{
  61 + person = fast_create(Person)
  62 + person.vote_for(article)
  63 + }
  64 + article = fast_create(Article, {:name=>'5 votes'})
  65 + 5.times{
  66 + person = fast_create(Person)
  67 + person.vote_for(article)
  68 + }
  69 + articles = Article.most_voted(Environment.default, 5)
  70 + assert_equal '10 votes', articles.first.name
  71 + assert_equal '2 votes', articles.last.name
  72 + end
  73 +
  74 + should 'list the most postive' do
  75 + if not enable_vote_plugin
  76 + return
  77 + end
  78 + article = fast_create(Article, {:name=>'23 votes for 20 votes against'})
  79 + 20.times{
  80 + person = fast_create(Person)
  81 + person.vote_against(article)
  82 + }
  83 + 23.times{
  84 + person = fast_create(Person)
  85 + person.vote_for(article)
  86 + }
  87 + article = fast_create(Article, {:name=>'10 votes for 5 votes against'})
  88 + 10.times{
  89 + person = fast_create(Person)
  90 + person.vote_for(article)
  91 + }
  92 + 5.times{
  93 + person = fast_create(Person)
  94 + person.vote_against(article)
  95 + }
  96 + article = fast_create(Article, {:name=>'2 votes against'})
  97 + 2.times{
  98 + person = fast_create(Person)
  99 + person.vote_against(article)
  100 + }
  101 +
  102 + article = fast_create(Article, {:name=>'7 votes for'})
  103 + 7.times{
  104 + person = fast_create(Person)
  105 + person.vote_for(article)
  106 + }
  107 + articles = Article.more_positive_votes(Environment.default, 5)
  108 + assert_equal '7 votes for', articles.first.name
  109 + assert_equal '23 votes for 20 votes against', articles.last.name
  110 + end
  111 +
  112 + should 'list the most negative' do
  113 + if not enable_vote_plugin
  114 + return
  115 + end
  116 + article = fast_create(Article, {:name=>'23 votes for 29 votes against'})
  117 + 29.times{
  118 + person = fast_create(Person)
  119 + person.vote_against(article)
  120 + }
  121 + 23.times{
  122 + person = fast_create(Person)
  123 + person.vote_for(article)
  124 + }
  125 + article = fast_create(Article, {:name=>'10 votes for 15 votes against'})
  126 + 10.times{
  127 + person = fast_create(Person)
  128 + person.vote_for(article)
  129 + }
  130 + 15.times{
  131 + person = fast_create(Person)
  132 + person.vote_against(article)
  133 + }
  134 + article = fast_create(Article, {:name=>'2 votes against'})
  135 + 2.times{
  136 + person = fast_create(Person)
  137 + person.vote_against(article)
  138 + }
  139 + article = fast_create(Article, {:name=>'7 votes for'})
  140 + 7.times{
  141 + person = fast_create(Person)
  142 + person.vote_for(article)
  143 + }
  144 + articles = Article.more_negative_votes(Environment.default, 5)
  145 + assert_equal '23 votes for 29 votes against', articles.first.name
  146 + assert_equal '2 votes against', articles.last.name
  147 + end
  148 +end
0 149 \ No newline at end of file
... ...
plugins/relevant_content/test/unit/relevant_content_block_test.rb
... ... @@ -19,8 +19,6 @@ class RelevantContentBlockTest &lt; ActiveSupport::TestCase
19 19 end
20 20 attr_reader :profile, :environment
21 21  
22   -
23   -
24 22 should 'have a default title' do
25 23 relevant_content_block = RelevantContentPlugin::RelevantContentBlock.new
26 24 block = Block.new
... ... @@ -46,172 +44,4 @@ class RelevantContentBlockTest &lt; ActiveSupport::TestCase
46 44 assert_equal RelevantContentPlugin::RelevantContentBlock.expire_on, {:environment=>[:article], :profile=>[:article]}
47 45 end
48 46  
49   - should 'not raise an exception when finding the most accessed content' do
50   - assert_nothing_raised{
51   - Article.most_accessed(Environment.default, 5)
52   - }
53   - end
54   -
55   - should 'not raise an exception when finding the most commented content' do
56   - assert_nothing_raised{
57   - Article.most_commented_relevant_content(Environment.default, 5)
58   - }
59   - end
60   -
61   - should 'not raise an exception when finding the most liked content' do
62   - begin
63   - Environment.default.enable_plugin(:vote)
64   - rescue
65   - puts "Unable to activate vote plugin"
66   - end
67   - if Environment.default.plugin_enabled?(:vote)
68   - assert_nothing_raised{
69   - Article.most_liked(Environment.default, 5)
70   - }
71   - end
72   - end
73   -
74   - should 'not raise an exception when finding the most disliked content' do
75   - begin
76   - Environment.default.enable_plugin(:vote)
77   - rescue
78   - puts "Unable to activate vote plugin"
79   - end
80   - if Environment.default.plugin_enabled?(:vote)
81   - assert_nothing_raised{
82   - Article.most_disliked(Environment.default, 5)
83   - }
84   - end
85   - end
86   -
87   -
88   - should 'not raise an exception when finding the more positive votes' do
89   - begin
90   - Environment.default.enable_plugin(:vote)
91   - rescue
92   - puts "Unable to activate vote plugin"
93   - end
94   - if Environment.default.plugin_enabled?(:vote)
95   - assert_nothing_raised{
96   - Article.more_positive_votes(Environment.default, 5)
97   - }
98   - end
99   - end
100   -
101   - should 'not raise an exception when finding the most voted' do
102   - begin
103   - Environment.default.enable_plugin(:vote)
104   - rescue
105   - puts "Unable to activate vote plugin"
106   - end
107   - if Environment.default.plugin_enabled?(:vote)
108   - assert_nothing_raised{
109   - Article.most_voted(Environment.default, 5)
110   - }
111   - end
112   - end
113   -
114   - should 'find the most voted' do
115   -
116   - article = fast_create(Article, {:name=>'2 votes'})
117   - for i in 0..2
118   - person = fast_create(Person)
119   - person.vote_for(article)
120   - end
121   -
122   - article = fast_create(Article, {:name=>'10 votes'})
123   - for i in 0..10
124   - person = fast_create(Person)
125   - person.vote_for(article)
126   - end
127   -
128   - article = fast_create(Article, {:name=>'5 votes'})
129   - for i in 0..5
130   - person = fast_create(Person)
131   - person.vote_for(article)
132   - end
133   -
134   - articles = Article.most_voted(Environment.default, 5)
135   - assert_equal '10 votes', articles.first.name
136   - assert_equal '2 votes', articles.last.name
137   - end
138   -
139   - should 'list the most postive' do
140   -
141   - article = fast_create(Article, {:name=>'23 votes for 20 votes against'})
142   - for i in 0..20
143   - person = fast_create(Person)
144   - person.vote_against(article)
145   - end
146   - for i in 0..23
147   - person = fast_create(Person)
148   - person.vote_for(article)
149   - end
150   -
151   - article = fast_create(Article, {:name=>'10 votes for 5 votes against'})
152   - for i in 0..10
153   - person = fast_create(Person)
154   - person.vote_for(article)
155   - end
156   - for i in 0..5
157   - person = fast_create(Person)
158   - person.vote_against(article)
159   - end
160   -
161   - article = fast_create(Article, {:name=>'2 votes against'})
162   - for i in 0..2
163   - person = fast_create(Person)
164   - person.vote_against(article)
165   - end
166   -
167   - article = fast_create(Article, {:name=>'7 votes for'})
168   - for i in 0..7
169   - person = fast_create(Person)
170   - person.vote_for(article)
171   - end
172   -
173   - articles = Article.more_positive_votes(Environment.default, 5)
174   - assert_equal '7 votes for', articles.first.name
175   - assert_equal '23 votes for 20 votes against', articles.last.name
176   - end
177   -
178   - should 'list the most negative' do
179   -
180   - article = fast_create(Article, {:name=>'23 votes for 29 votes against'})
181   - for i in 0..29
182   - person = fast_create(Person)
183   - person.vote_against(article)
184   - end
185   - for i in 0..23
186   - person = fast_create(Person)
187   - person.vote_for(article)
188   - end
189   -
190   - article = fast_create(Article, {:name=>'10 votes for 15 votes against'})
191   - for i in 0..10
192   - person = fast_create(Person)
193   - person.vote_for(article)
194   - end
195   - for i in 0..15
196   - person = fast_create(Person)
197   - person.vote_against(article)
198   - end
199   -
200   - article = fast_create(Article, {:name=>'2 votes against'})
201   - for i in 0..2
202   - person = fast_create(Person)
203   - person.vote_against(article)
204   - end
205   -
206   - article = fast_create(Article, {:name=>'7 votes for'})
207   - for i in 0..7
208   - person = fast_create(Person)
209   - person.vote_for(article)
210   - end
211   -
212   - articles = Article.more_negative_votes(Environment.default, 5)
213   - assert_equal '23 votes for 29 votes against', articles.first.name
214   - assert_equal '2 votes against', articles.last.name
215   - end
216   -
217 47 end
... ...
plugins/relevant_content/views/box_organizer/relevant_content_plugin/_relevant_content_block.rhtml
... ... @@ -5,8 +5,4 @@
5 5 <%= labelled_check_box _('Display most liked content'), "block[show_most_liked]", 1 ,@block.show_most_liked != 0 %><BR>
6 6 <%= labelled_check_box _('Display most voted content'), "block[show_most_voted]", 1 ,@block.show_most_voted != 0 %><BR>
7 7 <%= labelled_check_box _('Display most disliked content'), "block[show_most_disliked]", 1 , @block.show_most_disliked != 0 %><BR>
8   -</div>
9   -
10   -
11   -
12   -
  8 +</div>
13 9 \ No newline at end of file
... ...