Skip to content

Commit 95d4591

Browse files
Josh Hunterjonatack
authored andcommitted
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.
1 parent 5dc3b64 commit 95d4591

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ The sort link may be displayed without the order indicator arrow by passing
213213
<%= sort_link(@q, :name, hide_indicator: true) %>
214214
```
215215

216+
Alternatively, all sort links may be displayed without the order indicator arrow
217+
by adding this to an initializer file like `config/initializers/ransack.rb`:
218+
219+
```ruby
220+
Ransack.configure do |c|
221+
c.hide_sort_order_indicators = true
222+
end
223+
```
224+
216225
### Advanced Mode
217226

218227
"Advanced" searches (ab)use Rails' nested attributes functionality in order to

lib/ransack/configuration.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module Configuration
88
self.predicates = {}
99
self.options = {
1010
:search_key => :q,
11-
:ignore_unknown_conditions => true
11+
:ignore_unknown_conditions => true,
12+
:hide_sort_order_indicators => false
1213
}
1314

1415
def configure
@@ -67,6 +68,12 @@ def ignore_unknown_conditions=(boolean)
6768
self.options[:ignore_unknown_conditions] = boolean
6869
end
6970

71+
# Globally hide `sort_link` order indicator arrows if passed `true`.
72+
# Defaults to `false`.
73+
def hide_sort_order_indicators=(boolean)
74+
self.options[:hide_sort_order_indicators] = boolean
75+
end
76+
7077
def arel_predicate_with_suffix(arel_predicate, suffix)
7178
if arel_predicate === Proc
7279
proc { |v| "#{arel_predicate.call(v)}#{suffix}" }

lib/ransack/helpers/form_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ def initialize(search, attribute, args, params)
9494
@current_dir = existing_sort_direction
9595
@label_text = extract_label_and_mutate_args!(args)
9696
@options = extract_options_and_mutate_args!(args)
97-
@hide_indicator = @options.delete :hide_indicator
97+
@hide_indicator = @options.delete(:hide_indicator) ||
98+
Ransack.options[:hide_sort_order_indicators]
9899
@default_order = @options.delete :default_order
99100
end
100101

spec/ransack/helpers/form_helper_spec.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,34 @@ module Helpers
358358
it { should match /Full Name&nbsp;&#9660;/ }
359359
end
360360

361+
describe '#sort_link with config set to globally hide order indicators' do
362+
before do
363+
Ransack.configure { |c| c.hide_sort_order_indicators = true }
364+
end
365+
subject { @controller.view_context
366+
.sort_link(
367+
[:main_app, Person.search(sorts: ['name desc'])],
368+
:name,
369+
controller: 'people'
370+
)
371+
}
372+
it { should_not match /&#9660;|&#9650;/ }
373+
end
374+
375+
describe '#sort_link with config set to globally show order indicators' do
376+
before do
377+
Ransack.configure { |c| c.hide_sort_order_indicators = false }
378+
end
379+
subject { @controller.view_context
380+
.sort_link(
381+
[:main_app, Person.search(sorts: ['name desc'])],
382+
:name,
383+
controller: 'people'
384+
)
385+
}
386+
it { should match /Full Name&nbsp;&#9660;/ }
387+
end
388+
361389
describe '#search_form_for with default format' do
362390
subject { @controller.view_context
363391
.search_form_for(Person.search) {} }
@@ -398,7 +426,6 @@ module Helpers
398426
}
399427
it { should match /example_name_eq/ }
400428
end
401-
402429
end
403430
end
404431
end

0 commit comments

Comments
 (0)