Sha256: c5966e5407e15e34f25648b807c08997283abcf6b2b5b1f4919de49c34fd7f32

Contents?: true

Size: 1.31 KB

Versions: 46

Compression:

Stored size: 1.31 KB

Contents

module Agilibox::SortingHelper
  def sortable_column(name, column, options = {})
    unless column.is_a?(Symbol)
      raise ArgumentError, "invalid column, please use symbol"
    end

    current_column, current_direction = sortable_column_order

    if current_column == column
      if current_direction == :asc
        name           = "#{name} ↓"
        new_sort_param = "-#{column}"
      end

      if current_direction == :desc
        name           = "#{name} ↑"
        new_sort_param = column
      end

      klass = "sort #{current_direction}"
    else
      new_sort_param = column
      klass          = "sort"
    end

    unless (url_params = options.delete(:url_params))
      url_params = (params.try(:permit!) || params).to_h.symbolize_keys
    end
    url_params[:sort] = new_sort_param

    html_options = {class: klass}.merge(options)

    link_to(name, url_params, html_options)
  end

  def sortable_column_order(sort_param = params[:sort])
    sort_param = sort_param.to_s

    if sort_param.present?
      if sort_param.start_with?("-")
        column     = sort_param[1..-1].to_sym
        direction  = :desc
      else
        column     = sort_param.to_sym
        direction  = :asc
      end
    end

    if block_given?
      yield(column, direction)
    else
      [column, direction]
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
agilibox-1.9.4 app/helpers/agilibox/sorting_helper.rb
agilibox-1.9.3 app/helpers/agilibox/sorting_helper.rb
agilibox-1.9.1 app/helpers/agilibox/sorting_helper.rb
agilibox-1.9.0 app/helpers/agilibox/sorting_helper.rb
agilibox-1.8.0 app/helpers/agilibox/sorting_helper.rb
agilibox-1.7.4 app/helpers/agilibox/sorting_helper.rb
agilibox-1.7.3 app/helpers/agilibox/sorting_helper.rb
agilibox-1.7.2 app/helpers/agilibox/sorting_helper.rb
agilibox-1.7.1 app/helpers/agilibox/sorting_helper.rb
agilibox-1.7.0 app/helpers/agilibox/sorting_helper.rb
agilibox-1.6.2 app/helpers/agilibox/sorting_helper.rb
agilibox-1.6.1 app/helpers/agilibox/sorting_helper.rb
agilibox-1.6.0 app/helpers/agilibox/sorting_helper.rb
agilibox-1.5.13 app/helpers/agilibox/sorting_helper.rb
agilibox-1.5.12 app/helpers/agilibox/sorting_helper.rb
agilibox-1.5.11 app/helpers/agilibox/sorting_helper.rb
agilibox-1.5.10 app/helpers/agilibox/sorting_helper.rb
agilibox-1.5.9 app/helpers/agilibox/sorting_helper.rb
agilibox-1.5.8 app/helpers/agilibox/sorting_helper.rb
agilibox-1.5.7 app/helpers/agilibox/sorting_helper.rb