diff --git a/plugins/relevant_content/lib/ext/article.rb b/plugins/relevant_content/lib/ext/article.rb index 9a5e3d1..9872866 100644 --- a/plugins/relevant_content/lib/ext/article.rb +++ b/plugins/relevant_content/lib/ext/article.rb @@ -1,194 +1,194 @@ 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"] def self.most_accessed(owner, limit = nil) - if owner.kind_of?(Environment) + if owner.kind_of?(Environment) result = Article.relevant_content.find( :all, :order => 'hits desc', :limit => limit, - :conditions => ["hits > 0"] + :conditions => ["hits > 0"] ) result.paginate({:page => 1, :per_page => limit}) else #Owner is a profile result = Article.relevant_content.find( - :all, + :all, :order => 'hits desc', :limit => limit, - :conditions => ["profile_id = ? and hits > 0", owner.id] + :conditions => ["profile_id = ? and hits > 0", owner.id] ) result.paginate({:page => 1, :per_page => limit}) - end + end end def self.most_commented_relevant_content(owner, limit) - - if owner.kind_of?(Environment) + + if owner.kind_of?(Environment) result = Article.relevant_content.find( :all, :order => 'comments_count desc', :limit => limit, - :conditions => ["comments_count > 0"] + :conditions => ["comments_count > 0"] ) result.paginate({:page => 1, :per_page => limit}) else #Owner is a profile result = Article.relevant_content.find( - :all, + :all, :order => 'comments_count desc', :limit => limit, - :conditions => ["profile_id = ? and comments_count > 0", owner.id] + :conditions => ["profile_id = ? and comments_count > 0", owner.id] ) result.paginate({:page => 1, :per_page => limit}) - end - end - + 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) + if owner.kind_of?(Environment) result = Article.find( :all, - :select => articles_columns, + :select => articles_columns, :order => 'sum(vote) desc', :group => 'voteable_id, ' + articles_columns, :limit => limit, - :having => ['sum(vote) > 0'], - :conditions => {'votes.voteable_type' => 'Article'}, + :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( :all, - :select => articles_columns, + :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'] + :having => ['sum(vote) > 0'], + :conditions => ["profile_id = ? and votes.voteable_type = ? ", owner.id, 'Article'] ) result.paginate({:page => 1, :per_page => limit}) end end - + def self.more_negative_votes(owner, limit = nil) - if owner.kind_of?(Environment) + if owner.kind_of?(Environment) result = Article.find( :all, - :select => articles_columns, + :select => articles_columns, :order => 'sum(vote) asc', :group => 'voteable_id, ' + articles_columns, :limit => limit, - :having => ['sum(vote) < 0'], + :having => ['sum(vote) < 0'], :conditions => {'votes.voteable_type' => 'Article'}, - :joins => 'INNER JOIN votes ON articles.id = votes.voteable_id' + :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, + :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] + :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) + if owner.kind_of?(Environment) result = Article.find( :all, - :select => articles_columns, + :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( :all, - :select => articles_columns, + :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 => ["votes.voteable_type = 'Article' and vote > 0 and profile_id = ? ", owner.id] ) result.paginate({:page => 1, :per_page => limit}) end end - + def self.most_disliked(owner, limit = nil) - if owner.kind_of?(Environment) + if owner.kind_of?(Environment) 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"] + :conditions => ["votes.voteable_type = 'Article' and vote < 0"] ) result.paginate({:page => 1, :per_page => limit}) else #Owner is a profile result = Article.find( - :all, + :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] + :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) + if owner.kind_of?(Environment) result = Article.find( :all, - :select => articles_columns, + :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( :all, - :select => articles_columns, + :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 => ["votes.voteable_type = 'Article' and profile_id = ? ", owner.id] ) result.paginate({:page => 1, :per_page => limit}) end end - - - - -end \ No newline at end of file + + + + +end \ No newline at end of file diff --git a/plugins/relevant_content/lib/relevant_content_plugin.rb b/plugins/relevant_content/lib/relevant_content_plugin.rb index 012766c..f12d987 100644 --- a/plugins/relevant_content/lib/relevant_content_plugin.rb +++ b/plugins/relevant_content/lib/relevant_content_plugin.rb @@ -1,5 +1,5 @@ class RelevantContentPlugin < Noosfero::Plugin - + def self.plugin_name "Relevant Content Plugin" end @@ -16,6 +16,6 @@ class RelevantContentPlugin < Noosfero::Plugin def stylesheet? true - end + end end diff --git a/plugins/relevant_content/lib/relevant_content_plugin/relevant_content_block.rb b/plugins/relevant_content/lib/relevant_content_plugin/relevant_content_block.rb index b076e56..ffa5055 100644 --- a/plugins/relevant_content/lib/relevant_content_plugin/relevant_content_block.rb +++ b/plugins/relevant_content/lib/relevant_content_plugin/relevant_content_block.rb @@ -12,80 +12,80 @@ class RelevantContentPlugin::RelevantContentBlock < Block end settings_items :limit, :type => :integer, :default => 5 - settings_items :show_most_read, :type => :integer, :default => 1 - settings_items :show_most_commented, :type => :integer, :default => 1 - settings_items :show_most_liked, :type => :integer, :default => 1 - settings_items :show_most_disliked, :type => :integer, :default => 0 - settings_items :show_most_voted, :type => :integer, :default => 1 + settings_items :show_most_read, :type => :integer, :default => 1 + settings_items :show_most_commented, :type => :integer, :default => 1 + settings_items :show_most_liked, :type => :integer, :default => 1 + settings_items :show_most_disliked, :type => :integer, :default => 0 + settings_items :show_most_voted, :type => :integer, :default => 1 include ActionController::UrlWriter def content(args={}) - content = block_title(title) - + content = block_title(title) + if self.show_most_read != 0 - docs = Article.most_accessed(owner, self.limit) - if !docs.blank? + docs = Article.most_accessed(owner, self.limit) + if !docs.blank? subcontent = "" - subcontent += content_tag(:span, _("Most read articles"), :class=>"title mread") + "\n" - subcontent += content_tag(:ul, docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) - content += content_tag(:div, subcontent, :class=>"block mread") + "\n" - end + subcontent += content_tag(:span, _("Most read articles"), :class=>"title mread") + "\n" + subcontent += content_tag(:ul, docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) + content += content_tag(:div, subcontent, :class=>"block mread") + "\n" + end end if self.show_most_commented != 0 docs = Article.most_commented_relevant_content(owner, self.limit) - if !docs.blank? - subcontent = "" - subcontent += content_tag(:span, _("Most commented articles"), :class=>"title mcommented") + "\n" - subcontent += content_tag(:ul, docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) + if !docs.blank? + subcontent = "" + subcontent += content_tag(:span, _("Most commented articles"), :class=>"title mcommented") + "\n" + subcontent += content_tag(:ul, docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) content += content_tag(:div, subcontent, :class=>"block mcommented") + "\n" end end - if owner.kind_of?(Environment) + if owner.kind_of?(Environment) env = owner else env = owner.environment end - + if env.plugin_enabled?(VotePlugin) if self.show_most_liked != 0 docs = Article.more_positive_votes(owner, self.limit) - if !docs.blank? - subcontent = "" - subcontent += content_tag(:span, _("Most liked articles"), :class=>"title mliked") + "\n" - subcontent += content_tag(:ul, docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) + if !docs.blank? + subcontent = "" + subcontent += content_tag(:span, _("Most liked articles"), :class=>"title mliked") + "\n" + subcontent += content_tag(:ul, docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) content += content_tag(:div, subcontent, :class=>"block mliked") + "\n" end end if self.show_most_disliked != 0 docs = Article.more_negative_votes(owner, self.limit) - if !docs.blank? - subcontent = "" - subcontent += content_tag(:span, _("Most disliked articles"), :class=>"title mdisliked") + "\n" - subcontent += content_tag(:ul, docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) - content += content_tag(:div, subcontent, :class=>"block mdisliked") + "\n" + if !docs.blank? + subcontent = "" + subcontent += content_tag(:span, _("Most disliked articles"), :class=>"title mdisliked") + "\n" + subcontent += content_tag(:ul, docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) + content += content_tag(:div, subcontent, :class=>"block mdisliked") + "\n" end end - + if self.show_most_voted != 0 docs = Article.most_voted(owner, self.limit) - if !docs.blank? + if !docs.blank? subcontent = "" - subcontent += content_tag(:span, _("Most voted articles"), :class=>"title mvoted") + "\n" - subcontent += content_tag(:ul, docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) - content += content_tag(:div, subcontent, :class=>"block mvoted") + "\n" + subcontent += content_tag(:span, _("Most voted articles"), :class=>"title mvoted") + "\n" + subcontent += content_tag(:ul, docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) + content += content_tag(:div, subcontent, :class=>"block mvoted") + "\n" end - end + end end return content - end - + end + def timeout 4.hours - end - + end + def self.expire_on { :profile => [:article], :environment => [:article] } end 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 94adef4..e651478 100644 --- a/plugins/relevant_content/test/unit/relevant_content_block_test.rb +++ b/plugins/relevant_content/test/unit/relevant_content_block_test.rb @@ -5,10 +5,10 @@ require 'comment_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 @@ -18,15 +18,15 @@ class RelevantContentBlockTest < ActiveSupport::TestCase @environment = @profile.environment end attr_reader :profile, :environment - - - + + + should 'have a default title' do relevant_content_block = RelevantContentPlugin::RelevantContentBlock.new block = Block.new assert_not_equal block.default_title, relevant_content_block.default_title end - + should 'have a help tooltip' do relevant_content_block = RelevantContentPlugin::RelevantContentBlock.new block = Block.new @@ -36,7 +36,7 @@ class RelevantContentBlockTest < ActiveSupport::TestCase should 'describe itself' do assert_not_equal Block.description, RelevantContentPlugin::RelevantContentBlock.description end - + should 'is editable' do block = RelevantContentPlugin::RelevantContentBlock.new assert block.editable? @@ -51,19 +51,19 @@ class RelevantContentBlockTest < ActiveSupport::TestCase 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 + begin Environment.default.enable_plugin(:vote) rescue - puts "Unable to activate vote plugin" - end + puts "Unable to activate vote plugin" + end if Environment.default.plugin_enabled?(:vote) assert_nothing_raised{ Article.most_liked(Environment.default, 5) @@ -72,11 +72,11 @@ class RelevantContentBlockTest < ActiveSupport::TestCase end should 'not raise an exception when finding the most disliked content' do - begin + begin Environment.default.enable_plugin(:vote) rescue puts "Unable to activate vote plugin" - end + end if Environment.default.plugin_enabled?(:vote) assert_nothing_raised{ Article.most_disliked(Environment.default, 5) @@ -84,13 +84,13 @@ class RelevantContentBlockTest < ActiveSupport::TestCase end end - + should 'not raise an exception when finding the more positive votes' do - begin + begin Environment.default.enable_plugin(:vote) rescue - puts "Unable to activate vote plugin" - end + puts "Unable to activate vote plugin" + end if Environment.default.plugin_enabled?(:vote) assert_nothing_raised{ Article.more_positive_votes(Environment.default, 5) @@ -99,18 +99,18 @@ class RelevantContentBlockTest < ActiveSupport::TestCase end should 'not raise an exception when finding the most voted' do - begin + begin Environment.default.enable_plugin(:vote) rescue puts "Unable to activate vote plugin" - end + 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'}) @@ -118,19 +118,19 @@ class RelevantContentBlockTest < ActiveSupport::TestCase 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 @@ -147,7 +147,7 @@ class RelevantContentBlockTest < ActiveSupport::TestCase 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) @@ -157,7 +157,7 @@ class RelevantContentBlockTest < ActiveSupport::TestCase 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) @@ -169,12 +169,12 @@ class RelevantContentBlockTest < ActiveSupport::TestCase 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'}) @@ -186,7 +186,7 @@ class RelevantContentBlockTest < ActiveSupport::TestCase 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) @@ -196,7 +196,7 @@ class RelevantContentBlockTest < ActiveSupport::TestCase 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) @@ -208,10 +208,10 @@ class RelevantContentBlockTest < ActiveSupport::TestCase 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 b458b1a..3d965a3 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 @@ -1,12 +1,12 @@
- <%= labelled_form_field _('Limit of items per category'), text_field(:block, :limit, :size => 3) %> + <%= labelled_form_field _('Limit of items per category'), text_field(:block, :limit, :size => 3) %> <%= labelled_check_box _('Display most accessed content'), "block[show_most_read]", 1 ,@block.show_most_read != 0 %>
<%= labelled_check_box _('Display most commented content'), "block[show_most_commented]", 1 ,@block.show_most_commented != 0 %>
- <%= 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 %>
+ <%= 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 %>
- + -- libgit2 0.21.2