-
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
808 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,41 @@ | ||
require_relative 'config' | ||
require_relative 'configuration_methods' | ||
require_relative 'page_scope_methods' | ||
|
||
module Cursor | ||
module ActiveRecordModelExtension | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
cattr_accessor :total_count | ||
end | ||
|
||
included do | ||
self.send(:include, Cursor::ConfigurationMethods) | ||
|
||
def self.cursor_page(options = {}) | ||
(options || {}).to_hash.symbolize_keys! | ||
options[:direction] = options.keys.include?(:after) ? :after : :before | ||
|
||
cursor_id = options[options[:direction]] | ||
self.total_count = self.count | ||
on_cursor(cursor_id, options[:direction]). | ||
in_direction(options[:direction]). | ||
limit(options[:per_page] || default_per_page). | ||
extending(Cursor::PageScopeMethods) | ||
end | ||
|
||
def self.on_cursor(cursor_id, direction) | ||
if cursor_id.nil? | ||
where(nil) | ||
else | ||
where(["#{self.table_name}.id #{direction == :after ? '>' : '<'} ?", cursor_id]) | ||
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,66 @@ | ||
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 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 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) | ||
{}.tap do |h| | ||
h[:prev] = prev_url(request_url) unless prev_cursor.nil? | ||
h[:next] = next_url(request_url) unless next_cursor.nil? | ||
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
Oops, something went wrong.