Added custom search param key feature refs #43

Merges in #81
This commit is contained in:
Ryan Bigg 2012-04-11 11:58:27 -04:00
parent f3da0b529f
commit 2856ac6343
4 changed files with 25 additions and 12 deletions

View File

@ -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)

View File

@ -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
end

View File

@ -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 &#9660;/}
it { should match /people\?q%5Bs%5D=name\+asc/ }
it { should match /sort_link desc/ }
it { should match /Full Name &#9660;/ }
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

View File

@ -44,4 +44,4 @@ RSpec::Matchers.define :have_attribute_method do |expected|
match do |actual|
actual.attribute_method?(expected)
end
end
end