From 1898d13e1592733b3ffd80b38a9b2c2035ea2b9d Mon Sep 17 00:00:00 2001 From: Luciano Date: Tue, 6 Jan 2015 11:56:57 -0200 Subject: [PATCH] Add limit option for display_content_plugin when the dinamically option is selected. --- plugins/display_content/lib/display_content_block.rb | 9 +++++++-- plugins/display_content/test/unit/display_content_block_test.rb | 21 +++++++++++++++++++++ plugins/display_content/views/box_organizer/_choose_directly.html.erb | 19 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/plugins/display_content/lib/display_content_block.rb b/plugins/display_content/lib/display_content_block.rb index d6a4b94..d2c4940 100644 --- a/plugins/display_content/lib/display_content_block.rb +++ b/plugins/display_content/lib/display_content_block.rb @@ -26,8 +26,9 @@ class DisplayContentBlock < Block settings_items :display_folder_children, :type => :boolean, :default => true settings_items :types, :type => Array, :default => ['TextileArticle', 'TinyMceArticle', 'RawHTMLArticle'] settings_items :order_by_recent, :type => :boolean, :default => :true + settings_items :limit_to_show, :type => :integer, :default => 6 - attr_accessible :sections, :checked_nodes, :display_folder_children, :types, :order_by_recent + attr_accessible :sections, :checked_nodes, :display_folder_children, :types, :order_by_recent, :limit_to_show def self.description _('Display your contents') @@ -118,13 +119,17 @@ class DisplayContentBlock < Block def content(args={}) block = self + nodes_conditions = nodes.blank? ? '' : " AND articles.id IN(:nodes) " nodes_conditions += ' OR articles.parent_id IN(:nodes) ' if !nodes.blank? && display_folder_children order_string = "published_at" order_string += " DESC" if order_by_recent - docs = owner.articles.order(order_string).find(:all, :conditions => ["articles.type IN(:types) #{nodes.blank? ? '' : nodes_conditions}", {:nodes => self.nodes, :types => self.types}], :include => [:profile, :image, :tags]) + limit_final = [limit_to_show, 0].max + + 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) + docs = docs.limit(limit_final) if display_folder_children proc do block.block_title(block.title) + diff --git a/plugins/display_content/test/unit/display_content_block_test.rb b/plugins/display_content/test/unit/display_content_block_test.rb index baf84dc..8d7557c 100644 --- a/plugins/display_content/test/unit/display_content_block_test.rb +++ b/plugins/display_content/test/unit/display_content_block_test.rb @@ -690,4 +690,25 @@ class DisplayContentBlockTest < ActiveSupport::TestCase assert a1_index < a2_index end + should 'show articles in recent order with limit option' do + profile = create_user('testuser').person + Article.delete_all + a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :published_at => DateTime.current) + a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id, :published_at => (DateTime.current + 1)) + + block = DisplayContentBlock.new + block.sections = [{:value => 'title', :checked => true}] + block.display_folder_children = true + box = mock() + block.stubs(:box).returns(box) + box.stubs(:owner).returns(profile) + + block.order_by_recent = true + block.limit_to_show = 1 + + a1_index = instance_eval(&block.content).index(a1.name) + + assert a1_index.nil? + end + end diff --git a/plugins/display_content/views/box_organizer/_choose_directly.html.erb b/plugins/display_content/views/box_organizer/_choose_directly.html.erb index 9b0c8b1..bfd74df 100644 --- a/plugins/display_content/views/box_organizer/_choose_directly.html.erb +++ b/plugins/display_content/views/box_organizer/_choose_directly.html.erb @@ -5,6 +5,10 @@ <%= labelled_form_field check_box(:block, :display_folder_children) + _('Dinamically load children of selected folders'), '' %> +
+ <%= labelled_text_field(_('Limit:'), "block[limit_to_show]", @block.limit_to_show, :style => 'width: 40%') %> +
+ -- libgit2 0.21.2