Permalink
Browse files

Added spec and fix for 'order by custom scope' bug

Signed-off-by: Ben Johnson <bjohnson@binarylogic.com>
  • Loading branch information...
Falk Pauser authored and binarylogic committed Jul 29, 2009
1 parent 64053d5 commit 54cb5da3e0ecd2b514f2b060687e4def5f950580
Showing with 18 additions and 1 deletion.
  1. +11 −1 lib/searchlogic/named_scopes/ordering.rb
  2. +7 −0 spec/named_scopes/ordering_spec.rb
@@ -19,12 +19,16 @@ def primary_condition_name(name) # :nodoc
def order_condition?(name) # :nodoc:
!order_condition_details(name).nil?
end
+
+ def custom_order_condition?(name) # :nodoc:
+ !custom_order_condition_details(name).nil?
+ end
private
def method_missing(name, *args, &block)
if name == :order
named_scope name, lambda { |scope_name|
- return {} if !order_condition?(scope_name) && !association_ordering_condition?(scope_name)
+ return {} if !order_condition?(scope_name) && !custom_order_condition?(scope_name) && !association_ordering_condition?(scope_name)
send(scope_name).proxy_options
}
send(name, *args)
@@ -43,6 +47,12 @@ def order_condition_details(name)
{}
end
end
+
+ def custom_order_condition_details(name)
+ if name.to_s =~ /^(ascend|descend)_by_(.+)$/
+ {:order_as => $1, :scope => name.to_sym}
+ end
+ end
def create_order_conditions(column)
named_scope("ascend_by_#{column}".to_sym, {:order => "#{table_name}.#{column} ASC"})
@@ -20,6 +20,13 @@
it "should have order" do
User.order("ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
end
+
+ it "should have order by custom scope" do
+ User.column_names.should_not include("custom")
+ %w(bjohnson thunt fisons).each { |username| User.create(:username => username) }
+ User.named_scope(:ascend_by_custom, :order => "username ASC, name DESC")
+ User.order("ascend_by_custom").proxy_options.should == User.ascend_by_custom.proxy_options
+ end
it "should have priorty to columns over conflicting association columns" do
Company.ascend_by_users_count

0 comments on commit 54cb5da

Please sign in to comment.