Skip to content

Commit

Permalink
Add ability to remove sort_link arrows
Browse files Browse the repository at this point in the history
  • Loading branch information
frederfred committed Nov 24, 2014
1 parent a591075 commit d992f2b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def sort_link(search, attribute, *args)
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

Expand Down Expand Up @@ -133,27 +134,27 @@ def sort_link(search, attribute, *args)
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

Expand Down
12 changes: 12 additions & 0 deletions spec/ransack/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,18 @@ module Helpers
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"/ }
Expand Down

0 comments on commit d992f2b

Please sign in to comment.