<commit_before>require 'will_paginate/per_page'
require 'will_paginate/page_number'
require 'will_paginate/collection'
require 'active_record'
begin
  require 'active_record/deprecated_finders'
rescue LoadError
  # only for Rails 4.1
end

module WillPaginate
  # = Paginating finders for ActiveRecord models
  # 
  # WillPaginate adds +paginate+, +per_page+ and other methods to
  # ActiveRecord::Base class methods and associations.
  # 
  # In short, paginating finders are equivalent to ActiveRecord finders; the
  # only difference is that we start with "paginate" instead of "find" and
  # that <tt>:page</tt> is required parameter:
  #
  #   @posts = Post.paginate :all, :page => params[:page], :order => 'created_at DESC'
  #
  module ActiveRecord
    # makes a Relation look like WillPaginate::Collection
    module RelationMethods
      include WillPaginate::CollectionMethods

      attr_accessor :current_page
      attr_writer :total_entries, :wp_count_options

      def per_page(value = nil)
        if value.nil? then limit_value
        else limit(value)
        end
      end

      # TODO: solve with less relation clones and code dups
      def limit(num)
        rel = super
        if rel.current_page
          rel.offset rel.current_page.to_offset(rel.limit_value).to_i
        else
          rel
        end
      end

      # dirty hack to enable `first` after `limit` behavior above
      def first(*args)
        if current_page
          rel = clone
          rel.current_page = nil
          rel.first(*args)
        else
          super
        end
      end

      # fix for Rails 3.0
      def find_last
        if !loaded? and offset_value || limit_value
          @last ||= to_a.last
        else
          super
        end
      end

      def offset(value = nil)
        if value.nil? then offset_value
        else super(value)
        end
      end

      def total_entries
        @total_entries ||= begin
          if loaded? and size < limit_value and (current_page == 1 or size > 0)
            offset_value + size
          else
            @total_entries_queried = true
            result = count
            result = result.size if result.respond_to?(:size) and !result.is_a?(Integer)
            result
          end
        end
      end

      def count
        if limit_value
          excluded = [:order, :limit, :offset, :reorder]
          excluded << :includes unless eager_loading?
          rel = self.except(*excluded)
          # TODO: hack. decide whether to keep
          rel = rel.apply_finder_options(@wp_count_options) if defined? @wp_count_options
          rel.count
        else
          super
        end
      end

      # workaround for Active Record 3.0
      def size
        if !loaded? and limit_value and group_values.empty?
          [super, limit_value].min
        else
          super
        end
      end

      # overloaded to be pagination-aware
      def empty?
        if !loaded? and offset_value
          result = count
          result = result.size if result.respond_to?(:size) and !result.is_a?(Integer)
          result <= offset_value
        else
          super
        end
      end<commit_msg>Accept arguments for Relation#count<commit_after>require 'will_paginate/per_page'
require 'will_paginate/page_number'
require 'will_paginate/collection'
require 'active_record'
begin
  require 'active_record/deprecated_finders'
rescue LoadError
  # only for Rails 4.1
end

module WillPaginate
  # = Paginating finders for ActiveRecord models
  # 
  # WillPaginate adds +paginate+, +per_page+ and other methods to
  # ActiveRecord::Base class methods and associations.
  # 
  # In short, paginating finders are equivalent to ActiveRecord finders; the
  # only difference is that we start with "paginate" instead of "find" and
  # that <tt>:page</tt> is required parameter:
  #
  #   @posts = Post.paginate :all, :page => params[:page], :order => 'created_at DESC'
  #
  module ActiveRecord
    # makes a Relation look like WillPaginate::Collection
    module RelationMethods
      include WillPaginate::CollectionMethods

      attr_accessor :current_page
      attr_writer :total_entries, :wp_count_options

      def per_page(value = nil)
        if value.nil? then limit_value
        else limit(value)
        end
      end

      # TODO: solve with less relation clones and code dups
      def limit(num)
        rel = super
        if rel.current_page
          rel.offset rel.current_page.to_offset(rel.limit_value).to_i
        else
          rel
        end
      end

      # dirty hack to enable `first` after `limit` behavior above
      def first(*args)
        if current_page
          rel = clone
          rel.current_page = nil
          rel.first(*args)
        else
          super
        end
      end

      # fix for Rails 3.0
      def find_last
        if !loaded? and offset_value || limit_value
          @last ||= to_a.last
        else
          super
        end
      end

      def offset(value = nil)
        if value.nil? then offset_value
        else super(value)
        end
      end

      def total_entries
        @total_entries ||= begin
          if loaded? and size < limit_value and (current_page == 1 or size > 0)
            offset_value + size
          else
            @total_entries_queried = true
            result = count
            result = result.size if result.respond_to?(:size) and !result.is_a?(Integer)
            result
          end
        end
      end

      def count(*args)
        if limit_value
          excluded = [:order, :limit, :offset, :reorder]
          excluded << :includes unless eager_loading?
          rel = self.except(*excluded)
          # TODO: hack. decide whether to keep
          rel = rel.apply_finder_options(@wp_count_options) if defined? @wp_count_options
          rel.count(*args)
        else
          super
        end
      end

      # workaround for Active Record 3.0
      def size
        if !loaded? and limit_value and group_values.empty?
          [super, limit_value].min
        else
          super
        end
      end

      # overloaded to be pagination-aware
      def empty?
        if !loaded? and offset_value
          result = count
          result = result.size if result.respond_to?(:size) and !result.is_a?(Integer)
          result <= offset_value
        else
          super
        end
      end
{'fraction_non_alphanumeric': 0.06902461826114054, 'fraction_numerical': 0.0024929884699283265, 'mean_word_length': 1.9690101757631822, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '6699934', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1784, 'n_words': 558}