-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
858 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ script: bundle exec rspec | |
env: | ||
- PAGINATOR=kaminari | ||
- PAGINATOR=will_paginate | ||
- PAGINATOR=cursor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'cursor/active_record_model_extension' | ||
|
||
module Cursor | ||
module ActiveRecordExtension | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
# Future subclasses will pick up the model extension | ||
def inherited(kls) #:nodoc: | ||
super | ||
kls.send(:include, Cursor::ActiveRecordModelExtension) if kls.superclass == ::ActiveRecord::Base | ||
end | ||
end | ||
|
||
included do | ||
# Existing subclasses pick up the model extension as well | ||
self.descendants.each do |kls| | ||
kls.send(:include, Cursor::ActiveRecordModelExtension) if kls.superclass == ::ActiveRecord::Base | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require_relative 'config' | ||
require_relative 'configuration_methods' | ||
require_relative 'page_scope_methods' | ||
|
||
module Cursor | ||
module ActiveRecordModelExtension | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
cattr_accessor :_current_cursor, :_base_relation, :_per_page | ||
end | ||
|
||
included do | ||
self.send(:include, Cursor::ConfigurationMethods) | ||
|
||
def self.cursor_page(options = {}) | ||
opts = options.dup | ||
(opts || {}).to_hash.symbolize_keys! | ||
opts[:direction] = :after if opts[:after].present? | ||
opts[:direction] ||= :before if opts[:before].present? | ||
opts[:direction] ||= :after | ||
|
||
self._current_cursor = opts[opts[:direction]] && opts[opts[:direction]].to_i | ||
self._base_relation = self | ||
self._per_page = opts[:per_page].to_i | ||
on_cursor(opts[:direction]). | ||
in_direction(opts[:direction]). | ||
limit(opts[:per_page] || default_per_page). | ||
extending(Cursor::PageScopeMethods) | ||
end | ||
|
||
def self.on_cursor(direction) | ||
if _current_cursor.nil? | ||
where(nil) | ||
else | ||
where(["#{self.table_name}.id #{direction == :after ? '>' : '<'} ?", _current_cursor]) | ||
end | ||
end | ||
|
||
def self.in_direction(direction) | ||
reorder("#{self.table_name}.id #{direction == :after ? 'ASC' : 'DESC'}") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'active_support/configurable' | ||
|
||
module Cursor | ||
# Configures global settings for Divination | ||
# Cursor.configure do |config| | ||
# config.default_per_page = 10 | ||
# end | ||
def self.configure(&block) | ||
yield @config ||= Cursor::Configuration.new | ||
end | ||
|
||
# Global settings for Cursor | ||
def self.config | ||
@config | ||
end | ||
|
||
class Configuration #:nodoc: | ||
include ActiveSupport::Configurable | ||
config_accessor :default_per_page | ||
config_accessor :max_per_page | ||
|
||
def param_name | ||
config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name | ||
end | ||
end | ||
|
||
configure do |config| | ||
config.default_per_page = 25 | ||
config.max_per_page = nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Cursor | ||
module ConfigurationMethods | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
# Overrides the default +per_page+ value per model | ||
# class Article < ActiveRecord::Base | ||
# paginates_per 10 | ||
# end | ||
def paginates_per(val) | ||
@_default_per_page = val | ||
end | ||
|
||
# This model's default +per_page+ value | ||
# returns +default_per_page+ value unless explicitly overridden via <tt>paginates_per</tt> | ||
def default_per_page | ||
(defined?(@_default_per_page) && @_default_per_page) || Cursor.config.default_per_page | ||
end | ||
|
||
# Overrides the max +per_page+ value per model | ||
# class Article < ActiveRecord::Base | ||
# max_paginates_per 100 | ||
# end | ||
def max_paginates_per(val) | ||
@_max_per_page = val | ||
end | ||
|
||
# This model's max +per_page+ value | ||
# returns +max_per_page+ value unless explicitly overridden via <tt>max_paginates_per</tt> | ||
def max_per_page | ||
(defined?(@_max_per_page) && @_max_per_page) || Cursor.config.max_per_page | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
module Cursor | ||
module PageScopeMethods | ||
def per(num) | ||
if (n = num.to_i) <= 0 | ||
self | ||
elsif max_per_page && max_per_page < n | ||
limit(max_per_page) | ||
else | ||
limit(n) | ||
end | ||
end | ||
|
||
def next_cursor | ||
@_next_cursor ||= last.try!(:id) | ||
end | ||
|
||
def prev_cursor | ||
@_prev_cursor ||= first.try!(:id) | ||
end | ||
|
||
def first_cursor | ||
@_first_cursor ||= _base_relation.limit(limit_value).reorder('id ASC').first.try!(:id) | ||
end | ||
|
||
def last_cursor | ||
@_last_cursor ||= begin | ||
last_id = _base_relation.limit(limit_value).reorder('id DESC').last.try!(:id) | ||
return nil unless last_id | ||
last_id - 1 | ||
end | ||
end | ||
|
||
def total_count | ||
@_total_count ||= _base_relation.count | ||
end | ||
|
||
def just_one_page? | ||
total_count <= _per_page | ||
end | ||
|
||
def first_page? | ||
_current_cursor.nil? || _current_cursor == first_cursor | ||
end | ||
|
||
def last_page? | ||
_current_cursor && _current_cursor == last_cursor | ||
end | ||
|
||
def next_url(request_url) | ||
direction == :after ? | ||
after_url(request_url, next_cursor) : | ||
before_url(request_url, next_cursor) | ||
end | ||
|
||
def prev_url(request_url) | ||
direction == :after ? | ||
before_url(request_url, prev_cursor) : | ||
after_url(request_url, prev_cursor) | ||
end | ||
|
||
def last_url(request_url) | ||
after_url(request_url, last_cursor) | ||
end | ||
|
||
def first_url(request_url) | ||
after_url(request_url, nil) | ||
end | ||
|
||
def before_url(request_url, cursor) | ||
base, params = url_parts(request_url) | ||
params.merge!('before' => cursor) unless cursor.nil? | ||
params.to_query.length > 0 ? "#{base}?#{CGI.unescape(params.to_query)}" : base | ||
end | ||
|
||
def after_url(request_url, cursor) | ||
base, params = url_parts(request_url) | ||
params.merge!('after' => cursor) unless cursor.nil? | ||
params.to_query.length > 0 ? "#{base}?#{CGI.unescape(params.to_query)}" : base | ||
end | ||
|
||
def url_parts(request_url) | ||
base, params = request_url.split('?', 2) | ||
params = Rack::Utils.parse_nested_query(params || '') | ||
params.stringify_keys! | ||
params.delete('before') | ||
params.delete('after') | ||
[base, params] | ||
end | ||
|
||
def direction | ||
return :after if prev_cursor.nil? && next_cursor.nil? | ||
@_direction ||= prev_cursor < next_cursor ? :after : :before | ||
end | ||
|
||
def pagination(request_url) | ||
return {} if just_one_page? | ||
{}.tap do |h| | ||
unless first_page? | ||
h[:prev] = prev_url(request_url) unless prev_cursor.nil? | ||
h[:first] = first_url(request_url) unless first_cursor.nil? | ||
end | ||
|
||
unless last_page? | ||
h[:next] = next_url(request_url) unless next_cursor.nil? | ||
h[:last] = last_url(request_url) unless last_cursor.nil? | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.