Module: TranzitoUtils::SortableHelper

Included in:
Helpers
Defined in:
lib/tranzito_utils/helpers/sortable_helper.rb

Constant Summary collapse

DEFAULT_SEARCH_KEYS =
i[direction sort period start_time end_time per_page user_id query].freeze

Instance Method Summary collapse

Instance Method Details

#sortable(column, title = nil, html_options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tranzito_utils/helpers/sortable_helper.rb', line 7

def sortable(column, title = nil, html_options = {})
  if title.is_a?(Hash) # If title is a hash, it wasn't passed
    html_options = title
    title = nil
  end

  title ||= column.gsub(/_(id|at)\z/, "").titleize
  # Check for render_sortable - otherwise default to rendering
  render_sortable = html_options.key?(:render_sortable) ? html_options[:render_sortable] : !html_options[:skip_sortable]
  return title unless render_sortable

  html_options[:class] = "#{html_options[:class]} sortable-link"
  direction = (column == sort_column && sort_direction == "desc") ? "asc" : "desc"
  if column == sort_column
    html_options[:class] += " active"
    span_content = (direction == "asc") ? "\u2193" : "\u2191"
  end

  link_to(sortable_search_params.merge(sort: column, direction: direction), html_options) do
    concat(title.html_safe)
    concat((:span, span_content, class: "sortable-direction"))
  end
end

#sortable_search_paramsObject



31
32
33
34
# File 'lib/tranzito_utils/helpers/sortable_helper.rb', line 31

def sortable_search_params
  search_param_keys = params.keys.select { |k| k.to_s.start_with?("search_") }
  params.permit(*(DEFAULT_SEARCH_KEYS | TranzitoUtils::DEFAULT[:additional_search_keys] | search_param_keys))
end

#sortable_search_params?(except: []) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'lib/tranzito_utils/helpers/sortable_helper.rb', line 40

def sortable_search_params?(except: [])
  except_keys = i[direction sort period per_page] + except
  s_params = sortable_search_params.except(*except_keys).values.reject(&:blank?).any?

  return true if s_params
  return false if except.map(&:to_s).include?("period")
  params[:period].present? && params[:period] != "all"
end

#sortable_search_params_without_sortObject



36
37
38
# File 'lib/tranzito_utils/helpers/sortable_helper.rb', line 36

def sortable_search_params_without_sort
  sortable_search_params.except(:direction, :sort)
end