StoriesPaginator: make per_page configurable
This commit is contained in:
parent
fdf892e7f9
commit
88602f7535
|
@ -1,22 +1,25 @@
|
||||||
class StoriesPaginator
|
class StoriesPaginator
|
||||||
|
attr_accessor :per_page
|
||||||
|
|
||||||
STORIES_PER_PAGE = 25
|
STORIES_PER_PAGE = 25
|
||||||
|
|
||||||
def initialize(scope, page, user)
|
def initialize(scope, page, user)
|
||||||
@scope = scope
|
@scope = scope
|
||||||
@page = page
|
@page = page
|
||||||
@user = user
|
@user = user
|
||||||
|
@per_page = STORIES_PER_PAGE
|
||||||
end
|
end
|
||||||
|
|
||||||
def get
|
def get
|
||||||
with_pagination_info @scope.limit(STORIES_PER_PAGE + 1)
|
with_pagination_info @scope.limit(per_page + 1)
|
||||||
.offset((@page - 1) * STORIES_PER_PAGE)
|
.offset((@page - 1) * per_page)
|
||||||
.includes(:user, :taggings => :tag)
|
.includes(:user, :taggings => :tag)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def with_pagination_info(scope)
|
def with_pagination_info(scope)
|
||||||
scope = scope.to_a
|
scope = scope.to_a
|
||||||
show_more = scope.count > STORIES_PER_PAGE
|
show_more = scope.count > per_page
|
||||||
scope.pop if show_more
|
scope.pop if show_more
|
||||||
|
|
||||||
[cache_votes(scope), show_more]
|
[cache_votes(scope), show_more]
|
||||||
|
|
Loading…
Reference in a new issue