work
This commit is contained in:
6
lib/danbooru/paginator.rb
Normal file
6
lib/danbooru/paginator.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
require "danbooru/paginator/active_record_extension"
|
||||
require "danbooru/paginator/collection_extension"
|
||||
require "danbooru/paginator/numbered_collection_extension"
|
||||
require "danbooru/paginator/sequential_collection_extension"
|
||||
|
||||
ActiveRecord::Base.__send__(:include, Danbooru::Paginator::ActiveRecordExtension)
|
||||
68
lib/danbooru/paginator/active_record_extension.rb
Normal file
68
lib/danbooru/paginator/active_record_extension.rb
Normal file
@@ -0,0 +1,68 @@
|
||||
require 'active_support/concern'
|
||||
|
||||
module Danbooru
|
||||
module Paginator
|
||||
module ActiveRecordExtension
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
def paginate(page)
|
||||
if use_sequential_paginator?(page)
|
||||
paginate_sequential(page)
|
||||
else
|
||||
paginate_numbered(page)
|
||||
end
|
||||
end
|
||||
|
||||
def use_sequential_paginator?(page)
|
||||
page =~ /[ab]\d+/i
|
||||
end
|
||||
|
||||
def paginate_sequential(page)
|
||||
if page =~ /b(\d+)/
|
||||
paginate_sequential_before($1)
|
||||
elsif page =~ /a(\d+)/
|
||||
paginate_sequential_after($1)
|
||||
else
|
||||
paginate_numbered(page)
|
||||
end
|
||||
end
|
||||
|
||||
def paginate_sequential_before(before_id)
|
||||
limit(records_per_page).where("id < ?", before_id.to_i).reorder("id desc").tap do |obj|
|
||||
obj.extend(SequentialCollectionExtension)
|
||||
obj.sequential_paginator_mode = :before
|
||||
end
|
||||
end
|
||||
|
||||
def paginate_sequential_after(after_id)
|
||||
limit(records_per_page).where("id > ?", after_id.to_i).reorder("id asc").tap do |obj|
|
||||
obj.extend(SequentialCollectionExtension)
|
||||
obj.sequential_paginator_mode = :after
|
||||
end
|
||||
end
|
||||
|
||||
def paginate_numbered(page)
|
||||
page = [page.to_i, 1].max
|
||||
limit(records_per_page).offset((page - 1) * records_per_page).tap do |obj|
|
||||
obj.extend(NumberedCollectionExtension)
|
||||
obj.total_pages = (obj.total_count / records_per_page.to_f).ceil
|
||||
obj.current_page = page
|
||||
end
|
||||
end
|
||||
|
||||
def records_per_page
|
||||
Danbooru.config.posts_per_page
|
||||
end
|
||||
|
||||
# taken from kaminari (https://github.com/amatsuda/kaminari)
|
||||
def total_count
|
||||
c = except(:offset, :limit, :order)
|
||||
c = c.reorder(nil)
|
||||
c = c.count
|
||||
c.respond_to?(:count) ? c.count : c
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
6
lib/danbooru/paginator/collection_extension.rb
Normal file
6
lib/danbooru/paginator/collection_extension.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
module Danbooru
|
||||
module Paginator
|
||||
module CollectionExtension
|
||||
end
|
||||
end
|
||||
end
|
||||
27
lib/danbooru/paginator/numbered_collection_extension.rb
Normal file
27
lib/danbooru/paginator/numbered_collection_extension.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
module Danbooru
|
||||
module Paginator
|
||||
module NumberedCollectionExtension
|
||||
attr_accessor :current_page, :total_pages
|
||||
|
||||
def self.extended(obj)
|
||||
obj.extend(Danbooru::Paginator::CollectionExtension)
|
||||
end
|
||||
|
||||
def is_first_page?
|
||||
current_page == 1
|
||||
end
|
||||
|
||||
def is_last_page?
|
||||
current_page == total_pages
|
||||
end
|
||||
|
||||
def is_sequential_paginator?
|
||||
false
|
||||
end
|
||||
|
||||
def is_numbered_paginator?
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
51
lib/danbooru/paginator/sequential_collection_extension.rb
Normal file
51
lib/danbooru/paginator/sequential_collection_extension.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
module Danbooru
|
||||
module Paginator
|
||||
module SequentialCollectionExtension
|
||||
attr_accessor :sequential_paginator_mode
|
||||
|
||||
def self.extended(obj)
|
||||
obj.extend(Danbooru::Paginator::CollectionExtension)
|
||||
end
|
||||
|
||||
def is_first_page?
|
||||
size == 0
|
||||
end
|
||||
|
||||
def is_last_page?
|
||||
size == 0
|
||||
end
|
||||
|
||||
def is_sequential_paginator?
|
||||
true
|
||||
end
|
||||
|
||||
def is_numbered_paginator?
|
||||
false
|
||||
end
|
||||
|
||||
def to_a
|
||||
if sequential_paginator_mode == :before
|
||||
super
|
||||
else
|
||||
super.reverse
|
||||
end
|
||||
end
|
||||
|
||||
def before_id
|
||||
if size > 0
|
||||
self[-1].id
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def after_id
|
||||
if size > 0
|
||||
self[0].id
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user