more work on paginators
This commit is contained in:
37
app/logical/post_sets/base.rb
Normal file
37
app/logical/post_sets/base.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
module PostSets
|
||||
class Base
|
||||
def has_wiki?
|
||||
false
|
||||
end
|
||||
|
||||
def wiki_page
|
||||
nil
|
||||
end
|
||||
|
||||
def presenter
|
||||
@presenter ||= PostSetPresenter.new(self)
|
||||
end
|
||||
|
||||
def is_single_tag?
|
||||
false
|
||||
end
|
||||
|
||||
def tag
|
||||
tag_string
|
||||
end
|
||||
|
||||
def arbitrary_sql_order_clause(ids, table_name)
|
||||
if ids.empty?
|
||||
return "#{table_name}.id desc"
|
||||
end
|
||||
|
||||
conditions = []
|
||||
|
||||
ids.each_with_index do |x, n|
|
||||
conditions << "when #{x} then #{n}"
|
||||
end
|
||||
|
||||
"case #{table_name}.id " + conditions.join(" ") + " end"
|
||||
end
|
||||
end
|
||||
end
|
||||
24
app/logical/post_sets/favorite.rb
Normal file
24
app/logical/post_sets/favorite.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
module PostSets
|
||||
class Favorite < Base
|
||||
attr_reader :user, :page, :favorites, :posts
|
||||
|
||||
def initialize(params)
|
||||
@user = ::User.find(params[:id])
|
||||
@page = [params[:page].to_i, 1].max
|
||||
@favorites = ::Favorite.model_for(@user.id).for_user(@user.id).paginate(page)
|
||||
@posts = ::Post.where("id in (?)", post_ids).order(arbitrary_sql_order_clause(post_ids, "posts")).paginate("a0")
|
||||
end
|
||||
|
||||
def post_ids
|
||||
@post_ids ||= favorites.map(&:post_id)
|
||||
end
|
||||
|
||||
def offset
|
||||
(page - 1) * records_per_page
|
||||
end
|
||||
|
||||
def tag_string
|
||||
@tag_string ||= "fav:#{user.name}"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,5 @@
|
||||
module PostSets
|
||||
class Post
|
||||
class Post < Base
|
||||
attr_reader :tags, :page, :posts
|
||||
|
||||
def initialize(params)
|
||||
@@ -31,13 +31,5 @@ module PostSets
|
||||
def is_single_tag?
|
||||
tags.size == 1
|
||||
end
|
||||
|
||||
def tag
|
||||
tag_string
|
||||
end
|
||||
|
||||
def presenter
|
||||
@presenter ||= PostSetPresenter.new(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user