Module: ScopedSearch::RailsHelper
- Defined in:
- lib/scoped_search/rails_helper.rb
Instance Method Summary collapse
-
#sort(field, as: nil, default: "ASC", html_options: {}, url_options: params) ⇒ Object
Creates a link that alternates between ascending and descending.
Instance Method Details
#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 = [:class] ? [:class].split(" ") : [] if selected_sort == ascend as = "▲ ".html_safe + as css_classes << "ascending" else as = "▼ ".html_safe + as css_classes << "descending" end [:class] = css_classes.join(" ") end = .to_h if .respond_to?(:permit) # convert ActionController::Parameters if given = .merge(:order => new_sort) as = raw(as) if defined?(RailsXss) content_tag(:a, as, .merge(href: url_for())) end |