1
0
Fork 0
mirror of https://github.com/activerecord-hackery/ransack.git synced 2022-11-09 13:47:45 -05:00

Add ability to remove sort_link arrows

This commit is contained in:
Fred Bergman 2014-11-24 16:48:29 +01:00
parent a591075976
commit d992f2b75c
3 changed files with 26 additions and 7 deletions

View file

@ -194,6 +194,12 @@ This example toggles the sort directions of both fields, by default
initially sorting the `last_name` field by ascending order, and the
`first_name` field by descending order.
You can remove the order indicator arrow by passing hide_indicator: true
```erb
<%= sort_link(@q, :name, hide_indicator: true) %>
```
### Advanced Mode
"Advanced" searches (ab)use Rails' nested attributes functionality in order to

View file

@ -65,6 +65,7 @@ module Ransack
end
options = args.first.is_a?(Hash) ? args.shift.dup : {}
hide_indicator = options.delete :hide_indicator
default_order = options.delete :default_order
default_order_is_a_hash = Hash === default_order
@ -133,27 +134,27 @@ module Ransack
url_for(options_for_url)
end
name = link_name(label_text, field_current_dir)
name = link_name(label_text, field_current_dir, hide_indicator)
link_to(name, url, html_options)
end
private
def link_name(label_text, dir)
[ERB::Util.h(label_text), order_indicator_for(dir)]
def link_name(label_text, dir, hide_indicator)
[ERB::Util.h(label_text), order_indicator_for(dir, hide_indicator)]
.compact
.join(Constants::NON_BREAKING_SPACE)
.html_safe
end
def order_indicator_for(dir)
if dir == Constants::ASC
def order_indicator_for(dir, hide_indicator)
if hide_indicator
nil
elsif dir == Constants::ASC
Constants::ASC_ARROW
elsif dir == Constants::DESC
Constants::DESC_ARROW
else
nil
end
end

View file

@ -280,6 +280,18 @@ module Ransack
end
end
describe '#sort_link without order indicator' do
subject { @controller.view_context
.sort_link(
[:main_app, Person.search(:sorts => ['name desc'])],
:name,
:controller => 'people',
:hide_indicator => true
)
}
it { should match /Full Name<\/a>/ }
end
describe '#search_form_for with default format' do
subject { @controller.view_context.search_form_for(Person.search) {} }
it { should match /action="\/people"/ }