<commit_before>module Cylons
  module RemotePagination
    extend ::ActiveSupport::Concern

    META_METHODS = [
      :total_pages,
      :current_page
    ].freeze

    SEARCH_OPTION_KEYS = [:opts, :options].freeze
    MAX_PER_PAGE = 1000

    module ClassMethods
      def search(params = {})
        search_options = params.extract!(*SEARCH_OPTION_KEYS)

        search_options.delete_if {|k,v| v.nil? }

        query_scope = params.inject(all) do |query, hash_pair|
          query.__send__("by_#{hash_pair[0]}", hash_pair[1]).all
        end.all

        if search_options.present?
          paginated_results = query_scope.paginate(:page => search_options[:options][:page], :per_page => search_options[:options][:per_page])

          # with_pagination_meta(paginated_result)

        else
          paginated_results = query_scope.paginate(:page => 1, :per_page => MAX_PER_PAGE)

          # binding.pry

          paginated_results

          # with_pagination_meta(paginated_results)
        end
      end

      def with_pagination_meta(result)
        META_METHODS.each do |method|
          result.instance_variable_set("@#{method.to_s}", result.__send__(method))
        end

        result
        # result.inject(META_METHODS) do |result_array, method|
        #   result_array.instance_variable_set("@#{method.to_s}", result_array.__send__(method))
        #   result_array
        # end
        #
        # result
      end
    end


<commit_msg>move pagination to its own module<commit_after>module Cylons
  module RemotePagination
    extend ::ActiveSupport::Concern

    META_METHODS = [
      :total_pages,
      :current_page
    ].freeze

    SEARCH_OPTION_KEYS = [:opts, :options].freeze
    MAX_PER_PAGE = 1000

    module ClassMethods
      def search(params = {})
        search_options = params.extract!(*SEARCH_OPTION_KEYS)

        search_options.delete_if {|k,v| v.nil? }

        query_scope = params.inject(all) do |query, hash_pair|
          query.__send__("by_#{hash_pair[0]}", hash_pair[1]).all
        end.all

        if search_options.present?
          query_scope.paginate(:page => search_options[:options][:page], :per_page => search_options[:options][:per_page])
        else
          query_scope.paginate(:page => 1, :per_page => MAX_PER_PAGE)
        end
      end

    end
{'fraction_non_alphanumeric': 0.10723514211886305, 'fraction_numerical': 0.006029285099052541, 'mean_word_length': 2.675632911392405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '2339585', 'n_tokens_mistral': 753, 'n_tokens_neox': 728, 'n_words': 122}