<commit_before># -*- coding: utf-8 -*-

module SortablesHelper
  def sort_query(valid_fields, query_object, replacements = {})
    valid_fields = valid_fields.map { |elem| elem.to_sym }
    @_sort_column = params[:sort] || valid_fields.first
    @_sort_direction =  params[:dir] || :asc

    @_sort_direction = :asc unless [:asc, :desc].include?(@_sort_direction.to_sym)
    @_sort_direction = @_sort_direction.to_sym
    @_sort_column = valid_fields.first unless valid_fields.include?(@_sort_column.to_sym)
    @_sort_column = @_sort_column.to_sym

    order_name = if replacements and replacements[@_sort_column]
                   replacements[@_sort_column]
                 else
                   @_sort_column
                 end

    return query_object.order("#{order_name} #{@_sort_direction}")
  end

  def sortable(colname, col, method)
    html = ERB::Util.h(colname) + ' ' # TODO use a better method to escape
    html << link_to('↓', self.send(method, sort: col, dir: 'asc'))
    html << link_to('↑', self.send(method, sort: col, dir: 'desc'))
    html.html_safe
  end

  def sort_column
    @_sort_column
  end

  def sort_direction
    @_sort_direction
  end

end

# eof<commit_msg>feat: now the path helper can also be a lambda/proc<commit_after># -*- coding: utf-8 -*-

module SortablesHelper
  def sort_query(valid_fields, query_object, replacements = {})
    valid_fields = valid_fields.map { |elem| elem.to_sym }
    @_sort_column = params[:sort] || valid_fields.first
    @_sort_direction =  params[:dir] || :asc

    @_sort_direction = :asc unless [:asc, :desc].include?(@_sort_direction.to_sym)
    @_sort_direction = @_sort_direction.to_sym
    @_sort_column = valid_fields.first unless valid_fields.include?(@_sort_column.to_sym)
    @_sort_column = @_sort_column.to_sym

    order_name = if replacements and replacements[@_sort_column]
                   replacements[@_sort_column]
                 else
                   @_sort_column
                 end

    return query_object.order("#{order_name} #{@_sort_direction}")
  end

  def sortable(colname, col, method)
    html = ERB::Util.h(colname) + ' ' # TODO use a better method to escape

    if method.is_a?(Proc)
      link_asc = method.call(col, 'asc')
      link_desc = method.call(col, 'desc')
    else
      link_asc = self.send(method, sort: col, dir: 'asc')
      link_desc = self.send(method, sort: col, dir: 'desc')
    end

    html << link_to('↓', link_asc)
    html << link_to('↑', link_desc)
    html.html_safe
  end

  def sort_column
    @_sort_column
  end

  def sort_direction
    @_sort_direction
  end

end

# eof
{'fraction_non_alphanumeric': 0.11903853491033957, 'fraction_numerical': 0.0007630675314765357, 'mean_word_length': 3.096875, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6062604', 'n_tokens_mistral': 892, 'n_tokens_neox': 870, 'n_words': 190}