Commit 4caf1712a177c23bdc4269a0b77426276a8016d6

Authored by Victor Costa
2 parents c196af79 9b61dc02
Exists in staging and in 1 other branch production

Merge branch 'master' into staging

Conflicts:
	plugins/comment_paragraph/lib/comment_paragraph_plugin/discussion_block.rb
	plugins/comment_paragraph/views/blocks/discussion.html.erb
	plugins/comment_paragraph/views/box_organizer/comment_paragraph_plugin/_discussion_block.html.erb
plugins/comment_paragraph/lib/comment_paragraph_plugin/discussion_block.rb
... ... @@ -2,13 +2,16 @@ class CommentParagraphPlugin::DiscussionBlock < Block
2 2  
3 3 settings_items :presentation_mode, :type => String, :default => 'title_only'
4 4 settings_items :total_items, :type => Integer, :default => 5
5   - settings_items :show_blog_picture, :type => :boolean, :default => false
6 5 settings_items :discussion_status, :type => Integer
7 6  
8   - attr_accessible :presentation_mode, :total_items, :show_blog_picture, :discussion_status
  7 + attr_accessible :presentation_mode, :total_items, :discussion_status
9 8  
10 9 VALID_CONTENT = ['CommentParagraphPlugin::Discussion']
11 10  
  11 + STATUS_NOT_OPENED = 0
  12 + STATUS_AVAILABLE = 1
  13 + STATUS_CLOSED = 2
  14 +
12 15 def self.description
13 16 c_('Discussion Articles')
14 17 end
... ... @@ -18,17 +21,18 @@ class CommentParagraphPlugin::DiscussionBlock < Block
18 21 end
19 22  
20 23 def discussions
21   -# start_date = nil
22   -# end_date = nil
23   -# case self.discussion_status.to_s
24   -# when '0'
25   -# start_date > Time.now
26   -# when '2'
27   -# end_date < Time.now
28   -# else
29   -# start_date < Time.now && end_date > Time.now
30   -# end
31   - holder.articles.where(type: VALID_CONTENT).order('created_at DESC').limit(self.total_items)
  24 + current_time = Time.now
  25 + discussions = holder.articles.where(type: VALID_CONTENT).order('start_date ASC, end_date DESC, created_at DESC').limit(self.total_items)
  26 + case discussion_status
  27 + when STATUS_NOT_OPENED
  28 + discussions = discussions.where("start_date > ?", current_time)
  29 + when STATUS_AVAILABLE
  30 + discussions = discussions.where("start_date is null or start_date <= ?", current_time)
  31 + discussions = discussions.where("end_date is null or end_date >= ?", current_time)
  32 + when STATUS_CLOSED
  33 + discussions = discussions.where("end_date < ?", current_time)
  34 + end
  35 + discussions
32 36 end
33 37  
34 38 def holder
... ... @@ -41,10 +45,15 @@ class CommentParagraphPlugin::DiscussionBlock &lt; Block
41 45 end
42 46 end
43 47  
44   - include DatesHelper
45   -
46 48 def mode?(attr)
47 49 attr == self.presentation_mode
48 50 end
49 51  
  52 + def api_content
  53 + Api::Entities::ArticleBase.represent(self.discussions).as_json
  54 + end
  55 +
  56 + def display_api_content_by_default?
  57 + false
  58 + end
