Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an optional default arrow for unsorted fields #816

Merged
merged 2 commits into from
Jul 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,18 @@ initially sorting the `last_name` field by ascending order, and the

The sort link order indicator arrows may be globally customized by setting a
`custom_arrows` option in an initializer file like
`config/initializers/ransack.rb`:
`config/initializers/ransack.rb`.

You can also enable a `default_arrow` which is displayed on all sortable fields
which are not currently used in the sorting. This is disabled by default so
nothing will be displayed:

```ruby
Ransack.configure do |c|
c.custom_arrows = {
up_arrow: '<i class="custom-up-arrow-icon"></i>',
down_arrow: 'U+02193'
down_arrow: 'U+02193',
default_arrow: '<i class="default-arrow-icon"></i>'
}
end
```
Expand Down
12 changes: 9 additions & 3 deletions lib/ransack/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Configuration
:hide_sort_order_indicators => false,
:up_arrow => '&#9660;'.freeze,
:down_arrow => '&#9650;'.freeze,
:default_arrow => nil,
:sanitize_scope_args => true
}

Expand Down Expand Up @@ -83,20 +84,25 @@ def ignore_unknown_conditions=(boolean)
# up_arrow: '&#9660;'
# down_arrow: '&#9650;'
#
# One or both defaults may be globally overridden in an initializer file
# There is also a default arrow which is displayed if a column is not sorted.
# By default this is nil so nothing will be displayed.
#
# Any of the defaults may be globally overridden in an initializer file
# like `config/initializers/ransack.rb` as follows:
#
# Ransack.configure do |config|
# # Globally set the up arrow to an icon and the down arrow to unicode.
# # Globally set the up arrow to an icon, and the down and default arrows to unicode.
# config.custom_arrows = {
# up_arrow: '<i class="fa fa-long-arrow-up"></i>',
# down_arrow: 'U+02193'
# down_arrow: 'U+02193',
# default_arrow: 'U+11047'
# }
# end
#
def custom_arrows=(opts = {})
self.options[:up_arrow] = opts[:up_arrow].freeze if opts[:up_arrow]
self.options[:down_arrow] = opts[:down_arrow].freeze if opts[:down_arrow]
self.options[:default_arrow] = opts[:default_arrow].freeze if opts[:default_arrow]
end

# Ransack sanitizes many values in your custom scopes into booleans.
Expand Down
7 changes: 6 additions & 1 deletion lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def down_arrow
Ransack.options[:down_arrow]
end

def default_arrow
Ransack.options[:default_arrow]
end

def name
[ERB::Util.h(@label_text), order_indicator]
.compact
Expand Down Expand Up @@ -209,7 +213,8 @@ def default_sort_order(attr_name)
end

def order_indicator
return if @hide_indicator || no_sort_direction_specified?
return if @hide_indicator
return default_arrow if no_sort_direction_specified?
if @current_dir == 'desc'.freeze
up_arrow
else
Expand Down
19 changes: 17 additions & 2 deletions spec/ransack/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module Ransack
it 'should have default values for arrows' do
expect(Ransack.options[:up_arrow]).to eq '&#9660;'
expect(Ransack.options[:down_arrow]).to eq '&#9650;'
expect(Ransack.options[:default_arrow]).to eq nil
end

it 'changes the default value for the up arrow only' do
Expand All @@ -72,17 +73,31 @@ module Ransack
Ransack.options = default
end

it 'changes the default value for both arrows' do
it 'changes the default value for the default arrow only' do
default, new_default_arrow = Ransack.options.clone, '<i class="default"></i>'

Ransack.configure { |c| c.custom_arrows = { default_arrow: new_default_arrow } }

expect(Ransack.options[:up_arrow]).to eq default[:up_arrow]
expect(Ransack.options[:down_arrow]).to eq default[:down_arrow]
expect(Ransack.options[:default_arrow]).to eq new_default_arrow

Ransack.options = default
end

it 'changes the default value for all arrows' do
default = Ransack.options.clone
new_up_arrow = '<i class="fa fa-long-arrow-up"></i>'
new_down_arrow = 'U+02193'
new_default_arrow = 'defaultarrow'

Ransack.configure do |c|
c.custom_arrows = { up_arrow: new_up_arrow, down_arrow: new_down_arrow }
c.custom_arrows = { up_arrow: new_up_arrow, down_arrow: new_down_arrow, default_arrow: new_default_arrow }
end

expect(Ransack.options[:up_arrow]).to eq new_up_arrow
expect(Ransack.options[:down_arrow]).to eq new_down_arrow
expect(Ransack.options[:default_arrow]).to eq new_default_arrow

Ransack.options = default
end
Expand Down
29 changes: 27 additions & 2 deletions spec/ransack/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,36 @@ module Helpers
it { should match /Full Name&nbsp;&#9660;/ }
end

describe '#sort_link with config set to show arrows and a default arrow set' do
before do
Ransack.configure do |c|
c.hide_sort_order_indicators = false
c.custom_arrows = { default_arrow: "defaultarrow"}
end
end

after do
Ransack.configure do |c|
c.custom_arrows = { default_arrow: nil}
end
end

subject { @controller.view_context
.sort_link(
[:main_app, Person.search],
:name,
controller: 'people'
)
}

it { should match /Full Name&nbsp;defaultarrow/ }
end

describe '#sort_link w/config to hide arrows + custom arrow, hides all' do
before do
Ransack.configure do |c|
c.hide_sort_order_indicators = true
c.custom_arrows = { down_arrow: 'down' }
c.custom_arrows = { down_arrow: 'down', default_arrow: "defaultarrow" }
end
end

Expand All @@ -750,7 +775,7 @@ module Helpers
)
}

it { should_not match /&#9660;|down/ }
it { should_not match /&#9660;|down|defaultarrow/ }
end

describe '#sort_link with config set to show arrows + custom arrow' do
Expand Down