<commit_before>require 'listings/configuration_methods'
require 'listings/view_helper_methods'
require 'csv'

module Listings
  class Base
    include Listings::ConfigurationMethods
    include Listings::ViewHelperMethods
    include ERB::Util # html_escape methods are private in the view_context
                      # so they need to be included in order to be available

    attr_accessor :view_context
    attr_accessor :params

    def initialize
      @page_size = self.class.page_size
    end

    def name
      self.class.name.underscore.sub(/_listing$/,'')
    end

    def param_scope; :scope; end
    def param_page; :page; end
    def param_search; :s; end
    def param_sort_by; :sort_by; end
    def param_sort_direction; :sort_d; end

    def filter_items(params, items)
      self.page = params[param_page] || 1
      self.scope = scope_by_name(params[param_scope])
      self.search = params[param_search]

      items = scope.apply(self, items) unless scope.nil?

      if search.present? && self.searchable?
        criteria = []
        values = []
        self.columns.select(&:searchable?).each do |col|
          criteria << "#{model_class.table_name}.#{col.name} like ?"
          values << "%#{search}%"
        end
        items = items.where(criteria.join(' or '), *values)
      end

      if params.include?(param_sort_by)
        sort_col = column_with_name(params[param_sort_by])
        sort_col.sort = params[param_sort_direction]
        items = items.reorder(sort_col.sort_by.to_sym => params[param_sort_direction].to_sym)
      end

      if paginated?
        items = items.page(page).per(page_size)
      end

      if items.is_a?(Class)
        items = items.all
      end

      items
    end

    def query_items(params)
      @params = params
      items = self.model_class<commit_msg>revert reorder to use string arguments.<commit_after>require 'listings/configuration_methods'
require 'listings/view_helper_methods'
require 'csv'

module Listings
  class Base
    include Listings::ConfigurationMethods
    include Listings::ViewHelperMethods
    include ERB::Util # html_escape methods are private in the view_context
                      # so they need to be included in order to be available

    attr_accessor :view_context
    attr_accessor :params

    def initialize
      @page_size = self.class.page_size
    end

    def name
      self.class.name.underscore.sub(/_listing$/,'')
    end

    def param_scope; :scope; end
    def param_page; :page; end
    def param_search; :s; end
    def param_sort_by; :sort_by; end
    def param_sort_direction; :sort_d; end

    def filter_items(params, items)
      self.page = params[param_page] || 1
      self.scope = scope_by_name(params[param_scope])
      self.search = params[param_search]

      items = scope.apply(self, items) unless scope.nil?

      if search.present? && self.searchable?
        criteria = []
        values = []
        self.columns.select(&:searchable?).each do |col|
          criteria << "#{model_class.table_name}.#{col.name} like ?"
          values << "%#{search}%"
        end
        items = items.where(criteria.join(' or '), *values)
      end

      if params.include?(param_sort_by)
        sort_col = column_with_name(params[param_sort_by])
        sort_col.sort = params[param_sort_direction]
        items = items.reorder("#{sort_col.sort_by} #{params[param_sort_direction]}")
      end

      if paginated?
        items = items.page(page).per(page_size)
      end

      if items.is_a?(Class)
        items = items.all
      end

      items
    end

    def query_items(params)
      @params = params
      items = self.model_class
{'fraction_non_alphanumeric': 0.09817786238781616, 'fraction_numerical': 0.0005439216752787598, 'mean_word_length': 2.685370741482966, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 4, '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': '4292175', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1110, 'n_words': 262}