journalduhacker/app/models/stories_paginator.rb
joshua stein f9b309d342 separate story hiding from voting
A story downvote is considered a flag, just meaning the story has
problems and not necessarily that the user wants to ignore it.  By
moving hiding out of Vote and into a new HiddenStory model, a user
can now both downvote/flag and hide separately, or just one or the
other.
2015-02-11 11:37:03 -06:00

44 lines
924 B
Ruby

class StoriesPaginator
STORIES_PER_PAGE = 25
def initialize(scope, page, user)
@scope = scope
@page = page
@user = user
end
def get
with_pagination_info @scope.limit(STORIES_PER_PAGE + 1)
.offset((@page - 1) * STORIES_PER_PAGE)
.includes(:user, :taggings => :tag)
end
private
def with_pagination_info(scope)
scope = scope.to_a
show_more = scope.count > STORIES_PER_PAGE
scope.pop if show_more
[cache_votes(scope), show_more]
end
def cache_votes(scope)
if @user
votes = Vote.votes_by_user_for_stories_hash(@user.id, scope.map(&:id))
hs = HiddenStory.where(:user_id => @user.id, :story_id =>
scope.map(&:id)).map(&:story_id)
scope.each do |s|
if votes[s.id]
s.vote = votes[s.id]
end
if hs.include?(s.id)
s.is_hidden_by_cur_user = true
end
end
end
scope
end
end