Commit 99aaed18bdc052ee229642711fa9b6feca322a3e

Authored by Rafael Reggiani Manzo
1 parent 3f8c863c

Refactor RelevantContentBlock#content into views

Removes the html generation coupled into the class taking advantage of
BoxesHelper structure
plugins/relevant_content/lib/relevant_content_plugin/relevant_content_block.rb
... ... @@ -20,45 +20,8 @@ class RelevantContentPlugin::RelevantContentBlock < Block
20 20  
21 21 attr_accessible :limit, :show_most_voted, :show_most_disliked, :show_most_liked, :show_most_commented, :show_most_read
22 22  
23   - include ActionView::Helpers
24   - include Rails.application.routes.url_helpers
25   -
26   - def content(args={})
27   -
28   - content = block_title(title, subtitle)
29   -
30   - if self.show_most_read
31   - docs = Article.most_accessed(owner, self.limit)
32   - content += subcontent(docs, _("Most read articles"), "mread") unless docs.blank?
33   - end
34   -
35   - if self.show_most_commented
36   - docs = Article.most_commented_relevant_content(owner, self.limit)
37   - content += subcontent(docs, _("Most commented articles"), "mcommented") unless docs.blank?
38   - end
39   -
40   - if owner.kind_of?(Environment)
41   - env = owner
42   - else
43   - env = owner.environment
44   - end
45   -
46   - if env.plugin_enabled?('VotePlugin')
47   - if self.show_most_liked
48   - docs = Article.more_positive_votes(owner, self.limit)
49   - content += subcontent(docs, _("Most liked articles"), "mliked") unless docs.blank?
50   - end
51   - if self.show_most_disliked
52   - docs = Article.more_negative_votes(owner, self.limit)
53   - content += subcontent(docs, _("Most disliked articles"), "mdisliked") unless docs.blank?
54   - end
55   -
56   - if self.show_most_voted
57   - docs = Article.most_voted(owner, self.limit)
58   - content += subcontent(docs, _("Most voted articles"), "mvoted") unless docs.blank?
59   - end
60   - end
61   - return content.html_safe
  23 + def env
  24 + owner.kind_of?(Environment) ? owner : owner.environment
62 25 end
63 26  
64 27 def timeout
... ... @@ -69,14 +32,4 @@ class RelevantContentPlugin::RelevantContentBlock < Block
69 32 { :profile => [:article], :environment => [:article] }
70 33 end
71 34  
72   - protected
73   -
74   - def subcontent(docs, title, html_class)
75   - subcontent = safe_join([
76   - content_tag(:span, title, class: "title #{html_class}"),
77   - content_tag(:ul, safe_join(docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}, "\n"))
78   - ], "\n")
79   - content_tag(:div, subcontent, :class=>"block #{html_class}")
80   - end
81   -
82 35 end
... ...
plugins/relevant_content/test/unit/relevant_content_block_test.rb
... ... @@ -42,20 +42,6 @@ class RelevantContentBlockTest < ActiveSupport::TestCase
42 42 assert_equal RelevantContentPlugin::RelevantContentBlock.expire_on, {:environment=>[:article], :profile=>[:article]}
43 43 end
44 44  
45   - should 'not crash if vote plugin is not found' do
46   - box = fast_create(Box, :owner_id => @profile.id, :owner_type => 'Profile')
47   - block = RelevantContentPlugin::RelevantContentBlock.new(:box => box)
48   -
49   - Environment.any_instance.stubs(:enabled_plugins).returns(['RelevantContent'])
50   - # When the plugin is disabled from noosfero instance, its constant name is
51   - # undefined. To test this case, I have to manually undefine the constant
52   - # if necessary.
53   - Object.send(:remove_const, VotePlugin.to_s) if defined? VotePlugin
54   -
55   - assert_nothing_raised do
56   - block.content
57   - end
58   - end
59 45  
60 46 should 'check most voted articles from profile with relevant content block' do
61 47 community = fast_create(Community)
... ... @@ -77,11 +63,41 @@ class RelevantContentBlockTest < ActiveSupport::TestCase
77 63 assert_equal false, data.empty?
78 64 end
79 65  
  66 +end
  67 +
  68 +require 'boxes_helper'
  69 +
  70 +class RelevantContentBlockViewTest < ActionView::TestCase
  71 + include BoxesHelper
  72 +
  73 + def setup
  74 + @profile = create_user('testinguser').person
  75 + end
  76 +
  77 + should 'not crash if vote plugin is not found' do
  78 + box = fast_create(Box, :owner_id => @profile.id, :owner_type => 'Profile')
  79 + block = RelevantContentPlugin::RelevantContentBlock.new(:box => box)
  80 +
  81 + Environment.any_instance.stubs(:enabled_plugins).returns(['RelevantContent'])
  82 + ActionView::Base.any_instance.expects(:block_title).returns("")
  83 + # When the plugin is disabled from noosfero instance, its constant name is
  84 + # undefined. To test this case, I have to manually undefine the constant
  85 + # if necessary.
  86 + Object.send(:remove_const, VotePlugin.to_s) if defined? VotePlugin
  87 +
  88 + assert_nothing_raised do
  89 + render_block_content(block)
  90 + end
  91 + end
  92 +
