Commit 1898d13e1592733b3ffd80b38a9b2c2035ea2b9d

Authored by Luciano Prestes
Committed by Gabriela Navarro
1 parent 5d8f895c

Add limit option for display_content_plugin when the dinamically option is selected.

Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
plugins/display_content/lib/display_content_block.rb
... ... @@ -26,8 +26,9 @@ class DisplayContentBlock &lt; Block
26 26 settings_items :display_folder_children, :type => :boolean, :default => true
27 27 settings_items :types, :type => Array, :default => ['TextileArticle', 'TinyMceArticle', 'RawHTMLArticle']
28 28 settings_items :order_by_recent, :type => :boolean, :default => :true
  29 + settings_items :limit_to_show, :type => :integer, :default => 6
29 30  
30   - attr_accessible :sections, :checked_nodes, :display_folder_children, :types, :order_by_recent
  31 + attr_accessible :sections, :checked_nodes, :display_folder_children, :types, :order_by_recent, :limit_to_show
31 32  
32 33 def self.description
33 34 _('Display your contents')
... ... @@ -118,13 +119,17 @@ class DisplayContentBlock &lt; Block
118 119  
119 120 def content(args={})
120 121 block = self
  122 +
121 123 nodes_conditions = nodes.blank? ? '' : " AND articles.id IN(:nodes) "
122 124 nodes_conditions += ' OR articles.parent_id IN(:nodes) ' if !nodes.blank? && display_folder_children
123 125  
124 126 order_string = "published_at"
125 127 order_string += " DESC" if order_by_recent
126 128  
127   - 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])
  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
128 133  
129 134 proc do
130 135 block.block_title(block.title) +
... ...
plugins/display_content/test/unit/display_content_block_test.rb
... ... @@ -690,4 +690,25 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
690 690 assert a1_index < a2_index
691 691 end
692 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 +
693 714 end
... ...
plugins/display_content/views/box_organizer/_choose_directly.html.erb
... ... @@ -5,6 +5,10 @@
5 5 <%= labelled_form_field check_box(:block, :display_folder_children) + _('Dinamically load children of selected folders'), '' %>
6 6 </div>
7 7  
  8 +<div id="limit">
  9 + <%= labelled_text_field(_('Limit:'), "block[limit_to_show]", @block.limit_to_show, :style => 'width: 40%') %>
  10 +</div>
  11 +
8 12 <script type="text/javascript" >
9 13  
10 14 jQuery_1_8_3("#display_content").jstree({
... ... @@ -27,4 +31,19 @@ jQuery_1_8_3(&quot;#display_content&quot;).jstree({
27 31  
28 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 49 </script>
... ...