Sha256: 9fa2a549266543c4395f880c336db4cad6b3852446ba9cc307d6168b7ac769da

Contents?: true

Size: 1.16 KB

Versions: 37

Compression:

Stored size: 1.16 KB

Contents

module Ckeditor
  # Simple paginate relation
  class Paginatable
    attr_reader :limit_value, :offset_value

    def initialize(scope, options = {})
      @scope = scope
      @limit_value = (options[:limit] || Ckeditor.default_per_page).to_i
    end

    def page(num = 1)
      @offset_value = limit_value * ([num.to_i, 1].max - 1)
      self
    end

    def scoped
      @scope.limit(limit_value).offset(offset_value)
    end

    # Next page number in the collection
    def next_page
      current_page + 1 unless last_page?
    end

    # Previous page number in the collection
    def prev_page
      current_page - 1 unless first_page?
    end

    # First page of the collection?
    def first_page?
      current_page == 1
    end

    # Last page of the collection?
    def last_page?
      current_page >= total_pages
    end

    # Total number of pages
    def total_pages
      (total_count.to_f / limit_value).ceil
    end

    # total item numbers of scope
    def total_count
      @total_count ||= @scope.count
    end

    # Current page number
    def current_page
      offset = (offset_value < 0 ? 0 : offset_value)
      (offset / limit_value) + 1
    end
  end
end

Version data entries

37 entries across 37 versions & 4 rubygems

Version Path
glebtv-ckeditor-4.4.3.4 lib/ckeditor/paginatable.rb
glebtv-ckeditor-4.4.3.3 lib/ckeditor/paginatable.rb
glebtv-ckeditor-4.4.3.2 lib/ckeditor/paginatable.rb
glebtv-ckeditor-4.4.3.1 lib/ckeditor/paginatable.rb
glebtv-ckeditor-4.4.3.0 lib/ckeditor/paginatable.rb
glebtv-ckeditor-4.3.2.6 lib/ckeditor/paginatable.rb
ckeditor-4.1.0 lib/ckeditor/paginatable.rb
glebtv-ckeditor-4.3.2.5 lib/ckeditor/paginatable.rb
ckeditor-4.0.11 lib/ckeditor/paginatable.rb
glebtv-ckeditor-4.3.2.4 lib/ckeditor/paginatable.rb
glebtv-ckeditor-4.3.2.3 lib/ckeditor/paginatable.rb
glebtv-ckeditor-4.3.2.2 lib/ckeditor/paginatable.rb
glebtv-ckeditor-4.3.2.1 lib/ckeditor/paginatable.rb
glebtv-ckeditor-4.3.2.0 lib/ckeditor/paginatable.rb
ckeditor-4.0.10 lib/ckeditor/paginatable.rb
ckeditor-4.0.9 lib/ckeditor/paginatable.rb
ckeditor-4.0.8 lib/ckeditor/paginatable.rb