diff --git a/README.md b/README.md index e448a5562..e83ee3e26 100644 --- a/README.md +++ b/README.md @@ -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 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 diff --git a/lib/ransack/configuration.rb b/lib/ransack/configuration.rb index 9568c83ea..c35898c69 100644 --- a/lib/ransack/configuration.rb +++ b/lib/ransack/configuration.rb @@ -8,7 +8,8 @@ module Configuration self.predicates = {} self.options = { :search_key => :q, - :ignore_unknown_conditions => true + :ignore_unknown_conditions => true, + :hide_sort_order_indicators => false } def configure @@ -75,5 +76,9 @@ def arel_predicate_with_suffix(arel_predicate, suffix) end end + def hide_sort_order_indicators=(boolean) + self.options[:hide_sort_order_indicators] = boolean + end + end end diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 562a23471..e289562a1 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -94,7 +94,7 @@ def initialize(search, attribute, args, params) @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 @@ -198,11 +198,8 @@ def default_sort_order(attr_name) end def order_indicator - if @hide_indicator || no_sort_direction_specified? - nil - else - direction_arrow - end + return if @hide_indicator || no_sort_direction_specified? + direction_arrow end def no_sort_direction_specified?(dir = @current_dir) diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 261b95fb5..b1526963c 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -343,7 +343,7 @@ module Helpers hide_indicator: true ) } - it { should match /Full Name/ } + it { should_not match /▼|▲/ } end describe '#sort_link with hide order indicator set to false' do @@ -358,6 +358,34 @@ module Helpers it { should match /Full Name ▼/ } end + describe '#sort_link with config set to remove sort 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 /▼|▲/ } + end + + describe '#sort_link with config set to not remove sort 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 ▼/ } + end + describe '#search_form_for with default format' do subject { @controller.view_context .search_form_for(Person.search) {} } @@ -398,7 +426,6 @@ module Helpers } it { should match /example_name_eq/ } end - end end end