Skip to content

Commit

Permalink
Add ability to hide sort order indicators via Ransack.configure
Browse files Browse the repository at this point in the history
Author:    Josh Hunter <[email protected]>
Date:      Fri Aug 21 12:21:32 2015 -0500

Closes #577.
  • Loading branch information
Josh Hunter authored and jonatack committed Aug 27, 2015
1 parent 5dc3b64 commit 95d4591
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion lib/ransack/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}" }
Expand Down
3 changes: 2 additions & 1 deletion lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 28 additions & 1 deletion spec/ransack/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,34 @@ module Helpers
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) {} }
Expand Down Expand Up @@ -398,7 +426,6 @@ module Helpers
}
it { should match /example_name_eq/ }
end

end
end
end

1 comment on commit 95d4591

@jonatack
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wiki configuration page updated with docs about this feature.

Please sign in to comment.