diff --git a/lib/ransack/context.rb b/lib/ransack/context.rb index 2e703e9..a651c6b 100644 --- a/lib/ransack/context.rb +++ b/lib/ransack/context.rb @@ -3,7 +3,7 @@ require 'ransack/visitor' module Ransack class Context attr_reader :search, :object, :klass, :base, :engine, :arel_visitor - attr_accessor :auth_object + attr_accessor :auth_object, :search_key class << self @@ -32,6 +32,7 @@ module Ransack @klass = @object.klass @join_dependency = join_dependency(@object) @join_type = options[:join_type] || Arel::OuterJoin + @search_key = options[:search_key] || :q @base = @join_dependency.join_base @engine = @base.arel_engine @default_table = Arel::Table.new(@base.table_name, :as => @base.aliased_table_name, :engine => @engine) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 2a0414f..7919a02 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -33,7 +33,7 @@ module Ransack raise TypeError, "First argument must be a Ransack::Search!" unless Search === search - search_params = params[:q] || {}.with_indifferent_access + search_params = params[search.context.search_key] || {}.with_indifferent_access attr_name = attribute.to_s @@ -56,9 +56,9 @@ module Ransack html_options = args.first.is_a?(Hash) ? args.shift.dup : {} css = ['sort_link', current_dir].compact.join(' ') html_options[:class] = [css, html_options[:class]].compact.join(' ') - options.merge!( - :q => search_params.merge(:s => "#{attr_name} #{new_dir}") - ) + query_hash = {} + query_hash[search.context.search_key] = search_params.merge(:s => "#{attr_name} #{new_dir}") + options.merge!(query_hash) url = if routing_proxy send(routing_proxy).url_for(options) else @@ -84,4 +84,4 @@ module Ransack end end -end \ No newline at end of file +end diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 1ab203a..54d504a 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -25,14 +25,26 @@ module Ransack end end - describe '#sort_link' do + describe '#sort_link with default search_key' do subject { @controller.view_context.sort_link([:main_app, Person.search(:sorts => ['name desc'])], :name, :controller => 'people') } - - it { should match /people\?q%5Bs%5D=name\+asc/} - it { should match /sort_link desc/} - it { should match /Full Name ▼/} + it { should match /people\?q%5Bs%5D=name\+asc/ } + it { should match /sort_link desc/ } + it { should match /Full Name ▼/ } end + describe '#sort_link with default search_key defined as symbol' do + subject { @controller.view_context.sort_link(Person.search({:sorts => ['name desc']}, :search_key => :people_search), + :name, :controller => 'people') } + + it { should match /people\?people_search%5Bs%5D=name\+asc/ } + end + + describe '#sort_link with default search_key defined as string' do + subject { @controller.view_context.sort_link(Person.search({:sorts => ['name desc']}, :search_key => 'people_search'), + :name, :controller => 'people') } + + it { should match /people\?people_search%5Bs%5D=name\+asc/ } + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b27a91e..6cf14bd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -44,4 +44,4 @@ RSpec::Matchers.define :have_attribute_method do |expected| match do |actual| actual.attribute_method?(expected) end -end \ No newline at end of file +end