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 customizable indicator arrows #726

Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Change Log

## Unreleased

### Added

* Add config option to customize up and down arrows used for direction indicators in the FormHelper. PR [#726](https://github.com/activerecord-hackery/ransack/pull/726)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please wrap at 80 characters in the source file?


*Garett Arrowood*

### Fixed

* Use class attributes properly so that inheritance is respected.
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,18 @@ 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`:
These indicator arrows may also be customized by setting them in an initializer file like `config/initializers/ransack.rb`:
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here :)


```ruby
Ransack.configure do |c|
c.custom_arrows = {
up_arrow: '<i class="custom-up-arrow-icon"></i>',
down_arrow: 'U+02193'
}
end
```

Alternatively, all sort links may be displayed without the order indicator arrows:
Copy link
Contributor

Choose a reason for hiding this comment

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

and here :)


```ruby
Ransack.configure do |c|
Expand Down
22 changes: 21 additions & 1 deletion lib/ransack/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module Configuration
self.options = {
:search_key => :q,
:ignore_unknown_conditions => true,
:hide_sort_order_indicators => false
:hide_sort_order_indicators => false,
:up_arrow => '&#9660;'.freeze,
:down_arrow => '&#9650;'.freeze
}

def configure
Expand Down Expand Up @@ -75,6 +77,24 @@ def ignore_unknown_conditions=(boolean)
self.options[:ignore_unknown_conditions] = boolean
end

# Ransack's default indicator arrows are html-code snippets. These
# arrows may be replaced by anything wrapped in quotation marks. Both
# or just one arrow may be globally overridden in an initializer file
# like `config/initializers/ransack.rb` as follows:
#
# Ransack.configure do |config|
# # Set the up_arrow as an icon, set the down arrow as unicode
Copy link
Contributor

@jonatack jonatack Sep 28, 2016

Choose a reason for hiding this comment

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

For line 86, I'd suggest # # Set the up arrow as an icon, and the down arrow as unicode.

(Note: "up arrow" written as text instead of in snakecase)

# config.custom_arrows = {
# up_arrow: '<i class="fa fa-long-arrow-up"></i>',
# down_arrow: 'U+02193'
# }
# 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]
end

# By default, Ransack displays sort order indicator arrows in sort links.
# The default may be globally overridden in an initializer file like
# `config/initializers/ransack.rb` as follows:
Expand Down
4 changes: 2 additions & 2 deletions lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ def initialize(search, attribute, args, params)
end

def up_arrow
'&#9660;'.freeze
Ransack.options[:up_arrow]
end

def down_arrow
'&#9650;'.freeze
Ransack.options[:down_arrow]
end
Copy link
Contributor

@jonatack jonatack Sep 28, 2016

Choose a reason for hiding this comment

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

I originally extracted these 2 public arrow methods out from the private method #order_indicator to allow developers to set custom arrows by overloading them.

Now that developers can do it with a config setting, I'm not sure there is any reason to keep them as separate public methods. Performance-wise it would be better to inline them.

WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, except if anyone is actually overloading these methods now, their apps will break. Never mind 😄


def name
Expand Down
23 changes: 23 additions & 0 deletions spec/mongoid/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ module Ransack
Ransack.options = before
end

it 'should have default values for arrows' do
expect(Ransack.options[:up_arrow]).to eq '&#9660;'.freeze
expect(Ransack.options[:down_arrow]).to eq '&#9650;'.freeze
end

it 'changes default arrow strings' do
# store original state so we can restore it later
before = Ransack.options.clone

Ransack.configure do |config|
config.custom_arrows = {
up_arrow: '<i class="fa fa-long-arrow-up"></i>',
down_arrow: 'U+02193'
}
end

expect(Ransack.options[:up_arrow]).to eq '<i class="fa fa-long-arrow-up"></i>'.freeze
expect(Ransack.options[:down_arrow]).to eq 'U+02193'.freeze

# restore original state so we don't break other tests
Ransack.options = before
end

it 'adds predicates that take arrays, overriding compounds' do
Ransack.configure do |config|
config.add_predicate(
Expand Down
23 changes: 23 additions & 0 deletions spec/ransack/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ module Ransack
Ransack.options = before
end

it 'should have default values for arrows' do
expect(Ransack.options[:up_arrow]).to eq '&#9660;'.freeze
expect(Ransack.options[:down_arrow]).to eq '&#9650;'.freeze
end

it 'changes default arrow strings' do
# store original state so we can restore it later
before = Ransack.options.clone

Ransack.configure do |config|
config.custom_arrows = {
up_arrow: '<i class="fa fa-long-arrow-up"></i>',
down_arrow: 'U+02193'
}
end

expect(Ransack.options[:up_arrow]).to eq '<i class="fa fa-long-arrow-up"></i>'.freeze
expect(Ransack.options[:down_arrow]).to eq 'U+02193'.freeze

# restore original state so we don't break other tests
Ransack.options = before
end

it 'adds predicates that take arrays, overriding compounds' do
Ransack.configure do |config|
config.add_predicate(
Expand Down
46 changes: 46 additions & 0 deletions spec/ransack/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,52 @@ module Helpers
it { should match /Full Name&nbsp;&#9660;/ }
end

describe '#sort_link with config set with custom up_arrow' do
before do
Ransack.configure do |c|
c.custom_arrows = { up_arrow: "\u{1F446}" }
end
end
after do
#set back to default
Ransack.configure do |c|
c.custom_arrows = { up_arrow: "&#9660;" }
end
end
subject { @controller.view_context
.sort_link(
[:main_app, Person.search(sorts: ['name desc'])],
:name,
controller: 'people',
hide_indicator: false
)
}
it { should match /Full Name&nbsp;\u{1F446}/ }
end

describe '#sort_link with config set with custom down_arrow' do
before do
Ransack.configure do |c|
c.custom_arrows = { down_arrow: "\u{1F447}" }
end
end
after do
#set back to default
Ransack.configure do |c|
c.custom_arrows = { down_arrow: "&#9650;" }
end
end
subject { @controller.view_context
.sort_link(
[:main_app, Person.search(sorts: ['name asc'])],
:name,
controller: 'people',
hide_indicator: false
)
}
it { should match /Full Name&nbsp;\u{1F447}/ }
end

describe '#sort_link with config set to globally hide order indicators' do
before do
Ransack.configure { |c| c.hide_sort_order_indicators = true }
Expand Down