From 282e2486b6135c7c6268c668ce7002b7025a8c63 Mon Sep 17 00:00:00 2001 From: Robsteranium Date: Wed, 14 Dec 2011 11:26:24 +0000 Subject: [PATCH 1/2] Previous comparison method failed when the sort column is a date with a Nil. This revision doesn't really sort Nils, rather ignore them. --- app/models/content_type.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/content_type.rb b/app/models/content_type.rb index d56c02ba..978c804a 100644 --- a/app/models/content_type.rb +++ b/app/models/content_type.rb @@ -100,7 +100,7 @@ class ContentType end self.contents.where(conditions_with_names) - end).sort { |a, b| (a.send(column) || 0) <=> (b.send(column) || 0) } + end).sort { |a, b| (a.send(column) && b.send(column)) ? (a.send(column) || 0) <=> (b.send(column) || 0) : 0 } return list if self.order_manually? From 82a61b1d2dcff645b053f7e8d3dab0b82c78d23d Mon Sep 17 00:00:00 2001 From: Robsteranium Date: Thu, 15 Dec 2011 03:16:11 +0000 Subject: [PATCH 2/2] spec added to confirm content_type Date problem & patch. Also 1.8.7 compatibility with syck init. --- config/boot.rb | 2 +- spec/models/content_type_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/config/boot.rb b/config/boot.rb index c3c5221e..bbd37771 100644 --- a/config/boot.rb +++ b/config/boot.rb @@ -2,7 +2,7 @@ require 'rubygems' # Need to explicitly use syck for yaml require 'yaml' -YAML::ENGINE.yamler = 'syck' +YAML::ENGINE.yamler = 'syck' if defined?(YAML::ENGINE) # Set up gems listed in the Gemfile. if File.exist?(File.expand_path('../../Gemfile', __FILE__)) diff --git a/spec/models/content_type_spec.rb b/spec/models/content_type_spec.rb index 73f72679..0d487711 100644 --- a/spec/models/content_type_spec.rb +++ b/spec/models/content_type_spec.rb @@ -89,6 +89,20 @@ describe ContentType do @content_type.order_direction = 'desc' @content_type.ordered_contents.collect(&:name).should == %w(Sacha Did) end + + it 'returns a list of contents ordered by a Date column when first instance is missing the value' do + @content_type = FactoryGirl.build(:content_type, :order_by => 'created_at') + @content_type.content_custom_fields.build :label => 'Active at', :name => 'active_at', :kind => 'Date' + e = Date.parse('01/01/2001') + l = Date.parse('02/02/2002') + [nil,e,l].each { |d| @content_type.contents << @content_type.content_klass.new(:active_at => d) } + @content_type.order_by = 'active_at' + @content_type.order_direction = 'asc' + lambda { @content_type.ordered_contents }.should_not raise_error(ArgumentError) + @content_type.ordered_contents.map(&:active_at).should == [nil,e,l] + @content_type.order_direction = 'desc' + @content_type.ordered_contents.map(&:active_at).should == [l,e,nil] + end end