Sha256: 2768464e327b7eeda10b091005f079fe2b9459e55d92f540e1cc85e53f992f13

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module SimplePaginate
  module ActiveRecordModelExtension
    extend ActiveSupport::Concern

    included do
      def self.paginate(options = {})
        options = options.dup
        page = options.delete(:page) || 1
        per_page = options.delete(:per_page) || SimplePaginate.config.default_per_page

        offset(per_page * (page - 1)).limit(per_page + 1).extending do
          def current_page
            (offset_value / (limit_value - 1)) + 1
          end

          def first_page?
            current_page == 1
          end

          def last_page?
            loaded? ? @actual_records.length < limit_value : count(:all) < limit_value
          end

          def per(num)
            if (n = num.to_i) < 0 || !(/^\d/ =~ num.to_s)
              self
            elsif n.zero?
              limit(n + 1)
            else
              limit(n + 1).offset(offset_value / (limit_value - 1) + 1)
            end
          end

          private

          def exec_queries
            @actual_records = super
            @records = if @actual_records.length > 1 && @actual_records.length == limit_value
                         @actual_records.first(limit_value - 1)
                       else
                         @actual_records
                       end
            @records
          end
        end
      end

      def self.page(num = nil)
        self.paginate(page: num)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_paginate-0.0.2 lib/simple_paginate/models/active_record_model_extension.rb