mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-03-13 21:55:15 +09:00
30 lines
844 B
Ruby
30 lines
844 B
Ruby
module Message::Pagination
|
|
extend ActiveSupport::Concern
|
|
|
|
PAGE_SIZE = 40
|
|
|
|
included do
|
|
scope :last_page, -> { ordered.last(PAGE_SIZE) }
|
|
scope :first_page, -> { ordered.first(PAGE_SIZE) }
|
|
|
|
scope :before, ->(message) { where("created_at < ?", message.created_at) }
|
|
scope :after, ->(message) { where("created_at > ?", message.created_at) }
|
|
|
|
scope :page_before, ->(message) { before(message).last_page }
|
|
scope :page_after, ->(message) { after(message).first_page }
|
|
|
|
scope :page_created_since, ->(time) { where("created_at > ?", time).first_page }
|
|
scope :page_updated_since, ->(time) { where("updated_at > ?", time).last_page }
|
|
end
|
|
|
|
class_methods do
|
|
def page_around(message)
|
|
page_before(message) + [ message ] + page_after(message)
|
|
end
|
|
|
|
def paged?
|
|
count > PAGE_SIZE
|
|
end
|
|
end
|
|
end
|