major refactoring of post sets and pagination, incomplete

This commit is contained in:
albert
2011-06-03 19:47:16 -04:00
parent ce0695c606
commit ca3e9bb6db
21 changed files with 475 additions and 204 deletions

View File

@@ -1,78 +1,56 @@
module PostSets
class Post < Base
module Post
class Error < Exception ; end
attr_accessor :tags, :errors, :count
attr_accessor :wiki_page, :artist, :suggestions
def initialize(tags, options = {})
super(options)
@tags = Tag.normalize(tags)
@errors = []
load_associations
load_suggestions
validate
attr_accessor :tags, :count, :wiki_page, :artist, :suggestions
def tags
@tags ||= Tag.normalize(params[:tags])
end
def count
@count ||= ::Post.fast_count(tag_string)
end
def posts
@posts ||= slice(::Post.tag_match(tags).limit(limit))
end
def reload
super
@tags = nil
@tag_string = nil
@count = nil
@wiki_page = nil
@artist = nil
end
def wiki_page
@wiki_page ||= ::WikiPage.titled(tag_string).first
end
def artist
@artist ||= ::Artist.find_by_name(tag_string)
end
def has_wiki?
is_single_tag?
end
def has_errors?
errors.any?
end
def offset
x = (page - 1) * limit
if x < 0
x = 0
end
x
end
def is_single_tag?
tag_array.size == 1
end
def date_tag
tag_array.grep(/date:/).first
end
def load_associations
if is_single_tag?
@wiki_page = ::WikiPage.titled(tags).first
@artist = ::Artist.find_by_name(tags)
end
end
def load_posts
@count = ::Post.fast_count(tags)
@posts = ::Post.tag_match(tags).before_id(before_id).all(:order => "posts.id desc", :limit => limit, :offset => offset)
end
def load_suggestions
if count < limit && is_single_tag?
@suggestions = Tag.find_suggestions(tags)
else
@suggestions = []
end
end
def tag_array
@tag_array ||= Tag.scan_query(tags)
end
def tag
tag_array.first
end
def validate
super
validate_page
validate_query_count
rescue Error => x
@errors << x.to_s
end
def validate_page
if page > 1_000
raise Error.new("You cannot explicitly specify the page after page 1000")