Permalink
Browse files

added alias "sort_by", "order_by" to named_scope "ordered"

  • Loading branch information...
1 parent 03a5b97 commit 30f5d2bd5ece2d1e1585b0c7111f06c9e4ffd5bb @jney jney committed Oct 22, 2008
Showing with 10 additions and 2 deletions.
  1. +5 −1 lib/utility_scopes/ordered.rb
  2. +4 −0 spec/ordered_spec.rb
  3. +1 −1 spec/spec_helper.rb
@@ -13,8 +13,10 @@ def self.included(base)
{ :order => order.flatten.first || self.default_ordering }
})
- # Set the default order
class << self
+ alias_method :order_by, :ordered
+ alias_method :sort_by, :ordered
+ # Set the default order
define_method(:default_ordering) { 'created_at DESC' }
end
end
@@ -34,6 +36,8 @@ module ClassMethods
# Article.default_ordering #=> "published_at DESC"
#
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}" } :
View
@@ -19,6 +19,10 @@
Article.ordered(:popularity, :asc).proxy_options.should == {:order => 'popularity ASC'}
end
+ it "should have an alias" do
+ 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
Article.ordered_by 'published_at DESC'
Article.default_ordering.should == 'published_at DESC'
View
@@ -1,5 +1,5 @@
require 'rubygems'
require 'spec'
-
+
Spec::Runner.configure do |config|
end

0 comments on commit 30f5d2b

Please sign in to comment.