Merge pull request #254 from Robsteranium/patch-1

Previous comparison method failed when the sort column is a date with Nil
This commit is contained in:
Mario Visic 2011-12-15 05:47:17 -08:00
commit 79bf38c419
3 changed files with 16 additions and 2 deletions

View File

@ -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?

View File

@ -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__))

View File

@ -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