1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

📝 Documentation

This commit is contained in:
Akira Matsuda 2016-12-08 12:31:07 +09:00
parent feab7a152c
commit 3a6afa6a8d
5 changed files with 22 additions and 8 deletions

View file

@ -5,7 +5,7 @@ module Kaminari
module ActiveRecordExtension
extend ActiveSupport::Concern
module ClassMethods
module ClassMethods #:nodoc:
# Future subclasses will pick up the model extension
def inherited(kls) #:nodoc:
super

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
module Kaminari
# Active Record specific page scope methods implementations
module ActiveRecordRelationMethods
# Used for page_entry_info
def entry_name(options = {})
default = options[:count] == 1 ? model_name.human : model_name.human.pluralize
model_name.human(options.reverse_merge(default: default))
@ -35,12 +37,19 @@ module Kaminari
end
end
# Turn this Relation to a "without count mode" Relation.
# Note that the "without count mode" is supposed to be performant but has a feature limitation.
# Pro: paginates without casting an extra SELECT COUNT query
# Con: unable to know the total number of records/pages
def without_count
extend ::Kaminari::PaginatableWithoutCount
end
end
# A module that makes AR::Relation paginatable without having to cast another SELECT COUNT query
module PaginatableWithoutCount
# Overwrite AR::Relation#load to actually load one more record to judge if the page has next page
# then store the result in @_has_next ivar
def load
if loaded? || limit_value.nil?
super
@ -59,10 +68,12 @@ module Kaminari
end
end
# The page wouldn't be the last page if there's "limit + 1" record
def last_page?
!out_of_range? && !@_has_next
end
# Empty relation needs no pagination
def out_of_range?
load unless loaded?
@records.empty?

View file

@ -144,11 +144,13 @@ module Kaminari
(@options[:current_page] - @page).abs <= @options[:window]
end
# Current page is an isolated gap or not
def single_gap?
((@page == @options[:current_page] - @options[:window] - 1) && (@page == @options[:left] + 1)) ||
((@page == @options[:current_page] + @options[:window] + 1) && (@page == @options[:total_pages] - @options[:right]))
end
# The page number exceeds the range of pages or not
def out_of_range?
@page > @options[:total_pages]
end
@ -158,23 +160,23 @@ module Kaminari
@last.is_a? Gap
end
def to_i
def to_i #:nodoc:
number
end
def to_s
def to_s #:nodoc:
number.to_s
end
def +(other)
def +(other) #:nodoc:
to_i + other.to_i
end
def -(other)
def -(other) #:nodoc:
to_i - other.to_i
end
def <=>(other)
def <=>(other) #:nodoc:
to_i <=> other.to_i
end
end

View file

@ -32,6 +32,7 @@ module Kaminari
super(original_array || [])
end
# Used for page_entry_info
def entry_name(options = {})
I18n.t('helpers.page_entries_info.entry', options.reverse_merge(default: ENTRY.pluralize(options[:count])))
end

View file

@ -1,8 +1,8 @@
# frozen_string_literal: true
module Kaminari
module ConfigurationMethods
module ConfigurationMethods #:nodoc:
extend ActiveSupport::Concern
module ClassMethods
module ClassMethods #:nodoc:
# Overrides the default +per_page+ value per model
# class Article < ActiveRecord::Base
# paginates_per 10