Commit 58156aa712fe4d2a4fff7023d6212250bd170223

Authored by Antonio Terceiro
2 parents 98e92d96 53eb5ba1

Merge branch 'display_content_feature' into 'master'

Display content feature

Adds new render options for display_content_plugin:
  - User can choose type of ordering, recent or oldest.
  - Limit the number of articles showed when dynamically option is checked.

Fix javascript bug path with subdirectory.

See merge request !406
plugins/display_content/lib/display_content_block.rb
@@ -25,8 +25,10 @@ class DisplayContentBlock < Block @@ -25,8 +25,10 @@ class DisplayContentBlock < Block
25 {:value => 'abstract', :checked => true}] 25 {:value => 'abstract', :checked => true}]
26 settings_items :display_folder_children, :type => :boolean, :default => true 26 settings_items :display_folder_children, :type => :boolean, :default => true
27 settings_items :types, :type => Array, :default => ['TextileArticle', 'TinyMceArticle', 'RawHTMLArticle'] 27 settings_items :types, :type => Array, :default => ['TextileArticle', 'TinyMceArticle', 'RawHTMLArticle']
  28 + settings_items :order_by_recent, :type => :boolean, :default => :true
  29 + settings_items :limit_to_show, :type => :integer, :default => 6
28 30
29 - attr_accessible :sections, :checked_nodes, :display_folder_children, :types 31 + attr_accessible :sections, :checked_nodes, :display_folder_children, :types, :order_by_recent, :limit_to_show
30 32
31 def self.description 33 def self.description
32 _('Display your contents') 34 _('Display your contents')
@@ -117,14 +119,22 @@ class DisplayContentBlock < Block @@ -117,14 +119,22 @@ class DisplayContentBlock < Block
117 119
118 def content(args={}) 120 def content(args={})
119 block = self 121 block = self
  122 +
120 nodes_conditions = nodes.blank? ? '' : " AND articles.id IN(:nodes) " 123 nodes_conditions = nodes.blank? ? '' : " AND articles.id IN(:nodes) "
121 nodes_conditions += ' OR articles.parent_id IN(:nodes) ' if !nodes.blank? && display_folder_children 124 nodes_conditions += ' OR articles.parent_id IN(:nodes) ' if !nodes.blank? && display_folder_children
122 125
123 - docs = owner.articles.find(:all, :conditions => ["articles.type IN(:types) #{nodes.blank? ? '' : nodes_conditions}", {:nodes => self.nodes, :types => self.types}], :include => [:profile, :image, :tags]) 126 + order_string = "published_at"
  127 + order_string += " DESC" if order_by_recent
  128 +
  129 + limit_final = [limit_to_show, 0].max
  130 +
  131 + docs = owner.articles.order(order_string).where(["articles.type IN(:types) #{nodes.blank? ? '' : nodes_conditions}", {:nodes => self.nodes, :types => self.types}]).includes(:profile, :image, :tags)
  132 + docs = docs.limit(limit_final) if display_folder_children
  133 +
124 proc do 134 proc do
125 block.block_title(block.title) + 135 block.block_title(block.title) +
126 content_tag('ul', docs.map {|item| 136 content_tag('ul', docs.map {|item|
127 - if !item.folder? 137 + if !item.folder? && item.class != RssFeed
128 content_sections = '' 138 content_sections = ''
129 read_more_section = '' 139 read_more_section = ''
130 tags_section = '' 140 tags_section = ''
plugins/display_content/public/style.css
@@ -36,3 +36,7 @@ @@ -36,3 +36,7 @@
36 color: #FFF; 36 color: #FFF;
37 padding: 2px; 37 padding: 2px;
38 } 38 }
  39 +
  40 +.limit-input {
  41 + width: 8%;
  42 +}
plugins/display_content/test/unit/display_content_block_test.rb
@@ -648,4 +648,67 @@ class DisplayContentBlockTest < ActiveSupport::TestCase @@ -648,4 +648,67 @@ class DisplayContentBlockTest < ActiveSupport::TestCase
648 assert_equal [], block.parent_nodes 648 assert_equal [], block.parent_nodes
649 end 649 end
650 650
  651 + should 'show articles in recent order' do
  652 + profile = create_user('testuser').person
  653 + Article.delete_all
  654 + a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :published_at => DateTime.current)
  655 + a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id, :published_at => (DateTime.current + 1))
  656 +
  657 + block = DisplayContentBlock.new
  658 + block.sections = [{:value => 'title', :checked => true}]
  659 + block.nodes = [a1.id, a2.id]
  660 + box = mock()
  661 + block.stubs(:box).returns(box)
  662 + box.stubs(:owner).returns(profile)
  663 +
  664 + block.order_by_recent = true
  665 +
  666 + a1_index = instance_eval(&block.content).index(a1.name)
  667 + a2_index = instance_eval(&block.content).index(a2.name)
  668 +
  669 + assert a2_index < a1_index
  670 + end
  671 +
  672 + should 'show articles in oldest order' do
  673 + profile = create_user('testuser').person
  674 + Article.delete_all
  675 + a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :published_at => DateTime.current)
  676 + a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id, :published_at => (DateTime.current + 1))
  677 +
  678 + block = DisplayContentBlock.new
  679 + block.sections = [{:value => 'title', :checked => true}]
  680 + block.nodes = [a1.id, a2.id]
  681 + box = mock()
  682 + block.stubs(:box).returns(box)
  683 + box.stubs(:owner).returns(profile)
  684 +
  685 + block.order_by_recent = false
  686 +
  687 + a1_index = instance_eval(&block.content).index(a1.name)
  688 + a2_index = instance_eval(&block.content).index(a2.name)
  689 +
  690 + assert a1_index < a2_index
  691 + end
  692 +
  693 + should 'show articles in recent order with limit option' do
  694 + profile = create_user('testuser').person
  695 + Article.delete_all
  696 + a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :published_at => DateTime.current)
  697 + a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id, :published_at => (DateTime.current + 1))
  698 +
  699 + block = DisplayContentBlock.new
  700 + block.sections = [{:value => 'title', :checked => true}]
  701 + block.display_folder_children = true
  702 + box = mock()
  703 + block.stubs(:box).returns(box)
  704 + box.stubs(:owner).returns(profile)
  705 +
  706 + block.order_by_recent = true
  707 + block.limit_to_show = 1
  708 +
  709 + a1_index = instance_eval(&block.content).index(a1.name)
  710 +
  711 + assert a1_index.nil?
  712 + end
  713 +
