Commit 5d8f895c64155b3896e19ca353ac61a30b0b4377

Authored by Luciano Prestes
Committed by Gabriela Navarro
1 parent 98e92d96

Add order option in display_content plugin

Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
plugins/display_content/lib/display_content_block.rb
... ... @@ -25,8 +25,9 @@ class DisplayContentBlock &lt; Block
25 25 {:value => 'abstract', :checked => true}]
26 26 settings_items :display_folder_children, :type => :boolean, :default => true
27 27 settings_items :types, :type => Array, :default => ['TextileArticle', 'TinyMceArticle', 'RawHTMLArticle']
  28 + settings_items :order_by_recent, :type => :boolean, :default => :true
28 29  
29   - attr_accessible :sections, :checked_nodes, :display_folder_children, :types
  30 + attr_accessible :sections, :checked_nodes, :display_folder_children, :types, :order_by_recent
30 31  
31 32 def self.description
32 33 _('Display your contents')
... ... @@ -120,7 +121,11 @@ class DisplayContentBlock &lt; Block
120 121 nodes_conditions = nodes.blank? ? '' : " AND articles.id IN(:nodes) "
121 122 nodes_conditions += ' OR articles.parent_id IN(:nodes) ' if !nodes.blank? && display_folder_children
122 123  
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])
  124 + order_string = "published_at"
  125 + order_string += " DESC" if order_by_recent
  126 +
  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])
  128 +
124 129 proc do
125 130 block.block_title(block.title) +
126 131 content_tag('ul', docs.map {|item|
... ...
plugins/display_content/test/unit/display_content_block_test.rb
... ... @@ -648,4 +648,46 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
648 648 assert_equal [], block.parent_nodes
649 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 +
651 693 end
... ...
plugins/display_content/views/box_organizer/_display_content_block.html.erb
... ... @@ -9,7 +9,7 @@
9 9 <% for section in @block.sections do %>
10 10 <tr>
11 11 <td>
12   - <%= hidden_field_tag 'block[sections][][value]', section[:value] %>
  12 + <%= hidden_field_tag 'block[sections][][value]', section[:value] %>
13 13 <%= check_box_tag 'block[sections][][checked]', section[:value], section[:checked] %>
14 14 </td>
15 15 <td><%= @block.section_name(section[:value]) %></td>
... ... @@ -25,5 +25,8 @@
25 25  
26 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 31 </div>
29 32  
... ...