Commit 5d8f895c64155b3896e19ca353ac61a30b0b4377
Committed by
Gabriela Navarro
1 parent
98e92d96
Exists in
master
and in
27 other branches
Add order option in display_content plugin
Signed-off-by: Gabriela Navarro <navarro1703@gmail.com> Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
Showing
3 changed files
with
53 additions
and
3 deletions
Show diff stats
plugins/display_content/lib/display_content_block.rb
@@ -25,8 +25,9 @@ class DisplayContentBlock < Block | @@ -25,8 +25,9 @@ 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 | ||
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 | def self.description | 32 | def self.description |
32 | _('Display your contents') | 33 | _('Display your contents') |
@@ -120,7 +121,11 @@ class DisplayContentBlock < Block | @@ -120,7 +121,11 @@ class DisplayContentBlock < Block | ||
120 | nodes_conditions = nodes.blank? ? '' : " AND articles.id IN(:nodes) " | 121 | nodes_conditions = nodes.blank? ? '' : " AND articles.id IN(:nodes) " |
121 | nodes_conditions += ' OR articles.parent_id IN(:nodes) ' if !nodes.blank? && display_folder_children | 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 | proc do | 129 | proc do |
125 | block.block_title(block.title) + | 130 | block.block_title(block.title) + |
126 | content_tag('ul', docs.map {|item| | 131 | content_tag('ul', docs.map {|item| |
plugins/display_content/test/unit/display_content_block_test.rb
@@ -648,4 +648,46 @@ class DisplayContentBlockTest < ActiveSupport::TestCase | @@ -648,4 +648,46 @@ 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 | + | ||
651 | end | 693 | end |
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 |