From 321040ba62b400cfcbf05c0f2122f518483a4d88 Mon Sep 17 00:00:00 2001 From: jonatack Date: Wed, 8 Jan 2014 01:46:21 +0100 Subject: [PATCH] Allow for hashes with array sorts PR by @grokse (Kurt Ronshausen) --- lib/ransack/search.rb | 6 +++++- spec/ransack/search_spec.rb | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/ransack/search.rb b/lib/ransack/search.rb index da9bbd1..f1df3c7 100644 --- a/lib/ransack/search.rb +++ b/lib/ransack/search.rb @@ -41,7 +41,11 @@ module Ransack case args when Array args.each do |sort| - sort = Nodes::Sort.extract(@context, sort) + if sort.kind_of? Hash + sort = Nodes::Sort.new(@context).build(sort) + else + sort = Nodes::Sort.extract(@context, sort) + end self.sorts << sort end when Hash diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 8e256ef..56779db 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -225,7 +225,7 @@ module Ransack end it 'creates sorts based on multiple attributes/directions in array format' do - @s.sorts = ['id desc', 'name asc'] + @s.sorts = ['id desc', { :name => 'name', :dir => 'asc' }] @s.sorts.should have(2).items sort1, sort2 = @s.sorts sort1.should be_a Nodes::Sort @@ -237,7 +237,7 @@ module Ransack end it 'creates sorts based on multiple attributes and uppercase directions in array format' do - @s.sorts = ['id DESC', 'name ASC'] + @s.sorts = ['id DESC', { :name => 'name', :dir => 'ASC' }] @s.sorts.should have(2).items sort1, sort2 = @s.sorts sort1.should be_a Nodes::Sort @@ -249,7 +249,7 @@ module Ransack end it 'creates sorts based on multiple attributes and different directions in array format' do - @s.sorts = ['id DESC', 'name'] + @s.sorts = ['id DESC', { :name => 'name', :dir => nil }] @s.sorts.should have(2).items sort1, sort2 = @s.sorts sort1.should be_a Nodes::Sort