From d17ec758f18ff3548aad52879201dbcdb0692298 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 18 Dec 2014 23:51:02 +0100 Subject: [PATCH] Add tests & fix for sort_link `default_order` parsing --- lib/ransack/helpers/form_helper.rb | 2 +- spec/ransack/helpers/form_helper_spec.rb | 36 ++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 1b55693..0ccb14d 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -128,7 +128,7 @@ module Ransack def extract_options_and_mutate_args!(args) if args.first.is_a? Hash - args.shift + args.shift.with_indifferent_access else {} end diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index eaac013..612c818 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -121,7 +121,7 @@ module Ransack } end - describe '#sort_link with default search_key defined as string' do + describe '#sort_link with search_key defined as a string' do subject { @controller.view_context.sort_link( Person.search( @@ -142,6 +142,18 @@ module Ransack } end + describe '#sort_link with default_order defined with a string key' do + subject { @controller.view_context + .sort_link( + [:main_app, Person.search()], + :name, + :controller => 'people', + 'default_order' => 'desc' + ) + } + it { should_not match /default_order/ } + end + describe '#sort_link with multiple search_keys defined as an array' do subject { @controller.view_context .sort_link( @@ -205,13 +217,31 @@ module Ransack it { should match /Full Name ▼/ } end - describe '#sort_link with multiple search_keys should allow a default_order to be specified' do + describe '#sort_link with multiple search_keys and default_order specified as a string' do subject { @controller.view_context .sort_link( [:main_app, Person.search()], :name, [:name, :email], :controller => 'people', - :default_order => 'desc' + 'default_order' => 'desc' + ) + } + it { + should match( + /people\?q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=name\+desc&q(%5B|\[)s(%5D|\])(%5B|\[)(%5D|\])=email\+desc/ + ) + } + it { should match /sort_link/ } + it { should match /Full Name/ } + end + + describe '#sort_link with multiple search_keys and default_order specified as a symbol' do + subject { @controller.view_context + .sort_link( + [:main_app, Person.search()], + :name, [:name, :email], + :controller => 'people', + :default_order => :desc ) } it {