Add ability to hide sort order indicators via Ransack.configure

Author:    Josh Hunter <josh-hunter@am.com>
Date:      Fri Aug 21 12:21:32 2015 -0500

Closes #577.
This commit is contained in:
Josh Hunter 2015-08-21 12:21:32 -05:00 committed by Jon Atack
parent 5dc3b64124
commit 95d45915fd
4 changed files with 47 additions and 3 deletions

View File

@ -213,6 +213,15 @@ The sort link may be displayed without the order indicator arrow by passing
<%= sort_link(@q, :name, hide_indicator: true) %>
```
Alternatively, all sort links may be displayed without the order indicator arrow
by adding this to an initializer file like `config/initializers/ransack.rb`:
```ruby
Ransack.configure do |c|
c.hide_sort_order_indicators = true
end
```
### Advanced Mode
"Advanced" searches (ab)use Rails' nested attributes functionality in order to

View File

@ -8,7 +8,8 @@ module Ransack
self.predicates = {}
self.options = {
:search_key => :q,
:ignore_unknown_conditions => true
:ignore_unknown_conditions => true,
:hide_sort_order_indicators => false
}
def configure
@ -67,6 +68,12 @@ module Ransack
self.options[:ignore_unknown_conditions] = boolean
end
# Globally hide `sort_link` order indicator arrows if passed `true`.
# Defaults to `false`.
def hide_sort_order_indicators=(boolean)
self.options[:hide_sort_order_indicators] = boolean
end
def arel_predicate_with_suffix(arel_predicate, suffix)
if arel_predicate === Proc
proc { |v| "#{arel_predicate.call(v)}#{suffix}" }

View File

@ -94,7 +94,8 @@ module Ransack
@current_dir = existing_sort_direction
@label_text = extract_label_and_mutate_args!(args)
@options = extract_options_and_mutate_args!(args)
@hide_indicator = @options.delete :hide_indicator
@hide_indicator = @options.delete(:hide_indicator) ||
Ransack.options[:hide_sort_order_indicators]
@default_order = @options.delete :default_order
end

View File

@ -358,6 +358,34 @@ module Ransack
it { should match /Full Name&nbsp;&#9660;/ }
end
describe '#sort_link with config set to globally hide order indicators' do
before do
Ransack.configure { |c| c.hide_sort_order_indicators = true }
end
subject { @controller.view_context
.sort_link(
[:main_app, Person.search(sorts: ['name desc'])],
:name,
controller: 'people'
)
}
it { should_not match /&#9660;|&#9650;/ }
end
describe '#sort_link with config set to globally show order indicators' 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'
)
}
it { should match /Full Name&nbsp;&#9660;/ }
end
describe '#search_form_for with default format' do
subject { @controller.view_context
.search_form_for(Person.search) {} }
@ -398,7 +426,6 @@ module Ransack
}
it { should match /example_name_eq/ }
end
end
end
end