Commit 174e0852b442e4d20dbfa944e0f4b7ae4c3f5199

Authored by Leandro Santos
Committed by Victor Costa
1 parent d8b7fec7

comment_paragraph: new block that display discussions

plugins/comment_paragraph/lib/comment_paragraph_plugin.rb
... ... @@ -59,6 +59,12 @@ class CommentParagraphPlugin < Noosfero::Plugin
59 59 [CommentParagraphPlugin::API]
60 60 end
61 61  
  62 + def self.extra_blocks
  63 + {
  64 + CommentParagraphPlugin::DiscussionBlock => {:position => ['1','2','3'] }
  65 + }
  66 + end
  67 +
62 68 def content_types
63 69 [CommentParagraphPlugin::Discussion]
64 70 end
... ...
plugins/comment_paragraph/lib/comment_paragraph_plugin/discussion_block.rb 0 → 100644
... ... @@ -0,0 +1,41 @@
  1 +class CommentParagraphPlugin::DiscussionBlock < Block
  2 +
  3 + settings_items :presentation_mode, :type => String, :default => 'title_only'
  4 + settings_items :total_items, :type => Integer, :default => 5
  5 + settings_items :discussion_status, :type => Integer
  6 +
  7 + attr_accessible :presentation_mode, :total_items, :discussion_status
  8 +
  9 + VALID_CONTENT = ['CommentParagraphPlugin::Discussion']
  10 +
  11 + STATUS_NOT_OPENED = 0
  12 + STATUS_AVAILABLE = 1
  13 + STATUS_CLOSED = 2
  14 +
  15 + def self.description
  16 + c_('Discussion Articles')
  17 + end
  18 +
  19 + def help
  20 + _("This block displays all profile's article discussion")
  21 + end
  22 +
  23 + def discussions
  24 + holder.articles.where(type: VALID_CONTENT).order('created_at DESC').limit(self.total_items)
  25 + end
  26 +
  27 + def holder
  28 + return nil if self.box.nil? || self.box.owner.nil?
  29 + if self.box.owner.kind_of?(Environment)
  30 + return nil if self.box.owner.portal_community.nil?
  31 + self.box.owner.portal_community
  32 + else
  33 + self.box.owner
  34 + end
  35 + end
  36 +
  37 + def mode?(attr)
  38 + attr == self.presentation_mode
  39 + end
  40 +
  41 +end
... ...
plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb
... ... @@ -69,7 +69,7 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase
69 69 article = fast_create(Article, :profile_id => profile.id)
70 70 article.expects(:comment_paragraph_plugin_enabled?).returns(true)
71 71 article.expects(:allow_edit?).with(user).returns(true)
72   - article.expects(:comment_paragraph_plugin_activated?).returns(false)
  72 + article.expects(:comment_paragraph_plugin_activated?).at_least_once.returns(false)
73 73  
74 74 assert_equal 'Activate Comments', plugin.article_extra_toolbar_buttons(article)[:title]
75 75 end
... ... @@ -79,7 +79,7 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase
79 79 article = fast_create(Article, :profile_id => profile.id)
80 80 article.expects(:comment_paragraph_plugin_enabled?).returns(true)
81 81 article.expects(:allow_edit?).with(user).returns(true)
82   - article.expects(:comment_paragraph_plugin_activated?).returns(true)
  82 + article.expects(:comment_paragraph_plugin_activated?).at_least_once.returns(true)
