Files
once-campfire/app/models/message/pagination.rb
Kevin McConnell df76a227dc Hello world
First open source release of Campfire 🎉
2025-08-21 09:31:59 +01:00

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