Module: ScopedSearch::RailsHelper

Defined in:
lib/scoped_search/rails_helper.rb

Instance Method Summary collapse

Instance Method Details



67
68
69
70
71
# File 'lib/scoped_search/rails_helper.rb', line 67

def a_link(name, href, html_options)
  tag_options = tag_options(html_options)
  link = "<a href=\"#{href}\"#{tag_options}>#{name}</a>"
  return link.respond_to?(:html_safe) ? link.html_safe : link
end

#sort(field, as: nil, default: "ASC", html_options: {}, url_options: params) ⇒ Object

Creates a link that alternates between ascending and descending.

Examples:

sort :username
sort :created_at, as: "Created"
sort :created_at, default: "DESC"
  • field - the name of the named scope. This helper will prepend this value with “ascend_by_” and “descend_by_”

This helper accepts the following options:

  • :as - the text used in the link, defaults to whatever is passed to `field`

  • :default - default sorting order, DESC or ASC

  • :html_options - is a hash of HTML options for the anchor tag

  • :url_options - is a hash of URL parameters, defaulting to `params`, to preserve the current URL parameters.

On Rails 5 or higher, parameter whitelisting prevents any parameter being used in a link by default, so `params.permit(..)` should be passed for `url_options` for all known and permitted URL parameters, e.g.

sort :username, url_options: params.permit(:search)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/scoped_search/rails_helper.rb', line 27

def sort(field, as: nil, default: "ASC", html_options: {}, url_options: params)

  unless as
    id = field.to_s.downcase == "id"
    as = id ? field.to_s.upcase : field.to_s.humanize
  end

  ascend  = "#{field} ASC"
  descend = "#{field} DESC"
  selected_sort = [ascend, descend].find { |o| o == params[:order] }

  case params[:order]
    when ascend
      new_sort = descend
    when descend
      new_sort = ascend
    else
      new_sort = ["ASC", "DESC"].include?(default) ? "#{field} #{default}" : ascend
  end

  unless selected_sort.nil?
    css_classes = html_options[:class] ? html_options[:class].split(" ") : []
    if selected_sort == ascend
      as = "&#9650;&nbsp;#{as}"
      css_classes << "ascending"
    else
      as = "&#9660;&nbsp;#{as}"
      css_classes << "descending"
    end
    html_options[:class] = css_classes.join(" ")
  end

  url_options = url_options.to_h if url_options.respond_to?(:permit)  # convert ActionController::Parameters if given
  url_options = url_options.merge(:order => new_sort)

  as = raw(as) if defined?(RailsXss)

  a_link(as, html_escape(url_for(url_options)),html_options)
end