Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to remove sort_link arrows #473

Merged
merged 1 commit into from
Nov 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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