50 59 end
... ...
plugins/comment_paragraph/test/unit/discussion_block_test.rb 0 → 100644
... ... @@ -0,0 +1,204 @@
  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 + a5 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :end_date => DateTime.now + 1.day)
  108 + assert_equivalent [a2, a3, a5], b.discussions
  109 + end
  110 +
  111 + should 'return only closed discussions if discussion status is closed' do
  112 + community = fast_create(Community)
  113 + community.boxes << Box.new
  114 + b = CommentParagraphPlugin::DiscussionBlock.new
  115 + b.box = community.boxes.last
  116 + b.discussion_status = CommentParagraphPlugin::DiscussionBlock::STATUS_CLOSED
  117 + b.save
  118 + a1 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now + 1.day)
  119 + a2 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now )
  120 + a3 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 1.day)
  121 + a4 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, :start_date => DateTime.now - 2.day, :end_date => DateTime.now - 1.day)
  122 + assert_equivalent [a4], b.discussions
  123 + end
  124 +
  125 +end
  126 +
  127 +require 'boxes_helper'
  128 +
  129 +class DiscussionBlockViewTest < ActionView::TestCase
  130 + include BoxesHelper
  131 +
  132 + should 'show the title and the child titles when the block is set to title only mode' do
  133 + profile = create_user('testuser').person
  134 +
  135 + block = CommentParagraphPlugin::DiscussionBlock.new
  136 + block.stubs(:holder).returns(profile)
  137 + block.presentation_mode = 'title_only'
  138 +
  139 + ActionView::Base.any_instance.stubs(:block_title).returns("Block Title")
  140 + ActionView::Base.any_instance.stubs(:profile).returns(profile)
  141 +
  142 + content = render_block_content(block)
  143 +
  144 + assert_match /discussion-title/, content
  145 + assert_no_match /discussion-abstract/, content
  146 + end
  147 +
  148 + should 'show the title and the child titles and abstracts when the block is set to title and abstract mode' do
  149 + profile = create_user('testuser').person
  150 +
  151 + block = CommentParagraphPlugin::DiscussionBlock.new
  152 + block.stubs(:holder).returns(profile)
  153 + block.presentation_mode = 'title_and_abstract'
  154 +
  155 + ActionView::Base.any_instance.stubs(:block_title).returns("Block Title")
  156 + ActionView::Base.any_instance.stubs(:profile).returns(profile)
  157 +
  158 + content = render_block_content(block)
  159 +
  160 + assert_match /discussion-abstract/, content
  161 + end
  162 +
  163 + should 'show the title and the child full content when the block has no mode set' do
  164 + profile = create_user('testuser').person
  165 +
  166 + block = CommentParagraphPlugin::DiscussionBlock.new
  167 + block.stubs(:holder).returns(profile)
  168 + block.presentation_mode = ''
  169 +
  170 + ActionView::Base.any_instance.stubs(:block_title).returns("Block Title")
  171 + ActionView::Base.any_instance.stubs(:profile).returns(profile)
  172 +
  173 + content = render_block_content(block)
  174 +
  175 + assert_match /discussion-full/, content
  176 + end
  177 +
  178 + should 'return discussions in api_content' do
  179 + community = fast_create(Community)
  180 + community.boxes << Box.new
  181 + b = CommentParagraphPlugin::DiscussionBlock.new
  182 + b.box = community.boxes.last
  183 + b.save
  184 + a1 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id)
  185 + fast_create(Event, :profile_id => community.id)
  186 + fast_create(TinyMceArticle, :profile_id => community.id)
  187 + a2 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id)
  188 + assert_equivalent [a2.id, a1.id], b.api_content['articles'].map {|a| a[:id]}
  189 + end
  190 +
  191 + should 'sort discussions by start_date, end_date and created_at' do
  192 + community = fast_create(Community)
  193 + community.boxes << Box.new
  194 + b = CommentParagraphPlugin::DiscussionBlock.new
  195 + b.box = community.boxes.last
  196 + b.save
  197 + a1 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, start_date: Time.now)
  198 + a2 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, start_date: Time.now + 1, end_date: Time.now)
  199 + a3 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, start_date: Time.now + 1, end_date: Time.now + 1.day)
  200 + a4 = fast_create(CommentParagraphPlugin::Discussion, :profile_id => community.id, start_date: Time.now + 1, end_date: Time.now + 1.day)
  201 + assert_equal [a1.id, a2.id, a3.id, a4.id], b.discussions.map(&:id)
  202 + end
  203 +
  204 +end
... ...
plugins/comment_paragraph/views/blocks/discussion.html.erb
... ... @@ -25,7 +25,7 @@
25 25 <% children.each do |item| %>
26 26 <h2><%= link_to(item.title,item.url, :class => 'post-title')%></h2>
27 27 <span class="post-date"><%= show_date(item.published_at, true)%></span>
28   - <div class="headline"><%=item.body%></div>
  28 + <div class="headline"><%=item.body.html_safe %></div>
29 29 <p class="highlighted-news-read-more"><%= link_to(_('Read more'), item.url) %></p>
30 30 <% end %>
31 31 </div>
... ...
plugins/comment_paragraph/views/box_organizer/comment_paragraph_plugin/_discussion_block.html.erb
... ... @@ -2,7 +2,7 @@
2 2 labelled_form_field(_('Choose which blog should be displayed'),
3 3 select_tag(
4 4 'block[discussion_status]',
5   - options_for_select([[_('Not Openned Discussions'), 0],[_('Available Discussions'), 1], [_('Closed Discussions'),2]], @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 6 )
7 7 )
8 8 %>
... ...