Skip to content
Browse files

implement qsub array synthax instead of hash for ordering, ie :

Article.order_by([:id, :desc], :popularity)
  • Loading branch information...
1 parent 4df1233 commit 464c8dc69ef3966be990a0eebca480c4cfa784b1 @jney jney committed
Showing with 15 additions and 11 deletions.
  1. +1 −1 README.textile
  2. +6 −6 lib/utility_scopes/ordered.rb
  3. +8 −4 spec/ordered_spec.rb
View
2 README.textile
@@ -116,7 +116,7 @@ arguments are given it will default to @created_at DESC@:
Article.ordered(:id) # Get all articles ordered by "id"
Article.ordered("rank ASC") # Get all articles ordered by "rank ASC"
Article.order_by(:id) # order_by and sort_by are alias to ordered
- Article.order_by(:id => :desc, :popularity => :asc) # can take a hash as parameter
+ Article.order_by([:id, :desc], :popularity) # can take a hash as parameter
Article.order_by_id # can be set as a sentence
If you would like to specify a different default sort order you can do so on a per class basis
View
12 lib/utility_scopes/ordered.rb
@@ -8,9 +8,9 @@ def self.included(base)
base.class_eval do
# Provide an ordered scope
named_scope(:ordered, lambda { |*order|
- (order.size == 1 && order.first.is_a?(Hash)) ?
- { :order => order.first.collect{|(k,v)| "#{k} #{v.to_s.upcase}" }.join(', ') } :
- { :order => order.flatten.first || self.default_ordering }
+ { :order => order.empty? ?
+ self.default_ordering :
+ order.collect{|e| e.is_a?(Array) ? "#{e[0]} #{e[1].to_s.upcase}" : e}.join(', ') }
})
class << self
@@ -41,9 +41,9 @@ def ordered_by(clause)
# Override named scope on AR::Base so we can access default_ordering
# on subclass
named_scope(:ordered, lambda { |*order|
- (order.size == 1 && order.first.is_a?(Hash)) ?
- { :order => order.first.collect{|(k,v)| "#{k} #{v.to_s.upcase}" }.join(', ') } :
- { :order => order.flatten.first || self.default_ordering }
+ { :order => order.empty? ?
+ self.default_ordering :
+ order.collect{|e| e.is_a?(Array) ? "#{e[0]} #{e[1].to_s.upcase}" : e}.join(', ') }
})
metaclass.instance_eval do
View
12 spec/ordered_spec.rb
@@ -15,8 +15,12 @@
Article.ordered('created_at ASC').proxy_options.should == {:order => 'created_at ASC'}
end
- it "should allow the order to be specified at runtime with 2 args" do
- Article.ordered(:popularity => :asc, :second_param => :desc).proxy_options.should == {:order => 'popularity ASC, second_param DESC'}
+ it "should allow the order to arrays as arg" do
+ Article.ordered([:popularity, :asc], [:second_param, :desc]).proxy_options.should == {:order => 'popularity ASC, second_param DESC'}
+ end
+
+ it "should allow the order with several args" do
+ Article.ordered(:popularity, :updated_at).proxy_options.should == {:order => 'popularity, updated_at'}
end
it "should sort by column popularity when calling order_by_popularity" do
@@ -34,7 +38,7 @@
end
it "should have an alias" do
- Article.order_by(:popularity => :asc).proxy_options.should == {:order => 'popularity ASC'}
+ Article.order_by([:popularity, :asc]).proxy_options.should == {:order => 'popularity ASC'}
end
it "should allow the default to be overidden by using ordered_by" do
@@ -49,6 +53,6 @@
end
it "should be able to handle symbol order criteria" do
- Article.ordered(:id).proxy_options.should == { :order => :id }
+ Article.ordered(:id).proxy_options.should == { :order => 'id' }
end
end

0 comments on commit 464c8dc

Please sign in to comment.
Something went wrong with that request. Please try again.