From 2e6667bb42b2d0547ada8c9c91d9df952d246243 Mon Sep 17 00:00:00 2001 From: Andrea Dal Ponte Date: Wed, 7 Oct 2015 00:17:16 +0200 Subject: [PATCH] Add block argument for sort_link --- lib/ransack/helpers/form_helper.rb | 11 +++++++++-- spec/ransack/helpers/form_helper_spec.rb | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index be0d0a2be..82135fbdc 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -38,12 +38,19 @@ def search_form_for(record, options = {}, &proc) # # <%= sort_link(@q, :name, [:name, 'kind ASC'], 'Player Name') %> # - def sort_link(search_object, attribute, *args) + # You can use a block as well if your link target is hard to fit into the label parameter: + # + # <%= sort_link(@q, :name, [:name, 'kind ASC']) do %> + # Player Name + # <% end %> + # + def sort_link(search_object, attribute, *args, &block) search, routing_proxy = extract_search_and_routing_proxy(search_object) unless Search === search raise TypeError, 'First argument must be a Ransack::Search!' end - s = SortLink.new(search, attribute, args, params) + args.unshift(capture(&block)) if block_given? + s = SortLink.new(search, attribute, args, params, &block) link_to(s.name, url(routing_proxy, s.url_options), s.html_options(args)) end diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index b963b2b80..bad838bac 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -382,6 +382,20 @@ module Helpers it { should match /Full Name ▼/ } end + describe '#sort_link with a block' do + before do + Ransack.configure { |c| c.hide_sort_order_indicators = false } + end + subject { @controller.view_context + .sort_link( + [:main_app, Person.search(sorts: ['name desc'])], + :name, + controller: 'people' + ) { 'Block label' } + } + it { should match /Block label ▼/ } + end + describe '#search_form_for with default format' do subject { @controller.view_context .search_form_for(Person.search) {} }