Commit 9b61dc026598e836ea17e7e1cd853ce254a2d099
Exists in
ratings_minor_fixes
and in
3 other branches
Merge branch 'discussion_block' into 'master'
comment_paragraph: new block that display discussions See merge request !919
Showing
8 changed files
with
334 additions
and
2 deletions
Show diff stats
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,59 @@ |
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 | + 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 | |
36 | + end | |
37 | + | |
38 | + def holder | |
39 | + return nil if self.box.nil? || self.box.owner.nil? | |
40 | + if self.box.owner.kind_of?(Environment) | |
41 | + return nil if self.box.owner.portal_community.nil? | |
42 | + self.box.owner.portal_community | |
43 | + else | |
44 | + self.box.owner | |
45 | + end | |
46 | + end | |
47 | + | |
48 | + def mode?(attr) | |
49 | + attr == self.presentation_mode | |
50 | + end | |
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 | |
59 | +end | ... | ... |
plugins/comment_paragraph/test/unit/comment_paragraph_plugin_test.rb
... | ... | @@ -69,7 +69,7 @@ class CommentParagraphPluginTest < 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 < 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,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
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 | +%> | ... | ... |