Sha256: da9a8885687615f2af95ec673e0fad12b24c966c2821f2b80fb9f6181861d7a5

Contents?: true

Size: 931 Bytes

Versions: 1

Compression:

Stored size: 931 Bytes

Contents

require "simple_paginator/version"
require 'active_record'

module SimplePaginator
  DEFAULT_PER_PAGE = 25
  DEFAULT_MAX_PAGE = 10

  def self.included(base)
    base.extend ClassMethods

    base.class_eval do
      per_page DEFAULT_PER_PAGE
      max_page DEFAULT_MAX_PAGE
    end
  end

  module ClassMethods
    def paged(page, options = {})
      num = page_number(page)
      per_page = options[:per_page].presence || @per_page
      max_page = options[:max_page].presence || @max_page

      if num > max_page
        scoped = none
      elsif num == max_page
        scoped = limit(per_page).offset((num-1)*per_page)
      else
        scoped = limit(per_page+1).offset((num-1)*per_page)
      end
      scoped
    end

    def page_number(raw_page)
      [raw_page.to_i, 1].max
    end

    def per_page(per_page)
      @per_page = per_page
    end

    def max_page(max_page)
      @max_page = max_page
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_paginator-1.0.0 lib/simple_paginator.rb