83 83  
84 84 assert_equal 'Deactivate Comments', plugin.article_extra_toolbar_buttons(article)[:title]
85 85 end
... ...
plugins/comment_paragraph/test/unit/discussion_block_test.rb 0 → 100644
... ... @@ -0,0 +1,176 @@
  1 +require_relative '../test_helper'
  2 +class DiscussionBlockTest < ActiveSupport::TestCase
  3 +
  4 + def setup
  5 + @environment = Environment.default
  6 + @environment.enable_plugin(CommentParagraphPlugin)
  7 + end
  8 +
  9 + attr_reader :environment
  10 +
  11 + should 'describe itself' do
  12 + assert_not_equal Block.description, CommentParagraphPlugin::DiscussionBlock.description
  13 + end
  14 +
  15 + should 'holder be nil if there is no box' do
  16 + b = CommentParagraphPlugin::DiscussionBlock.new
  17 + assert_nil b.holder
  18 + end
  19 +
  20 + should 'holder be nil if there is no box owner to the box' do
  21 + b = CommentParagraphPlugin::DiscussionBlock.new
  22 + box = Box.new
  23 + b.box = box
  24 + assert_nil b.holder
  25 + end
  26 +
  27 + should 'holder be nil if there is no portal community in environment' do
  28 + b = CommentParagraphPlugin::DiscussionBlock.new
  29 + environment.boxes<< Box.new
  30 + b.box = environment.boxes.last
  31 + assert_nil environment.portal_community
  32 + assert_nil b.holder
  33 + end
  34 +
  35 + should 'holder be the portal community for environments blocks' do
  36 + community = fast_create(Community)
  37 + environment.portal_community= community
  38 + environment.save!
  39 + environment.boxes<< Box.new
  40 + b = CommentParagraphPlugin::DiscussionBlock.new
  41 + b.box = environment.boxes.last
  42 + assert_equal environment.portal_community, b.holder
  43 + end
  44 +
  45 + should 'holder be the person for people blocks' do
  46 + person = fast_create(Person)
  47 + person.boxes << Box.new
  48 + b = CommentParagraphPlugin::DiscussionBlock.new
  49 + b.box = person.boxes.last
  50 + assert_equal person, b.holder
  51 + end
  52 +
  53 + should 'holder be the community for communities blocks' do
  54 + community = fast_create(Community)
  55 + community.boxes << Box.new
  56 + b = CommentParagraphPlugin::DiscussionBlock.new
  57 + b.box = community.boxes.last
  58 + assert_equal community, b.holder
  59 + end
  60 +
  61 + should 'holder be the enterprise for enterprises blocks' do
  62 + enterprise = fast_create(Enterprise)
  63 + enterprise.boxes << Box.new
  64 + b = CommentParagraphPlugin::DiscussionBlock.new
  65 + b.box = enterprise.boxes.last
  66 + assert_equal enterprise, b.holder
  67 + end
  68 +
  69 + should 'discussions return only discussion articles' do
  70 + community = fast_create(Community)
  71 + community.boxes << Box.new
  72 + b = CommentParagraphPlugin::DiscussionBlock.new
  73 + b.box = community.boxes.last
  74 + b.save
  75 + a1 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id)
  76 + fast_create(Event, :profile_id => community.id)
  77 + fast_create(TinyMceArticle, :profile_id => community.id)
  78 + a2 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id)
  79 + assert_equivalent [a1, a2], b.discussions
  80 + end
  81 +
  82 + should 'return only not opened discussions if discussion status is not opened' do
  83 + community = fast_create(Community)
  84 + community.boxes << Box.new
  85 + b = CommentParagraphPlugin::DiscussionBlock.new
  86 + b.box = community.boxes.last
  87 + b.discussion_status = CommentParagraphPlugin::DiscussionBlock::STATUS_NOT_OPENED
  88 + b.save
  89 + a1 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now + 1.day)
  90 + a2 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now )
  91 + a3 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 1.day)
  92 + a4 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 2.day, :end_date => DateTime.now - 1.day)
  93 + assert_equivalent [a1], b.discussions
  94 + end
  95 +
  96 + should 'return only available discussions if discussion status is available' do
  97 + community = fast_create(Community)
  98 + community.boxes << Box.new
  99 + b = CommentParagraphPlugin::DiscussionBlock.new
  100 + b.box = community.boxes.last
  101 + b.discussion_status = CommentParagraphPlugin::DiscussionBlock::STATUS_AVAILABLE
  102 + b.save
  103 + a1 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now + 1.day)
  104 + a2 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now )
  105 + a3 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 1.day)
  106 + a4 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 2.day, :end_date => DateTime.now - 1.day)
  107 + assert_equivalent [a2,a3], b.discussions
  108 + end
  109 +
  110 + should 'return only closed discussions if discussion status is closed' do
  111 + community = fast_create(Community)
  112 + community.boxes << Box.new
  113 + b = CommentParagraphPlugin::DiscussionBlock.new
  114 + b.box = community.boxes.last
  115 + b.discussion_status = CommentParagraphPlugin::DiscussionBlock::STATUS_CLOSED
  116 + b.save
  117 + a1 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now + 1.day)
  118 + a2 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now )
  119 + a3 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 1.day)
  120 + a4 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 2.day, :end_date => DateTime.now - 1.day)
  121 + assert_equivalent [a4], b.discussions
  122 + end
  123 +
  124 +end
  125 +
  126 +require 'boxes_helper'
  127 +
  128 +class DiscussionBlockViewTest < ActionView::TestCase
  129 + include BoxesHelper
  130 +
  131 + should 'show the title and the child titles when the block is set to title only mode' do
  132 + profile = create_user('testuser').person
  133 +
  134 + block = CommentParagraphPlugin::DiscussionBlock.new
  135 + block.stubs(:holder).returns(profile)
  136 + block.presentation_mode = 'title_only'
  137 +
  138 + ActionView::Base.any_instance.stubs(:block_title).returns("Block Title")
  139 + ActionView::Base.any_instance.stubs(:profile).returns(profile)
  140 +
  141 + content = render_block_content(block)
  142 +
  143 + assert_match /discussion-title/, content
  144 + assert_no_match /discussion-abstract/, content
  145 + end
  146 +
  147 + should 'show the title and the child titles and abstracts when the block is set to title and abstract mode' do
  148 + profile = create_user('testuser').person
  149 +
  150 + block = CommentParagraphPlugin::DiscussionBlock.new
  151 + block.stubs(:holder).returns(profile)
  152 + block.presentation_mode = 'title_and_abstract'
  153 +
  154 + ActionView::Base.any_instance.stubs(:block_title).returns("Block Title")
  155 + ActionView::Base.any_instance.stubs(:profile).returns(profile)
  156 +
  157 + content = render_block_content(block)
  158 +
  159 + assert_match /discussion-abstract/, content
  160 + end
  161 +
  162 + should 'show the title and the child full content when the block has no mode set' do
  163 + profile = create_user('testuser').person
  164 +
  165 + block = CommentParagraphPlugin::DiscussionBlock.new
  166 + block.stubs(:holder).returns(profile)
  167 + block.presentation_mode = ''
  168 +
  169 + ActionView::Base.any_instance.stubs(:block_title).returns("Block Title")
  170 + ActionView::Base.any_instance.stubs(:profile).returns(profile)
  171 +
  172 + content = render_block_content(block)
  173 +
  174 + assert_match /discussion-full/, content
  175 + end
  176 +end