80 93 should 'not escape html in block content' do
81   - fast_create(Article, profile_id: profile.id, hits: 10)
82   - box = fast_create(Box, :owner_id => profile.id, :owner_type => 'Profile')
  94 + fast_create(Article, profile_id: @profile.id, hits: 10)
  95 + box = fast_create(Box, :owner_id => @profile.id, :owner_type => 'Profile')
83 96 block = RelevantContentPlugin::RelevantContentBlock.new(:box => box)
  97 +
84 98 Environment.any_instance.stubs(:enabled_plugins).returns(['RelevantContent'])
85   - assert_tag_in_string block.content, tag: 'span', attributes: { class: 'title mread' }
  99 + ActionView::Base.any_instance.expects(:block_title).returns("")
  100 +
  101 + assert_tag_in_string render_block_content(block), tag: 'span', attributes: { class: 'title mread' }
86 102 end
87 103 end
... ...
plugins/relevant_content/views/blocks/_doc.slim 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +li
  2 + = link_to(h(doc.title), doc.url)
... ...
plugins/relevant_content/views/blocks/_subcontent.slim 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +- unless docs.blank?
  2 + div class="block #{html_class}"
  3 + span class="title #{html_class}"
  4 + = title
  5 + ul
  6 + = render partial: 'blocks/doc', collection: docs
... ...
plugins/relevant_content/views/blocks/relevant_content.slim 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 += block_title(block.title, block.subtitle)
  2 +
  3 +- if block.show_most_read
  4 + = render partial: 'blocks/subcontent', locals: {docs: Article.most_accessed(block.owner, block.limit), title: _("Most read articles"), html_class: 'mread'}
  5 +- if block.show_most_commented
  6 + = render partial: 'blocks/subcontent', locals: {docs: Article.most_commented_relevant_content(block.owner, block.limit), title: _("Most commented articles"), html_class: 'mcommented'}
  7 +
  8 +- if block.env.plugin_enabled?('VotePlugin')
  9 + - if block.show_most_liked
  10 + = render partial: 'blocks/subcontent', locals: {docs: Article.more_positive_votes(block.owner, block.limit), title: _("Most liked articles"), html_class: 'mliked'}
  11 + - if block.show_most_disliked
  12 + = render partial: 'blocks/subcontent', locals: {docs: Article.more_negative_votes(block.owner, block.limit), title: _("Most disliked articles"), html_class: 'mdisliked'}
  13 + - if block.show_most_voted
  14 + = render partial: 'blocks/subcontent', locals: {docs: Article.most_voted(block.owner, block.limit), title: _("Most voted articles"), html_class: 'mvoted'}
... ...