Module: Binxtils::SortableHelper
- Defined in:
- lib/binxtils/sortable_helper.rb
Constant Summary collapse
- BASE_SEARCH_KEYS =
[ :direction, :sort, # sorting params :period, :start_time, :end_time, :render_chart, # Time period params :user_id, :query, :per_page # General search params ].freeze
Instance Method Summary collapse
- #default_search_keys ⇒ Object
-
#sort_column ⇒ Object
Set defaults, required for testing.
- #sort_direction ⇒ Object
- #sortable(column, title = nil, html_options = {}, &block) ⇒ Object
- #sortable_params ⇒ Object
- #sortable_search_params ⇒ Object
- #sortable_search_params?(except: []) ⇒ Boolean
Instance Method Details
#default_search_keys ⇒ Object
15 16 17 |
# File 'lib/binxtils/sortable_helper.rb', line 15 def default_search_keys BASE_SEARCH_KEYS end |
#sort_column ⇒ Object
Set defaults, required for testing
12 |
# File 'lib/binxtils/sortable_helper.rb', line 12 def sort_column = "id" |
#sort_direction ⇒ Object
13 |
# File 'lib/binxtils/sortable_helper.rb', line 13 def sort_direction = "desc" |
#sortable(column, title = nil, html_options = {}, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/binxtils/sortable_helper.rb', line 19 def sortable(column, title = nil, = {}, &block) if title.is_a?(Hash) # If title is a hash, it wasn't passed = title title = nil end title ||= column.gsub(/_(id|at)\z/, "").titleize # Check for render_sortable - otherwise default to rendering render_sortable = .key?(:render_sortable) ? [:render_sortable] : ![:skip_sortable] return title unless render_sortable [:class] = "#{[:class]} sortable-link" direction = (column == sort_column && sort_direction == "desc") ? "asc" : "desc" if column == sort_column [:class] += " active" span_content = (direction == "asc") ? "\u2193" : "\u2191" end link_to(sortable_url(column, direction), ) do concat(block_given? ? capture(&block) : title.html_safe) concat(content_tag(:span, span_content, class: "sortable-direction")) end end |
#sortable_params ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/binxtils/sortable_helper.rb', line 54 def sortable_params @sortable_params ||= sortable_search_params.as_json.filter_map do |k, v| next if v.blank? || k == "sort" && v == default_column || k == "direction" && v == default_direction [k, v] end.to_h.with_indifferent_access end |
#sortable_search_params ⇒ Object
62 63 64 65 66 67 |
# File 'lib/binxtils/sortable_helper.rb', line 62 def sortable_search_params return @sortable_search_params if defined?(@sortable_search_params) search_param_keys = params.keys.select { |k| k.to_s.start_with?("search_") } # match params starting with search_ @sortable_search_params = params.permit(*(default_search_keys | search_param_keys)) end |
#sortable_search_params?(except: []) ⇒ Boolean
44 45 46 47 48 49 50 51 52 |
# File 'lib/binxtils/sortable_helper.rb', line 44 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 |