... ...
plugins/comment_paragraph/views/blocks/discussion.html.erb 0 → 100644
... ... @@ -0,0 +1,33 @@
  1 +<div id="discussion-block">
  2 + <% children = block.discussions %>
  3 + <div class="discussion">
  4 + <%= block_title(block.title.blank? ? c_("Discussions") : block.title, block.subtitle ) %>
  5 + </div>
  6 + <% if block.mode?('title_only') %>
  7 + <div class="discussion-title">
  8 + <ul>
  9 + <% children.each do |item| %>
  10 + <li> <%= link_to(h(item.title), item.url)%></li>
  11 + <% end %>
  12 + </ul>
  13 + </div>
  14 + <% elsif block.mode?('title_and_abstract') %>
  15 + <div class="discussion-abstract">
  16 + <% children.each do |item| %>
  17 + <h2><%= link_to(item.title,item.url, :class => 'post-title')%></h2>
  18 + <span class="post-date"><%= show_date(item.published_at, true)%></span>
  19 + <div class="headline"><%=item.lead%></div>
  20 + <p class="highlighted-news-read-more"><%= link_to(_('Read more'), item.url) %></p>
  21 + <% end %>
  22 + </div>
  23 + <% else %>
  24 + <div class="discussion-full">
  25 + <% children.each do |item| %>
  26 + <h2><%= link_to(item.title,item.url, :class => 'post-title')%></h2>
  27 + <span class="post-date"><%= show_date(item.published_at, true)%></span>
  28 + <div class="headline"><%=item.body.html_safe %></div>
  29 + <p class="highlighted-news-read-more"><%= link_to(_('Read more'), item.url) %></p>
  30 + <% end %>
  31 + </div>
  32 + <% end %>
  33 +</div>
... ...
plugins/comment_paragraph/views/box_organizer/comment_paragraph_plugin/_discussion_block.html.erb 0 → 100644
... ... @@ -0,0 +1,28 @@
  1 +<%=
  2 +labelled_form_field(_('Choose which blog should be displayed'),
  3 + select_tag(
  4 + 'block[discussion_status]',
  5 + options_for_select([[_('Not Opened Discussions'), CommentParagraphPlugin::DiscussionBlock::STATUS_NOT_OPENED],[_('Available Discussions'), CommentParagraphPlugin::DiscussionBlock::STATUS_AVAILABLE], [_('Closed Discussions'), CommentParagraphPlugin::DiscussionBlock::STATUS_CLOSED]], @block.discussion_status)
  6 + )
  7 +)
  8 +%>
  9 +<%=
  10 +labelled_form_field(_('Choose how the content should be displayed'),
  11 + select_tag(
  12 + 'block[presentation_mode]',
  13 + options_for_select(
  14 + {
  15 + _("Title only") => "title_only",
  16 + _("Title and abstract") => "title_and_abstract",
  17 + _("Full content") => "full_content"
  18 + },
  19 + @block.presentation_mode
  20 + )
  21 + )
  22 +)
  23 +%>
  24 +<%= labelled_form_field(_('Choose how many items will be displayed'),
  25 + text_field_tag('block[total_items]',
  26 + @block.total_items, :size => 3, :maxlength => 5)
  27 + )
  28 +%>
... ...
plugins/comment_paragraph/views/environment_design 0 → 120000
... ... @@ -0,0 +1 @@
  1 +box_organizer
0 2 \ No newline at end of file
... ...
plugins/comment_paragraph/views/profile_design 0 → 120000
... ... @@ -0,0 +1 @@
  1 +box_organizer
0 2 \ No newline at end of file
... ...