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 functionality to hide all search indicator arrows via config #577

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 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.

Please use markdown ruby code format block for the config code example (3 backticks + 'ruby' ... code ... 3 backticks).

  code

```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
7 changes: 6 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
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps call it :hide_sort_order_indicators, WDYT?


def configure
Expand Down Expand Up @@ -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
9 changes: 3 additions & 6 deletions lib/ransack/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
31 changes: 29 additions & 2 deletions spec/ransack/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ module Helpers
hide_indicator: true
)
}
it { should match /Full Name/ }
it { should_not match /&#9660;|&#9650;/ }
end

describe '#sort_link with hide order indicator set to false' do
Expand All @@ -358,6 +358,34 @@ module Helpers
it { should match /Full Name&nbsp;&#9660;/ }
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 /&#9660;|&#9650;/ }
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&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