From 5d8f895c64155b3896e19ca353ac61a30b0b4377 Mon Sep 17 00:00:00 2001 From: Luciano Date: Tue, 6 Jan 2015 10:14:05 -0200 Subject: [PATCH] Add order option in display_content plugin --- plugins/display_content/lib/display_content_block.rb | 9 +++++++-- plugins/display_content/test/unit/display_content_block_test.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ plugins/display_content/views/box_organizer/_display_content_block.html.erb | 5 ++++- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/plugins/display_content/lib/display_content_block.rb b/plugins/display_content/lib/display_content_block.rb index 5de69dd..d6a4b94 100644 --- a/plugins/display_content/lib/display_content_block.rb +++ b/plugins/display_content/lib/display_content_block.rb @@ -25,8 +25,9 @@ class DisplayContentBlock < Block {:value => 'abstract', :checked => true}] 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 - attr_accessible :sections, :checked_nodes, :display_folder_children, :types + attr_accessible :sections, :checked_nodes, :display_folder_children, :types, :order_by_recent def self.description _('Display your contents') @@ -120,7 +121,11 @@ class DisplayContentBlock < Block nodes_conditions = nodes.blank? ? '' : " AND articles.id IN(:nodes) " nodes_conditions += ' OR articles.parent_id IN(:nodes) ' if !nodes.blank? && display_folder_children - docs = owner.articles.find(:all, :conditions => ["articles.type IN(:types) #{nodes.blank? ? '' : nodes_conditions}", {:nodes => self.nodes, :types => self.types}], :include => [:profile, :image, :tags]) + 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]) + proc do block.block_title(block.title) + content_tag('ul', docs.map {|item| 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 0bdf70b..baf84dc 100644 --- a/plugins/display_content/test/unit/display_content_block_test.rb +++ b/plugins/display_content/test/unit/display_content_block_test.rb @@ -648,4 +648,46 @@ class DisplayContentBlockTest < ActiveSupport::TestCase assert_equal [], block.parent_nodes end + should 'show articles in recent order' 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.nodes = [a1.id, a2.id] + box = mock() + block.stubs(:box).returns(box) + box.stubs(:owner).returns(profile) + + block.order_by_recent = true + + a1_index = instance_eval(&block.content).index(a1.name) + a2_index = instance_eval(&block.content).index(a2.name) + + assert a2_index < a1_index + end + + should 'show articles in oldest order' 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.nodes = [a1.id, a2.id] + box = mock() + block.stubs(:box).returns(box) + box.stubs(:owner).returns(profile) + + block.order_by_recent = false + + a1_index = instance_eval(&block.content).index(a1.name) + a2_index = instance_eval(&block.content).index(a2.name) + + assert a1_index < a2_index + end + end diff --git a/plugins/display_content/views/box_organizer/_display_content_block.html.erb b/plugins/display_content/views/box_organizer/_display_content_block.html.erb index 44ffd69..a125ecc 100644 --- a/plugins/display_content/views/box_organizer/_display_content_block.html.erb +++ b/plugins/display_content/views/box_organizer/_display_content_block.html.erb @@ -9,7 +9,7 @@ <% for section in @block.sections do %> - <%= hidden_field_tag 'block[sections][][value]', section[:value] %> + <%= hidden_field_tag 'block[sections][][value]', section[:value] %> <%= check_box_tag 'block[sections][][checked]', section[:value], section[:checked] %> <%= @block.section_name(section[:value]) %> @@ -25,5 +25,8 @@ <%= render_tabs(tabs) %> +

<%= _('Order by:')%>

+<%= labelled_radio_button(_("Most recent"), "block[order_by_recent]", true, @block.order_by_recent)%> +<%= labelled_radio_button(_("Most oldest"), "block[order_by_recent]", false, !@block.order_by_recent)%> -- libgit2 0.21.2