<commit_before>module JsonCollectionPagination
  extend ActiveSupport::Concern
  PER_PAGE ||= 50
  SERIALIZER = nil

  def paginated_json_collection(collection, path_helper, sort_key, sort_order, offset, per_page, total, params)
    #changing from order to reorder to discard previous ordering
    sort_key = (sort_key.presence || :id).to_sym
    sort_order = sort_order.to_s == 'desc' ? :desc : :asc
    fail ArgumentError.new('Invalid sort_key') unless collection.column_names.include?(sort_key.to_s)
    collection = collection.reorder(sort_key => sort_order)

    # Meta
    offset = (offset.presence || 0).to_i
    include_total = (total == true || total == 'true')
    meta = {
        sort_key: sort_key,
        sort_order: sort_order,
        offset: offset
    }

    if [false, 'false', '∞'].include?(per_page)
      # allow pagination to be disabled
      # worst case: the query takes longer than 2 minutes and is
      # killed at the database and the load balancer
      meta[:per_page] = per_page
      data_on_page = collection
    else
      per_page = (per_page.presence || self.class::PER_PAGE).to_i
      meta[:per_page] = per_page
      # Get the current page of results.
      #  Add +1 to limit to see if there is a next page.
      #  This will be dropped in the return.
      data = collection.offset(offset).limit(per_page+1).to_a

      # Previous and next page
      if offset > 0
        meta[:prev] = path_helper.call(params.merge({
          sort_key: sort_key,
          sort_order: sort_order,
          offset: (offset - per_page) >= 0 ? (offset - per_page) : 0,
          per_page: per_page,
          total: total
        }))
      end
      if data.size > per_page
        meta[:next] = path_helper.call(params.merge({
          sort_key: sort_key,
          sort_order: sort_order,
          offset: offset + per_page,
          per_page: per_page,
          total: total
        }))
      end

      data_on_page = data[0...per_page]
    end

    if include_total
      meta[:total] = collection.count
    end

    # Return results + meta
    if self.class::SERIALIZER
      { json: data_on_page, meta: meta, each_serializer: self.class::SERIALIZER }
    else
      { json: data_on_page, meta: meta }
    end
  end

  def paginated_json_collection_new(collection, path_helper)
    collection = sort_collection(collection)

    # Meta
    per_page = sort_per_page
    offset = sort_offset
    include_total = sort_total
    meta = sort_meta

    if per_page
      data = collection.offset(offset).limit(per_page+1).to_a
      # Previous and next page
      if offset > 0
        meta[:prev] = path_helper.call(params.slice(*query_params).merge(meta).merge({
          offset: (offset - per_page) >= 0 ? (offset - per_page) : 0,
        }))
      end
      if data.size > per_page
        meta[:next] = path_helper.call(params.slice(*query_params).merge(meta).merge({
          offset: offset + per_page,
        }))
      end
      data_on_page = data[0...per_page]
    else
      data_on_page = collection
    end

    if include_total
      meta[:total] = collection.count
    end

    # Return results + meta
    if self.class::SERIALIZER
      { json: data_on_page, meta: meta, each_serializer: self.class::SERIALIZER }
    else
      { json: data_on_page, meta: meta }
    end
  end

  private

  def query_params
    ['asd']
  end

  def sort_key
    (params[:sort_key].presence || :id).to_sym
  end

  def sort_order
    params[:sort_order].to_s == 'desc' ? :desc : :asc
  end

  def sort_collection(collection)
    key = sort_key
    fail ArgumentError.new('Invalid sort_key') unless collection.column_names.include?(key.to_s)
    collection.reorder(key => sort_order)
  end

  def sort_meta
    {
      sort_key: sort_key,
      sort_order: sort_order,
      offset: sort_offset,
      per_page: sort_per_page
    }
  end

  def sort_offset
    (params[:offset].presence || 0).to_i
  end

  def sort_per_page
    return false if ['false', false, '∞'].include?(params[:per_page])
    (params[:per_page].presence || self.class::PER_PAGE).to_i
  end

  def sort_total
    AllowFiltering.to_boolean(params[:total])
  end

end
<commit_msg>Backwards compat<commit_after>module JsonCollectionPagination
  extend ActiveSupport::Concern
  PER_PAGE ||= 50
  SERIALIZER = nil

  def paginated_json_collection(collection, path_helper, sort_key, sort_order, offset, per_page, total, params)
    params ||= {}
    params[:sort_key] = sort_key
    params[:sort_order] = sort_order
    params[:offset] = offset
    params[:per_page] = per_page
    params[:total] = total
    self.params = params
    paginated_json_collection_new(collection)
  end

  def paginated_json_collection_new(collection, path_helper)
    collection = sort_collection(collection)

    # Meta
    per_page = sort_per_page
    offset = sort_offset
    include_total = sort_total
    meta = sort_meta

    if per_page
      data = collection.offset(offset).limit(per_page+1).to_a
      # Previous and next page
      if offset > 0
        meta[:prev] = path_helper.call(params.slice(*query_params).merge(meta).merge({
          offset: (offset - per_page) >= 0 ? (offset - per_page) : 0,
        }))
      end
      if data.size > per_page
        meta[:next] = path_helper.call(params.slice(*query_params).merge(meta).merge({
          offset: offset + per_page,
        }))
      end
      data_on_page = data[0...per_page]
    else
      data_on_page = collection
    end

    if include_total
      meta[:total] = collection.count
    end

    # Return results + meta
    if self.class::SERIALIZER
      { json: data_on_page, meta: meta, each_serializer: self.class::SERIALIZER }
    else
      { json: data_on_page, meta: meta }
    end
  end

  private

  def query_params
    params.slice()
  end

  def sort_key
    (params[:sort_key].presence || :id).to_sym
  end

  def sort_order
    params[:sort_order].to_s == 'desc' ? :desc : :asc
  end

  def sort_collection(collection)
    key = sort_key
    fail ArgumentError.new('Invalid sort_key') unless collection.column_names.include?(key.to_s)
    collection.reorder(key => sort_order)
  end

  def sort_meta
    {
      sort_key: sort_key,
      sort_order: sort_order,
      offset: sort_offset,
      per_page: sort_per_page
    }
  end

  def sort_offset
    (params[:offset].presence || 0).to_i
  end

  def sort_per_page
    return false if ['false', false, '∞'].include?(params[:per_page])
    (params[:per_page].presence || self.class::PER_PAGE).to_i
  end

  def sort_total
    AllowFiltering.to_boolean(params[:total])
  end

end

{'fraction_non_alphanumeric': 0.10284762193274766, 'fraction_numerical': 0.0036352620418055137, 'mean_word_length': 2.7538374076179646, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '975690', 'n_tokens_mistral': 2184, 'n_tokens_neox': 2129, 'n_words': 492}