Skip to content

Commit

Permalink
* ordered method can take a hash as parameter.
Browse files Browse the repository at this point in the history
* ordered cannot take anymore :asc or :desc as second parameter, use a hash instead
  • Loading branch information
jney committed Oct 23, 2008
1 parent 7e7e935 commit 4df1233
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/utility_scopes/ordered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
})

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/ordered_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 4df1233

Please sign in to comment.