651 end 714 end
plugins/display_content/views/box_organizer/_choose_directly.html.erb
@@ -5,6 +5,10 @@ @@ -5,6 +5,10 @@
5 <%= labelled_form_field check_box(:block, :display_folder_children) + _('Dinamically load children of selected folders'), '' %> 5 <%= labelled_form_field check_box(:block, :display_folder_children) + _('Dinamically load children of selected folders'), '' %>
6 </div> 6 </div>
7 7
  8 +<div id="limit">
  9 + <%= labelled_text_field(_('Limit:'), "block[limit_to_show]", @block.limit_to_show, :class => "limit-input") %>
  10 +</div>
  11 +
8 <script type="text/javascript" > 12 <script type="text/javascript" >
9 13
10 jQuery_1_8_3("#display_content").jstree({ 14 jQuery_1_8_3("#display_content").jstree({
@@ -13,7 +17,7 @@ jQuery_1_8_3(&quot;#display_content&quot;).jstree({ @@ -13,7 +17,7 @@ jQuery_1_8_3(&quot;#display_content&quot;).jstree({
13 real_checkboxes : true, 17 real_checkboxes : true,
14 real_checkboxes_names : function (n) { return [("block[checked_nodes[" + n.attr('node_id') + "]]"), 1]; } 18 real_checkboxes_names : function (n) { return [("block[checked_nodes[" + n.attr('node_id') + "]]"), 1]; }
15 }, 19 },
16 - themes : {"theme" : "classic", "icons" : true, "url": "/plugins/display_content/javascripts/jstree/themes/classic/style.css"}, 20 + themes : {"theme" : "classic", "icons" : true, "url": "<%= Noosfero.root %>/plugins/display_content/javascripts/jstree/themes/classic/style.css"},
17 json_data : { 21 json_data : {
18 ajax : { 22 ajax : {
19 url : '<%= url_for @block.url_params %>', 23 url : '<%= url_for @block.url_params %>',
@@ -27,4 +31,19 @@ jQuery_1_8_3(&quot;#display_content&quot;).jstree({ @@ -27,4 +31,19 @@ jQuery_1_8_3(&quot;#display_content&quot;).jstree({
27 31
28 jQuery( "#sortable" ).sortable(); 32 jQuery( "#sortable" ).sortable();
29 33
  34 +function show_limit_field(show){
  35 + if (show){
  36 + jQuery("#limit").show();
  37 + }else {
  38 + jQuery("#limit").hide();
  39 + }
  40 +}
  41 +
  42 +jQuery(document).ready(function(){
  43 + show_limit_field(jQuery("#block_display_folder_children").is(":checked"));
  44 + jQuery("#block_display_folder_children").click(function(event){
  45 + show_limit_field(jQuery("#block_display_folder_children").is(":checked"));
  46 + });
  47 +});
  48 +
30 </script> 49 </script>
plugins/display_content/views/box_organizer/_display_content_block.html.erb
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 <% for section in @block.sections do %> 9 <% for section in @block.sections do %>
10 <tr> 10 <tr>
11 <td> 11 <td>
12 - <%= hidden_field_tag 'block[sections][][value]', section[:value] %> 12 + <%= hidden_field_tag 'block[sections][][value]', section[:value] %>
13 <%= check_box_tag 'block[sections][][checked]', section[:value], section[:checked] %> 13 <%= check_box_tag 'block[sections][][checked]', section[:value], section[:checked] %>
14 </td> 14 </td>
15 <td><%= @block.section_name(section[:value]) %></td> 15 <td><%= @block.section_name(section[:value]) %></td>
@@ -25,5 +25,8 @@ @@ -25,5 +25,8 @@
25 25
26 <%= render_tabs(tabs) %> 26 <%= render_tabs(tabs) %>
27 27
  28 +<h3> <%= _('Order by:')%> </h3>
  29 +<%= labelled_radio_button(_("Most recent"), "block[order_by_recent]", true, @block.order_by_recent)%>
  30 +<%= labelled_radio_button(_("Most oldest"), "block[order_by_recent]", false, !@block.order_by_recent)%>
28 </div> 31 </div>
29 32