Otineb
Otineb

Reputation: 31

How to edit output from Ransack's "sort_link" method?

As a total noob to Ruby on Rails I have, what I hope, is a fairly simple question.

I'm using the Ransack gem to power my site's search bar and there are a bunch of great methods. One of them is the sort_link method that makes my table of results sortable by clicking on the element.

The only problem is that when it sorts, it injects  ▼ (or  ▲) after the column header name.

All I'm trying to do is edit this output to be <span>▲</span> or <span>▼</span> instead. After looking everywhere and even asking the question in #rubyonrails IRC I can't seem to find a way customize this output.

As a stop-gap, i've written some javascript to directly manipulate the DOM but it just feels like a dirty approach, especially in a custom site!

Any help here would be awesome.

Thanks!

Upvotes: 1

Views: 1353

Answers (1)

Substantial
Substantial

Reputation: 6682

Here they are in the Ransack gem source on GitHub

# lib/ransack/constants.rb:5-6

module Ransack
  module Constants
    ASC = 'asc'.freeze
    DESC = 'desc'.freeze
    ASC_ARROW = '&#9650;'.freeze   # <- ascending arrow
    DESC_ARROW = '&#9660;'.freeze  # <- descending arrow
# ..snipped..

To swap arrow directions, copy the file into your application here: /lib/ransack/constants.rb and swap the two HTML entities (or simply swap ASC_ARROW and DESC_ARROW).


EDIT

To add a span tag and remove the non-breaking space, we will modify the helper method that handles both.

In this file we will take this helper method

# lib/ransack/helpers/form_helper.rb:143
def link_name(label_text, dir)
  [ERB::Util.h(label_text), order_indicator_for(dir)]
  .compact
  .join(Ransack::Constants::NON_BREAKING_SPACE)
  .html_safe
end

and modify it to:

  • Wrap the order indicator arrow in a span tag
  • Remove nbsp;

Copy the raw file to your application (to /lib/ransack/helpers/form_helper.rb) and change this method to look like:

# lib/ransack/helpers/form_helper.rb:143
def link_name(label_text, dir)
  [ERB::Util.h(label_text), content_tag(:span, order_indicator_for(dir))]
  .compact
  .html_safe
end

Upvotes: 1

Related Questions