Merge pull request #278 from paulsponagl/order_by
with_scope order_by: "fieldname [asc|desc]"
This commit is contained in:
commit
7223c171e5
@ -83,11 +83,19 @@ class ContentType
|
|||||||
list = (if conditions.nil? || conditions.empty?
|
list = (if conditions.nil? || conditions.empty?
|
||||||
self.contents
|
self.contents
|
||||||
else
|
else
|
||||||
|
# check for order_by: "field [asc|desc]" condition
|
||||||
|
if order_by = ( conditions.delete('order_by') || conditions.delete(:order_by) )
|
||||||
|
column, sort_order = order_by.split
|
||||||
|
column = column.to_sym
|
||||||
|
else
|
||||||
|
sort_order = nil
|
||||||
|
end
|
||||||
|
|
||||||
conditions_with_names = {}
|
conditions_with_names = {}
|
||||||
|
|
||||||
conditions.each do |key, value|
|
conditions.each do |key, value|
|
||||||
# convert alias (key) to name
|
# convert alias (key) to name
|
||||||
field = self.content_custom_fields.detect { |f| f._alias == key }
|
if field = self.content_custom_fields.detect { |f| f._alias == key }
|
||||||
|
|
||||||
case field.kind.to_sym
|
case field.kind.to_sym
|
||||||
when :category
|
when :category
|
||||||
@ -98,13 +106,16 @@ class ContentType
|
|||||||
conditions_with_names[field._name.to_sym] = value
|
conditions_with_names[field._name.to_sym] = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
self.contents.where(conditions_with_names)
|
conditions_with_names.blank? ? self.contents : self.contents.where(conditions_with_names)
|
||||||
end).sort { |a, b| (a.send(column) && b.send(column)) ? (a.send(column) || 0) <=> (b.send(column) || 0) : 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?
|
if sort_order.nil?
|
||||||
|
(self.order_manually? || self.asc_order?) ? list : list.reverse
|
||||||
self.asc_order? ? list : list.reverse
|
else
|
||||||
|
sort_order == 'asc' ? list : list.reverse
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def sort_contents!(ids)
|
def sort_contents!(ids)
|
||||||
|
@ -90,6 +90,13 @@ describe ContentType do
|
|||||||
@content_type.ordered_contents.collect(&:name).should == %w(Sacha Did)
|
@content_type.ordered_contents.collect(&:name).should == %w(Sacha Did)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'returns a list of contents ordered through condition {order_by: "name asc"}' do
|
||||||
|
@content_type.order_by = 'name'
|
||||||
|
@content_type.order_direction = 'desc'
|
||||||
|
@content_type.ordered_contents(:order_by => 'name asc').collect(&:name).should == %w(Did Sacha)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
it 'returns a list of contents ordered by a Date column when first instance is missing the value' do
|
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 = FactoryGirl.build(:content_type, :order_by => 'created_at')
|
||||||
@content_type.content_custom_fields.build :label => 'Active at', :name => 'active_at', :kind => 'Date'
|
@content_type.content_custom_fields.build :label => 'Active at', :name => 'active_at', :kind => 'Date'
|
||||||
|
Loading…
Reference in New Issue
Block a user