2014-07-07 18:05:53 +02:00
|
|
|
class StoriesPaginator
|
2015-04-03 19:58:14 +02:00
|
|
|
attr_accessor :per_page
|
|
|
|
|
2014-07-07 18:05:53 +02:00
|
|
|
STORIES_PER_PAGE = 25
|
|
|
|
|
|
|
|
def initialize(scope, page, user)
|
|
|
|
@scope = scope
|
|
|
|
@page = page
|
|
|
|
@user = user
|
2015-04-03 19:58:14 +02:00
|
|
|
@per_page = STORIES_PER_PAGE
|
2014-07-07 18:05:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def get
|
2015-04-03 19:58:14 +02:00
|
|
|
with_pagination_info @scope.limit(per_page + 1)
|
|
|
|
.offset((@page - 1) * per_page)
|
2014-07-07 18:05:53 +02:00
|
|
|
.includes(:user, :taggings => :tag)
|
|
|
|
end
|
|
|
|
|
2014-08-04 03:47:13 +02:00
|
|
|
private
|
2014-07-07 18:05:53 +02:00
|
|
|
def with_pagination_info(scope)
|
|
|
|
scope = scope.to_a
|
2015-04-03 19:58:14 +02:00
|
|
|
show_more = scope.count > per_page
|
2014-07-07 18:05:53 +02:00
|
|
|
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))
|
|
|
|
|
2015-02-11 18:37:03 +01:00
|
|
|
hs = HiddenStory.where(:user_id => @user.id, :story_id =>
|
|
|
|
scope.map(&:id)).map(&:story_id)
|
|
|
|
|
2014-07-07 18:05:53 +02:00
|
|
|
scope.each do |s|
|
|
|
|
if votes[s.id]
|
|
|
|
s.vote = votes[s.id]
|
|
|
|
end
|
2015-02-11 18:37:03 +01:00
|
|
|
if hs.include?(s.id)
|
|
|
|
s.is_hidden_by_cur_user = true
|
|
|
|
end
|
2014-07-07 18:05:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
scope
|
|
|
|
end
|
|
|
|
end
|