From 95d45915fd2ca0530a714470391c228cf9c4a7d0 Mon Sep 17 00:00:00 2001 From: Josh Hunter Date: Fri, 21 Aug 2015 12:21:32 -0500 Subject: [PATCH] Add ability to hide sort order indicators via Ransack.configure Author: Josh Hunter Date: Fri Aug 21 12:21:32 2015 -0500 Closes #577. --- README.md | 9 ++++++++ lib/ransack/configuration.rb | 9 +++++++- lib/ransack/helpers/form_helper.rb | 3 ++- spec/ransack/helpers/form_helper_spec.rb | 29 +++++++++++++++++++++++- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e448a5562..2c5e69b50 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 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 diff --git a/lib/ransack/configuration.rb b/lib/ransack/configuration.rb index 9568c83ea..be02947e1 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 @@ -67,6 +68,12 @@ def ignore_unknown_conditions=(boolean) 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}" } diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 562a23471..06651965c 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -94,7 +94,8 @@ 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 diff --git a/spec/ransack/helpers/form_helper_spec.rb b/spec/ransack/helpers/form_helper_spec.rb index 261b95fb5..b1422da59 100644 --- a/spec/ransack/helpers/form_helper_spec.rb +++ b/spec/ransack/helpers/form_helper_spec.rb @@ -358,6 +358,34 @@ module Helpers it { should match /Full Name ▼/ } 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 /▼|▲/ } + 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 ▼/ } + 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