mirror of
https://github.com/activerecord-hackery/ransack.git
synced 2022-11-09 13:47:45 -05:00
Add ability to use a named route when using sort_link
For example, sort_link [:engine, Engine::Model.search] Will use the engine's routing to determine where the route should go. The :engine symbol here should be the name of your engine, i.e. the engine_name setting which generally defaults to the downcased version of your engine's module. Addresses problem discussed in #84.
This commit is contained in:
parent
adcd42a9e8
commit
25ba7e3a8f
2 changed files with 12 additions and 4 deletions
|
@ -25,6 +25,12 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def sort_link(search, attribute, *args)
|
def sort_link(search, attribute, *args)
|
||||||
|
# Extract out a routing proxy for url_for scoping later
|
||||||
|
if search.is_a?(Array)
|
||||||
|
routing_proxy = search.shift
|
||||||
|
search = search.first
|
||||||
|
end
|
||||||
|
|
||||||
raise TypeError, "First argument must be a Ransack::Search!" unless Search === search
|
raise TypeError, "First argument must be a Ransack::Search!" unless Search === search
|
||||||
|
|
||||||
search_params = params[:q] || {}.with_indifferent_access
|
search_params = params[:q] || {}.with_indifferent_access
|
||||||
|
@ -53,9 +59,11 @@ module Ransack
|
||||||
options.merge!(
|
options.merge!(
|
||||||
:q => search_params.merge(:s => "#{attr_name} #{new_dir}")
|
:q => search_params.merge(:s => "#{attr_name} #{new_dir}")
|
||||||
)
|
)
|
||||||
|
options.merge!(:use_route => routing_proxy) if routing_proxy
|
||||||
|
|
||||||
link_to [ERB::Util.h(name), order_indicator_for(current_dir)].compact.join(' ').html_safe,
|
link_to [ERB::Util.h(name), order_indicator_for(current_dir)].compact.join(' ').html_safe,
|
||||||
url_for(options),
|
url_for(options),
|
||||||
html_options
|
html_options
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -26,7 +26,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#sort_link' do
|
describe '#sort_link' do
|
||||||
subject { @controller.view_context.sort_link(Person.search(:sorts => ['name desc']), :name, :controller => 'people') }
|
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 /people\?q%5Bs%5D=name\+asc/}
|
||||||
it { should match /sort_link desc/}
|
it { should match /sort_link desc/}
|
||||||
|
@ -35,4 +35,4 @@ module Ransack
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue