major refactoring of post sets and pagination, incomplete
This commit is contained in:
@@ -1,45 +1,78 @@
|
||||
# A PostSet represents a paginated slice of posts. It is used in conjunction
|
||||
# with the helpers to render the paginator.
|
||||
#
|
||||
# Usage:
|
||||
#
|
||||
# @post_set = PostSets::Base.new(params)
|
||||
# @post_set.extend(PostSets::Sequential)
|
||||
# @post_set.extend(PostSets::Post)
|
||||
|
||||
module PostSets
|
||||
class Base
|
||||
attr_accessor :page, :before_id, :count, :posts
|
||||
|
||||
def initialize(options = {})
|
||||
@page = options[:page] ? options[:page].to_i : 1
|
||||
@before_id = options[:before_id]
|
||||
load_posts
|
||||
attr_reader :params, :posts
|
||||
delegate :to_xml, :to_json, :to => :posts
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def has_wiki?
|
||||
false
|
||||
end
|
||||
|
||||
def use_sequential_paginator?
|
||||
!use_numbered_paginator?
|
||||
end
|
||||
|
||||
def use_numbered_paginator?
|
||||
before_id.nil?
|
||||
end
|
||||
|
||||
def load_posts
|
||||
# Should a return a paginated array of posts. This means it should have
|
||||
# at most <limit> elements.
|
||||
def posts
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def to_xml
|
||||
posts.to_xml
|
||||
|
||||
# Does this post set have a valid wiki page representation?
|
||||
def has_wiki?
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def to_json
|
||||
posts.to_json
|
||||
|
||||
# Should return an array of strings representing the tags.
|
||||
def tags
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
|
||||
# Given an ActiveRelation object, perform the necessary pagination to
|
||||
# extract at most <limit> elements. Should return an array.
|
||||
def slice(relation)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# For cases where we're not relying on the default pagination
|
||||
# implementation (for example, if the ids are cached in a string)
|
||||
# then pass in the offset/before_id/after_id parameters here.
|
||||
def pagination_options
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# This method should throw an exception if for whatever reason the query
|
||||
# is invalid or forbidden.
|
||||
def validate
|
||||
end
|
||||
|
||||
# Clear out any memoized instance variables.
|
||||
def reload
|
||||
@posts = nil
|
||||
@presenter = nil
|
||||
@tag_string = nil
|
||||
end
|
||||
|
||||
def tag_string
|
||||
@tag_string ||= tags.join(" ")
|
||||
end
|
||||
|
||||
def is_first_page?
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def is_last_page?
|
||||
posts.size == 0
|
||||
end
|
||||
|
||||
def presenter
|
||||
@presenter ||= PostSetPresenter.new(self)
|
||||
end
|
||||
|
||||
def offset
|
||||
((page < 1) ? 0 : (page - 1)) * count
|
||||
end
|
||||
|
||||
def limit
|
||||
Danbooru.config.posts_per_page
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user