diff --git a/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb b/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb index def4113..3f29a81 100644 --- a/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb +++ b/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb @@ -59,6 +59,12 @@ class CommentParagraphPlugin < Noosfero::Plugin [CommentParagraphPlugin::API] end + def self.extra_blocks + { + CommentParagraphPlugin::DiscussionBlock => {:position => ['1','2','3'] } + } + end + def content_types [CommentParagraphPlugin::Discussion] end diff --git a/plugins/comment_paragraph/lib/comment_paragraph_plugin/discussion_block.rb b/plugins/comment_paragraph/lib/comment_paragraph_plugin/discussion_block.rb new file mode 100644 index 0000000..a7a230d --- /dev/null +++ b/plugins/comment_paragraph/lib/comment_paragraph_plugin/discussion_block.rb @@ -0,0 +1,41 @@ +class CommentParagraphPlugin::DiscussionBlock < Block + + settings_items :presentation_mode, :type => String, :default => 'title_only' + settings_items :total_items, :type => Integer, :default => 5 + settings_items :discussion_status, :type => Integer + + attr_accessible :presentation_mode, :total_items, :discussion_status + + VALID_CONTENT = ['CommentParagraphPlugin::Discussion'] + + STATUS_NOT_OPENED = 0 + STATUS_AVAILABLE = 1 + STATUS_CLOSED = 2 + + def self.description + c_('Discussion Articles') + end + + def help + _("This block displays all profile's article discussion") + end + + def discussions + holder.articles.where(type: VALID_CONTENT).order('created_at DESC').limit(self.total_items) + end + + def holder + return nil if self.box.nil? || self.box.owner.nil? + if self.box.owner.kind_of?(Environment) + return nil if self.box.owner.portal_community.nil? + self.box.owner.portal_community + else + self.box.owner + end + end + + def mode?(attr) + attr == self.presentation_mode + end + +end diff --git a/plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb b/plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb index 82a6e68..07f6c24 100644 --- a/plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb +++ b/plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb @@ -69,7 +69,7 @@ class CommentParagraphPluginTest < ActiveSupport::TestCase article = fast_create(Article, :profile_id => profile.id) article.expects(:comment_paragraph_plugin_enabled?).returns(true) article.expects(:allow_edit?).with(user).returns(true) - article.expects(:comment_paragraph_plugin_activated?).returns(false) + article.expects(:comment_paragraph_plugin_activated?).at_least_once.returns(false) assert_equal 'Activate Comments', plugin.article_extra_toolbar_buttons(article)[:title] end @@ -79,7 +79,7 @@ class CommentParagraphPluginTest < ActiveSupport::TestCase article = fast_create(Article, :profile_id => profile.id) article.expects(:comment_paragraph_plugin_enabled?).returns(true) article.expects(:allow_edit?).with(user).returns(true) - article.expects(:comment_paragraph_plugin_activated?).returns(true) + article.expects(:comment_paragraph_plugin_activated?).at_least_once.returns(true) assert_equal 'Deactivate Comments', plugin.article_extra_toolbar_buttons(article)[:title] end diff --git a/plugins/comment_paragraph/test/unit/discussion_block_test.rb b/plugins/comment_paragraph/test/unit/discussion_block_test.rb new file mode 100644 index 0000000..6fe10d5 --- /dev/null +++ b/plugins/comment_paragraph/test/unit/discussion_block_test.rb @@ -0,0 +1,176 @@ +require_relative '../test_helper' +class DiscussionBlockTest < ActiveSupport::TestCase + + def setup + @environment = Environment.default + @environment.enable_plugin(CommentParagraphPlugin) + end + + attr_reader :environment + + should 'describe itself' do + assert_not_equal Block.description, CommentParagraphPlugin::DiscussionBlock.description + end + + should 'holder be nil if there is no box' do + b = CommentParagraphPlugin::DiscussionBlock.new + assert_nil b.holder + end + + should 'holder be nil if there is no box owner to the box' do + b = CommentParagraphPlugin::DiscussionBlock.new + box = Box.new + b.box = box + assert_nil b.holder + end + + should 'holder be nil if there is no portal community in environment' do + b = CommentParagraphPlugin::DiscussionBlock.new + environment.boxes<< Box.new + b.box = environment.boxes.last + assert_nil environment.portal_community + assert_nil b.holder + end + + should 'holder be the portal community for environments blocks' do + community = fast_create(Community) + environment.portal_community= community + environment.save! + environment.boxes<< Box.new + b = CommentParagraphPlugin::DiscussionBlock.new + b.box = environment.boxes.last + assert_equal environment.portal_community, b.holder + end + + should 'holder be the person for people blocks' do + person = fast_create(Person) + person.boxes << Box.new + b = CommentParagraphPlugin::DiscussionBlock.new + b.box = person.boxes.last + assert_equal person, b.holder + end + + should 'holder be the community for communities blocks' do + community = fast_create(Community) + community.boxes << Box.new + b = CommentParagraphPlugin::DiscussionBlock.new + b.box = community.boxes.last + assert_equal community, b.holder + end + + should 'holder be the enterprise for enterprises blocks' do + enterprise = fast_create(Enterprise) + enterprise.boxes << Box.new + b = CommentParagraphPlugin::DiscussionBlock.new + b.box = enterprise.boxes.last + assert_equal enterprise, b.holder + end + + should 'discussions return only discussion articles' do + community = fast_create(Community) + community.boxes << Box.new + b = CommentParagraphPlugin::DiscussionBlock.new + b.box = community.boxes.last + b.save + a1 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id) + fast_create(Event, :profile_id => community.id) + fast_create(TinyMceArticle, :profile_id => community.id) + a2 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id) + assert_equivalent [a1, a2], b.discussions + end + + should 'return only not opened discussions if discussion status is not opened' do + community = fast_create(Community) + community.boxes << Box.new + b = CommentParagraphPlugin::DiscussionBlock.new + b.box = community.boxes.last + b.discussion_status = CommentParagraphPlugin::DiscussionBlock::STATUS_NOT_OPENED + b.save + a1 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now + 1.day) + a2 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now ) + a3 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 1.day) + a4 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 2.day, :end_date => DateTime.now - 1.day) + assert_equivalent [a1], b.discussions + end + + should 'return only available discussions if discussion status is available' do + community = fast_create(Community) + community.boxes << Box.new + b = CommentParagraphPlugin::DiscussionBlock.new + b.box = community.boxes.last + b.discussion_status = CommentParagraphPlugin::DiscussionBlock::STATUS_AVAILABLE + b.save + a1 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now + 1.day) + a2 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now ) + a3 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 1.day) + a4 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 2.day, :end_date => DateTime.now - 1.day) + assert_equivalent [a2,a3], b.discussions + end + + should 'return only closed discussions if discussion status is closed' do + community = fast_create(Community) + community.boxes << Box.new + b = CommentParagraphPlugin::DiscussionBlock.new + b.box = community.boxes.last + b.discussion_status = CommentParagraphPlugin::DiscussionBlock::STATUS_CLOSED + b.save + a1 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now + 1.day) + a2 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now ) + a3 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 1.day) + a4 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 2.day, :end_date => DateTime.now - 1.day) + assert_equivalent [a4], b.discussions + end + +end + +require 'boxes_helper' + +class DiscussionBlockViewTest < ActionView::TestCase + include BoxesHelper + + should 'show the title and the child titles when the block is set to title only mode' do + profile = create_user('testuser').person + + block = CommentParagraphPlugin::DiscussionBlock.new + block.stubs(:holder).returns(profile) + block.presentation_mode = 'title_only' + + ActionView::Base.any_instance.stubs(:block_title).returns("Block Title") + ActionView::Base.any_instance.stubs(:profile).returns(profile) + + content = render_block_content(block) + + assert_match /discussion-title/, content + assert_no_match /discussion-abstract/, content + end + + should 'show the title and the child titles and abstracts when the block is set to title and abstract mode' do + profile = create_user('testuser').person + + block = CommentParagraphPlugin::DiscussionBlock.new + block.stubs(:holder).returns(profile) + block.presentation_mode = 'title_and_abstract' + + ActionView::Base.any_instance.stubs(:block_title).returns("Block Title") + ActionView::Base.any_instance.stubs(:profile).returns(profile) + + content = render_block_content(block) + + assert_match /discussion-abstract/, content + end + + should 'show the title and the child full content when the block has no mode set' do + profile = create_user('testuser').person + + block = CommentParagraphPlugin::DiscussionBlock.new + block.stubs(:holder).returns(profile) + block.presentation_mode = '' + + ActionView::Base.any_instance.stubs(:block_title).returns("Block Title") + ActionView::Base.any_instance.stubs(:profile).returns(profile) + + content = render_block_content(block) + + assert_match /discussion-full/, content + end +end diff --git a/plugins/comment_paragraph/views/blocks/discussion.html.erb b/plugins/comment_paragraph/views/blocks/discussion.html.erb new file mode 100644 index 0000000..eb235ce --- /dev/null +++ b/plugins/comment_paragraph/views/blocks/discussion.html.erb @@ -0,0 +1,33 @@ +
<%= link_to(_('Read more'), item.url) %>
+ <% end %> +<%= link_to(_('Read more'), item.url) %>
+ <% end %> +