From 99aaed18bdc052ee229642711fa9b6feca322a3e Mon Sep 17 00:00:00 2001 From: Rafael Reggiani Manzo Date: Tue, 17 May 2016 14:24:58 -0300 Subject: [PATCH] Refactor RelevantContentBlock#content into views --- plugins/relevant_content/lib/relevant_content_plugin/relevant_content_block.rb | 51 ++------------------------------------------------- plugins/relevant_content/test/unit/relevant_content_block_test.rb | 50 +++++++++++++++++++++++++++++++++----------------- plugins/relevant_content/views/blocks/_doc.slim | 2 ++ plugins/relevant_content/views/blocks/_subcontent.slim | 6 ++++++ plugins/relevant_content/views/blocks/relevant_content.slim | 14 ++++++++++++++ 5 files changed, 57 insertions(+), 66 deletions(-) create mode 100644 plugins/relevant_content/views/blocks/_doc.slim create mode 100644 plugins/relevant_content/views/blocks/_subcontent.slim create mode 100644 plugins/relevant_content/views/blocks/relevant_content.slim 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 7380fe0..59f3de6 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 @@ -20,45 +20,8 @@ class RelevantContentPlugin::RelevantContentBlock < Block attr_accessible :limit, :show_most_voted, :show_most_disliked, :show_most_liked, :show_most_commented, :show_most_read - include ActionView::Helpers - include Rails.application.routes.url_helpers - - def content(args={}) - - content = block_title(title, subtitle) - - if self.show_most_read - docs = Article.most_accessed(owner, self.limit) - content += subcontent(docs, _("Most read articles"), "mread") unless docs.blank? - end - - if self.show_most_commented - docs = Article.most_commented_relevant_content(owner, self.limit) - content += subcontent(docs, _("Most commented articles"), "mcommented") unless docs.blank? - end - - if owner.kind_of?(Environment) - env = owner - else - env = owner.environment - end - - if env.plugin_enabled?('VotePlugin') - if self.show_most_liked - docs = Article.more_positive_votes(owner, self.limit) - content += subcontent(docs, _("Most liked articles"), "mliked") unless docs.blank? - end - if self.show_most_disliked - docs = Article.more_negative_votes(owner, self.limit) - content += subcontent(docs, _("Most disliked articles"), "mdisliked") unless docs.blank? - end - - if self.show_most_voted - docs = Article.most_voted(owner, self.limit) - content += subcontent(docs, _("Most voted articles"), "mvoted") unless docs.blank? - end - end - return content.html_safe + def env + owner.kind_of?(Environment) ? owner : owner.environment end def timeout @@ -69,14 +32,4 @@ class RelevantContentPlugin::RelevantContentBlock < Block { :profile => [:article], :environment => [:article] } end - protected - - def subcontent(docs, title, html_class) - subcontent = safe_join([ - content_tag(:span, title, class: "title #{html_class}"), - content_tag(:ul, safe_join(docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}, "\n")) - ], "\n") - content_tag(:div, subcontent, :class=>"block #{html_class}") - end - 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 dc93f38..b4bde7a 100644 --- a/plugins/relevant_content/test/unit/relevant_content_block_test.rb +++ b/plugins/relevant_content/test/unit/relevant_content_block_test.rb @@ -42,20 +42,6 @@ class RelevantContentBlockTest < ActiveSupport::TestCase assert_equal RelevantContentPlugin::RelevantContentBlock.expire_on, {:environment=>[:article], :profile=>[:article]} end - should 'not crash if vote plugin is not found' do - box = fast_create(Box, :owner_id => @profile.id, :owner_type => 'Profile') - block = RelevantContentPlugin::RelevantContentBlock.new(:box => box) - - Environment.any_instance.stubs(:enabled_plugins).returns(['RelevantContent']) - # When the plugin is disabled from noosfero instance, its constant name is - # undefined. To test this case, I have to manually undefine the constant - # if necessary. - Object.send(:remove_const, VotePlugin.to_s) if defined? VotePlugin - - assert_nothing_raised do - block.content - end - end should 'check most voted articles from profile with relevant content block' do community = fast_create(Community) @@ -77,11 +63,41 @@ class RelevantContentBlockTest < ActiveSupport::TestCase assert_equal false, data.empty? end +end + +require 'boxes_helper' + +class RelevantContentBlockViewTest < ActionView::TestCase + include BoxesHelper + + def setup + @profile = create_user('testinguser').person + end + + should 'not crash if vote plugin is not found' do + box = fast_create(Box, :owner_id => @profile.id, :owner_type => 'Profile') + block = RelevantContentPlugin::RelevantContentBlock.new(:box => box) + + Environment.any_instance.stubs(:enabled_plugins).returns(['RelevantContent']) + ActionView::Base.any_instance.expects(:block_title).returns("") + # When the plugin is disabled from noosfero instance, its constant name is + # undefined. To test this case, I have to manually undefine the constant + # if necessary. + Object.send(:remove_const, VotePlugin.to_s) if defined? VotePlugin + + assert_nothing_raised do + render_block_content(block) + end + end + should 'not escape html in block content' do - fast_create(Article, profile_id: profile.id, hits: 10) - box = fast_create(Box, :owner_id => profile.id, :owner_type => 'Profile') + fast_create(Article, profile_id: @profile.id, hits: 10) + box = fast_create(Box, :owner_id => @profile.id, :owner_type => 'Profile') block = RelevantContentPlugin::RelevantContentBlock.new(:box => box) + Environment.any_instance.stubs(:enabled_plugins).returns(['RelevantContent']) - assert_tag_in_string block.content, tag: 'span', attributes: { class: 'title mread' } + ActionView::Base.any_instance.expects(:block_title).returns("") + + assert_tag_in_string render_block_content(block), tag: 'span', attributes: { class: 'title mread' } end end diff --git a/plugins/relevant_content/views/blocks/_doc.slim b/plugins/relevant_content/views/blocks/_doc.slim new file mode 100644 index 0000000..07518a8 --- /dev/null +++ b/plugins/relevant_content/views/blocks/_doc.slim @@ -0,0 +1,2 @@ +li + = link_to(h(doc.title), doc.url) diff --git a/plugins/relevant_content/views/blocks/_subcontent.slim b/plugins/relevant_content/views/blocks/_subcontent.slim new file mode 100644 index 0000000..28b0470 --- /dev/null +++ b/plugins/relevant_content/views/blocks/_subcontent.slim @@ -0,0 +1,6 @@ +- unless docs.blank? + div class="block #{html_class}" + span class="title #{html_class}" + = title + ul + = render partial: 'blocks/doc', collection: docs diff --git a/plugins/relevant_content/views/blocks/relevant_content.slim b/plugins/relevant_content/views/blocks/relevant_content.slim new file mode 100644 index 0000000..3d42fb0 --- /dev/null +++ b/plugins/relevant_content/views/blocks/relevant_content.slim @@ -0,0 +1,14 @@ += block_title(block.title, block.subtitle) + +- if block.show_most_read + = render partial: 'blocks/subcontent', locals: {docs: Article.most_accessed(block.owner, block.limit), title: _("Most read articles"), html_class: 'mread'} +- if block.show_most_commented + = render partial: 'blocks/subcontent', locals: {docs: Article.most_commented_relevant_content(block.owner, block.limit), title: _("Most commented articles"), html_class: 'mcommented'} + +- if block.env.plugin_enabled?('VotePlugin') + - if block.show_most_liked + = render partial: 'blocks/subcontent', locals: {docs: Article.more_positive_votes(block.owner, block.limit), title: _("Most liked articles"), html_class: 'mliked'} + - if block.show_most_disliked + = render partial: 'blocks/subcontent', locals: {docs: Article.more_negative_votes(block.owner, block.limit), title: _("Most disliked articles"), html_class: 'mdisliked'} + - if block.show_most_voted + = render partial: 'blocks/subcontent', locals: {docs: Article.most_voted(block.owner, block.limit), title: _("Most voted articles"), html_class: 'mvoted'} -- libgit2 0.21.2