Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* ordered method can take a hash as parameter.

* ordered cannot take anymore :asc or :desc as second parameter, use a hash instead
  • Loading branch information...
commit 4df123367776c7d31bb9aac0453e0e33e7f51298 1 parent 7e7e935
Jean-Sébastien Ney jney authored
2  README.textile
View
@@ -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) # :desc and :asc can be set as second parameter
+ Article.order_by(:id => :desc, :popularity => :asc) # 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
10 lib/utility_scopes/ordered.rb
View
@@ -8,8 +8,8 @@ def self.included(base)
base.class_eval do
# Provide an ordered scope
named_scope(:ordered, lambda { |*order|
- (order.size == 2) ?
- { :order => "#{order.flatten.first} #{order.flatten.last.to_s.upcase}" } :
+ (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 }
})
@@ -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 == 2) ?
- { :order => "#{order.flatten.first} #{order.flatten.last.to_s.upcase}" } :
- { :order => order.flatten.first || default_ordering }
+ (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 }
})
metaclass.instance_eval do
4 spec/ordered_spec.rb
View
@@ -16,7 +16,7 @@
end
it "should allow the order to be specified at runtime with 2 args" do
- Article.ordered(:popularity, :asc).proxy_options.should == {:order => 'popularity ASC'}
+ Article.ordered(:popularity => :asc, :second_param => :desc).proxy_options.should == {:order => 'popularity ASC, second_param DESC'}
end
it "should sort by column popularity when calling order_by_popularity" do
@@ -34,7 +34,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
Please sign in to comment.
Something went wrong with that request. Please try again.