diff --git a/plugins/relevant_content/lib/ext/article.rb b/plugins/relevant_content/lib/ext/article.rb index 5cb56e0..580e78d 100644 --- a/plugins/relevant_content/lib/ext/article.rb +++ b/plugins/relevant_content/lib/ext/article.rb @@ -2,7 +2,11 @@ require_dependency 'article' class Article - named_scope :relevant_content, :conditions => ["(articles.type != 'UploadedFile' and articles.type != 'Blog' and articles.type != 'RssFeed') OR articles.type is NULL"] + 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"] + + def self.articles_columns + Article.column_names.map {|c| "articles.#{c}"} .join(",") + end def self.most_accessed(owner, limit = nil) conditions = owner.kind_of?(Environment) ? ["hits > 0"] : ["profile_id = ? and hits > 0", owner.id] @@ -15,169 +19,77 @@ class Article end def self.most_commented_relevant_content(owner, limit) - - if owner.kind_of?(Environment) + conditions = owner.kind_of?(Environment) ? ["comments_count > 0"] : ["profile_id = ? and comments_count > 0", owner.id] result = Article.relevant_content.find( :all, :order => 'comments_count desc', :limit => limit, - :conditions => ["comments_count > 0"] - ) + :conditions => conditions) result.paginate({:page => 1, :per_page => limit}) - else - #Owner is a profile - result = Article.relevant_content.find( - :all, - :order => 'comments_count desc', - :limit => limit, - :conditions => ["profile_id = ? and comments_count > 0", owner.id] - ) - result.paginate({:page => 1, :per_page => limit}) - end - end - - def self.articles_columns - Article.column_names.map {|c| "articles.#{c}"} .join(",") end def self.more_positive_votes(owner, limit = nil) - if owner.kind_of?(Environment) - result = Article.find( - :all, - :select => articles_columns, - :order => 'sum(vote) desc', - :group => 'voteable_id, ' + articles_columns, - :limit => limit, - :having => ['sum(vote) > 0'], - :conditions => {'votes.voteable_type' => 'Article'}, - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id' - ) - result.paginate({:page => 1, :per_page => limit}) - else - #Owner is a profile - result = Article.find( + conditions = owner.kind_of?(Environment) ? {'votes.voteable_type' => 'Article'} : ["profile_id = ? and votes.voteable_type = ? ", owner.id, 'Article'] + result = Article.relevant_content.find( :all, - :select => articles_columns, :order => 'sum(vote) desc', :group => 'voteable_id, ' + articles_columns, :limit => limit, - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id', :having => ['sum(vote) > 0'], - :conditions => ["profile_id = ? and votes.voteable_type = ? ", owner.id, 'Article'] - ) + :conditions => conditions, + :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id') result.paginate({:page => 1, :per_page => limit}) - end end def self.more_negative_votes(owner, limit = nil) - if owner.kind_of?(Environment) - result = Article.find( + conditions = owner.kind_of?(Environment) ? {'votes.voteable_type' => 'Article'} : ["profile_id = ? and votes.voteable_type = 'Article' ", owner.id] + result = Article.relevant_content.find( :all, - :select => articles_columns, :order => 'sum(vote) asc', :group => 'voteable_id, ' + articles_columns, :limit => limit, :having => ['sum(vote) < 0'], - :conditions => {'votes.voteable_type' => 'Article'}, + :conditions => conditions, :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id' ) - result.paginate({:page => 1, :per_page => limit}) - else - #Owner is a profile - result = Article.find( - :all, - :select => articles_columns, - :order => 'sum(vote) asc', - :group => 'voteable_id, ' + articles_columns, - :limit => limit, - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id', - :having => ['sum(vote) < 0'], - :conditions => ["profile_id = ? and votes.voteable_type = 'Article' ", owner.id] - ) result.paginate({:page => 1, :per_page => limit}) - end end def self.most_liked(owner, limit = nil) - if owner.kind_of?(Environment) - result = Article.find( - :all, - :select => articles_columns, - :order => 'count(voteable_id) desc', - :group => 'voteable_id, ' + articles_columns, - :limit => limit, - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id', - :conditions => ["votes.voteable_type = 'Article' and vote > 0"] - ) - result.paginate({:page => 1, :per_page => limit}) - else - #Owner is a profile - result = Article.find( + conditions = owner.kind_of?(Environment) ? ["votes.voteable_type = 'Article' and vote > 0"] : ["votes.voteable_type = 'Article' and vote > 0 and profile_id = ? ", owner.id] + result = Article.relevant_content.find( :all, :select => articles_columns, :order => 'count(voteable_id) desc', :group => 'voteable_id, ' + articles_columns, :limit => limit, - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id', - :conditions => ["votes.voteable_type = 'Article' and vote > 0 and profile_id = ? ", owner.id] - ) + :conditions => conditions, + :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id') result.paginate({:page => 1, :per_page => limit}) - end end def self.most_disliked(owner, limit = nil) - if owner.kind_of?(Environment) - result = Article.find( + conditions = owner.kind_of?(Environment) ? ["votes.voteable_type = 'Article' and vote < 0"] : ["votes.voteable_type = 'Article' and vote < 0 and profile_id = ? ", owner.id] + result = Article.relevant_content.find( :all, :order => 'count(voteable_id) desc', :group => 'voteable_id, ' + articles_columns, :limit => limit, - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id', - :conditions => ["votes.voteable_type = 'Article' and vote < 0"] - ) + :conditions => conditions, + :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id') result.paginate({:page => 1, :per_page => limit}) - else - #Owner is a profile - result = Article.find( - :all, - :order => 'count(voteable_id) desc', - :group => 'voteable_id, ' + articles_columns, - :limit => limit, - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id', - :conditions => ["votes.voteable_type = 'Article' and vote < 0 and profile_id = ? ", owner.id] - ) - result.paginate({:page => 1, :per_page => limit}) - end end def self.most_voted(owner, limit = nil) - if owner.kind_of?(Environment) - result = Article.find( - :all, - :select => articles_columns, - :order => 'count(voteable_id) desc', - :group => 'voteable_id, ' + articles_columns, - :limit => limit, - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id', - :conditions => ["votes.voteable_type = 'Article'"] - ) - result.paginate({:page => 1, :per_page => limit}) - else - #Owner is a profile - result = Article.find( + conditions = owner.kind_of?(Environment) ? ["votes.voteable_type = 'Article'"] : ["votes.voteable_type = 'Article' and profile_id = ? ", owner.id] + result = Article.relevant_content.find( :all, :select => articles_columns, :order => 'count(voteable_id) desc', :group => 'voteable_id, ' + articles_columns, :limit => limit, - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id', - :conditions => ["votes.voteable_type = 'Article' and profile_id = ? ", owner.id] - ) + :conditions => conditions, + :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id') result.paginate({:page => 1, :per_page => limit}) - end end - - - - end diff --git a/plugins/relevant_content/test/unit/article.rb b/plugins/relevant_content/test/unit/article.rb new file mode 100644 index 0000000..d986717 --- /dev/null +++ b/plugins/relevant_content/test/unit/article.rb @@ -0,0 +1,148 @@ +require File.dirname(__FILE__) + '/../test_helper' + +require 'comment_controller' +# Re-raise errors caught by the controller. +class CommentController; def rescue_action(e) raise e end; end + +class RelevantContentBlockTest < ActiveSupport::TestCase + + include AuthenticatedTestHelper + fixtures :users, :environments + + def setup + @controller = CommentController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @profile = create_user('testinguser').person + @environment = @profile.environment + end + attr_reader :profile, :environment + + def enable_vote_plugin + enabled = false + environment=Environment.default + if Noosfero::Plugin.all.include?('VotePlugin') + if not environment.enabled_plugins.include?(:vote) + environment.enable_plugin(Vote) + environment.save! + end + enabled = true + end + enabled + end + + should 'list most commented articles' do + Article.delete_all + a1 = create(TextileArticle, :name => "art 1", :profile_id => profile.id) + a2 = create(TextileArticle, :name => "art 2", :profile_id => profile.id) + a3 = create(TextileArticle, :name => "art 3", :profile_id => profile.id) + + 2.times { Comment.create(:title => 'test', :body => 'asdsad', :author => profile, :source => a2).save! } + 4.times { Comment.create(:title => 'test', :body => 'asdsad', :author => profile, :source => a3).save! } + + # should respect the order (more commented comes first) + assert_equal a3.name, profile.articles.most_commented_relevant_content(Environment.default, 3).first.name + # It is a2 instead of a1 since it does not list articles without comments + assert_equal a2.name, profile.articles.most_commented_relevant_content(Environment.default, 3).last.name + end + + + should 'find the most voted' do + if not enable_vote_plugin + return + end + article = fast_create(Article, {:name=>'2 votes'}) + 2.times{ + person = fast_create(Person) + person.vote_for(article) + } + article = fast_create(Article, {:name=>'10 votes'}) + 10.times{ + person = fast_create(Person) + person.vote_for(article) + } + article = fast_create(Article, {:name=>'5 votes'}) + 5.times{ + person = fast_create(Person) + person.vote_for(article) + } + articles = Article.most_voted(Environment.default, 5) + assert_equal '10 votes', articles.first.name + assert_equal '2 votes', articles.last.name + end + + should 'list the most postive' do + if not enable_vote_plugin + return + end + article = fast_create(Article, {:name=>'23 votes for 20 votes against'}) + 20.times{ + person = fast_create(Person) + person.vote_against(article) + } + 23.times{ + person = fast_create(Person) + person.vote_for(article) + } + article = fast_create(Article, {:name=>'10 votes for 5 votes against'}) + 10.times{ + person = fast_create(Person) + person.vote_for(article) + } + 5.times{ + person = fast_create(Person) + person.vote_against(article) + } + article = fast_create(Article, {:name=>'2 votes against'}) + 2.times{ + person = fast_create(Person) + person.vote_against(article) + } + + article = fast_create(Article, {:name=>'7 votes for'}) + 7.times{ + person = fast_create(Person) + person.vote_for(article) + } + articles = Article.more_positive_votes(Environment.default, 5) + assert_equal '7 votes for', articles.first.name + assert_equal '23 votes for 20 votes against', articles.last.name + end + + should 'list the most negative' do + if not enable_vote_plugin + return + end + article = fast_create(Article, {:name=>'23 votes for 29 votes against'}) + 29.times{ + person = fast_create(Person) + person.vote_against(article) + } + 23.times{ + person = fast_create(Person) + person.vote_for(article) + } + article = fast_create(Article, {:name=>'10 votes for 15 votes against'}) + 10.times{ + person = fast_create(Person) + person.vote_for(article) + } + 15.times{ + person = fast_create(Person) + person.vote_against(article) + } + article = fast_create(Article, {:name=>'2 votes against'}) + 2.times{ + person = fast_create(Person) + person.vote_against(article) + } + article = fast_create(Article, {:name=>'7 votes for'}) + 7.times{ + person = fast_create(Person) + person.vote_for(article) + } + articles = Article.more_negative_votes(Environment.default, 5) + assert_equal '23 votes for 29 votes against', articles.first.name + assert_equal '2 votes against', articles.last.name + end +end \ No newline at end of file diff --git a/plugins/relevant_content/test/unit/relevant_content_block_test.rb b/plugins/relevant_content/test/unit/relevant_content_block_test.rb index e651478..7823c05 100644 --- a/plugins/relevant_content/test/unit/relevant_content_block_test.rb +++ b/plugins/relevant_content/test/unit/relevant_content_block_test.rb @@ -19,8 +19,6 @@ class RelevantContentBlockTest < ActiveSupport::TestCase end attr_reader :profile, :environment - - should 'have a default title' do relevant_content_block = RelevantContentPlugin::RelevantContentBlock.new block = Block.new @@ -46,172 +44,4 @@ class RelevantContentBlockTest < ActiveSupport::TestCase assert_equal RelevantContentPlugin::RelevantContentBlock.expire_on, {:environment=>[:article], :profile=>[:article]} end - should 'not raise an exception when finding the most accessed content' do - assert_nothing_raised{ - Article.most_accessed(Environment.default, 5) - } - end - - should 'not raise an exception when finding the most commented content' do - assert_nothing_raised{ - Article.most_commented_relevant_content(Environment.default, 5) - } - end - - should 'not raise an exception when finding the most liked content' do - begin - Environment.default.enable_plugin(:vote) - rescue - puts "Unable to activate vote plugin" - end - if Environment.default.plugin_enabled?(:vote) - assert_nothing_raised{ - Article.most_liked(Environment.default, 5) - } - end - end - - should 'not raise an exception when finding the most disliked content' do - begin - Environment.default.enable_plugin(:vote) - rescue - puts "Unable to activate vote plugin" - end - if Environment.default.plugin_enabled?(:vote) - assert_nothing_raised{ - Article.most_disliked(Environment.default, 5) - } - end - end - - - should 'not raise an exception when finding the more positive votes' do - begin - Environment.default.enable_plugin(:vote) - rescue - puts "Unable to activate vote plugin" - end - if Environment.default.plugin_enabled?(:vote) - assert_nothing_raised{ - Article.more_positive_votes(Environment.default, 5) - } - end - end - - should 'not raise an exception when finding the most voted' do - begin - Environment.default.enable_plugin(:vote) - rescue - puts "Unable to activate vote plugin" - end - if Environment.default.plugin_enabled?(:vote) - assert_nothing_raised{ - Article.most_voted(Environment.default, 5) - } - end - end - - should 'find the most voted' do - - article = fast_create(Article, {:name=>'2 votes'}) - for i in 0..2 - person = fast_create(Person) - person.vote_for(article) - end - - article = fast_create(Article, {:name=>'10 votes'}) - for i in 0..10 - person = fast_create(Person) - person.vote_for(article) - end - - article = fast_create(Article, {:name=>'5 votes'}) - for i in 0..5 - person = fast_create(Person) - person.vote_for(article) - end - - articles = Article.most_voted(Environment.default, 5) - assert_equal '10 votes', articles.first.name - assert_equal '2 votes', articles.last.name - end - - should 'list the most postive' do - - article = fast_create(Article, {:name=>'23 votes for 20 votes against'}) - for i in 0..20 - person = fast_create(Person) - person.vote_against(article) - end - for i in 0..23 - person = fast_create(Person) - person.vote_for(article) - end - - article = fast_create(Article, {:name=>'10 votes for 5 votes against'}) - for i in 0..10 - person = fast_create(Person) - person.vote_for(article) - end - for i in 0..5 - person = fast_create(Person) - person.vote_against(article) - end - - article = fast_create(Article, {:name=>'2 votes against'}) - for i in 0..2 - person = fast_create(Person) - person.vote_against(article) - end - - article = fast_create(Article, {:name=>'7 votes for'}) - for i in 0..7 - person = fast_create(Person) - person.vote_for(article) - end - - articles = Article.more_positive_votes(Environment.default, 5) - assert_equal '7 votes for', articles.first.name - assert_equal '23 votes for 20 votes against', articles.last.name - end - - should 'list the most negative' do - - article = fast_create(Article, {:name=>'23 votes for 29 votes against'}) - for i in 0..29 - person = fast_create(Person) - person.vote_against(article) - end - for i in 0..23 - person = fast_create(Person) - person.vote_for(article) - end - - article = fast_create(Article, {:name=>'10 votes for 15 votes against'}) - for i in 0..10 - person = fast_create(Person) - person.vote_for(article) - end - for i in 0..15 - person = fast_create(Person) - person.vote_against(article) - end - - article = fast_create(Article, {:name=>'2 votes against'}) - for i in 0..2 - person = fast_create(Person) - person.vote_against(article) - end - - article = fast_create(Article, {:name=>'7 votes for'}) - for i in 0..7 - person = fast_create(Person) - person.vote_for(article) - end - - articles = Article.more_negative_votes(Environment.default, 5) - assert_equal '23 votes for 29 votes against', articles.first.name - assert_equal '2 votes against', articles.last.name - end - end diff --git a/plugins/relevant_content/views/box_organizer/relevant_content_plugin/_relevant_content_block.rhtml b/plugins/relevant_content/views/box_organizer/relevant_content_plugin/_relevant_content_block.rhtml index 3d965a3..b36f659 100644 --- a/plugins/relevant_content/views/box_organizer/relevant_content_plugin/_relevant_content_block.rhtml +++ b/plugins/relevant_content/views/box_organizer/relevant_content_plugin/_relevant_content_block.rhtml @@ -5,8 +5,4 @@ <%= labelled_check_box _('Display most liked content'), "block[show_most_liked]", 1 ,@block.show_most_liked != 0 %>
<%= labelled_check_box _('Display most voted content'), "block[show_most_voted]", 1 ,@block.show_most_voted != 0 %>
<%= labelled_check_box _('Display most disliked content'), "block[show_most_disliked]", 1 , @block.show_most_disliked != 0 %>
- - - - - + \ No newline at end of file -- libgit2 0.21.2