Added pagination of screenshots to reduce loading times
This commit is contained in:
parent
f323f94582
commit
77d8daedea
4 changed files with 47 additions and 20 deletions
|
|
@ -13,11 +13,23 @@ class PackagesController < ApplicationController
|
|||
def details
|
||||
# TODO: Get only screenshots visible to the user (admin or owner or approved)
|
||||
@package = Package.find_by(name: params[:name])
|
||||
|
||||
|
||||
unless @package
|
||||
@packagename = params[:name]
|
||||
render 'notfound'
|
||||
end
|
||||
|
||||
# The first page of screenshots (if there are multiple at all) shows the
|
||||
# first image twice as large. So the first page contains 5 images while
|
||||
# the second and further page contain 6 images.
|
||||
@page = params[:page]
|
||||
@images_per_page = if @page.to_i < 2
|
||||
5
|
||||
else
|
||||
6
|
||||
end
|
||||
# raise
|
||||
@screenshots = screenshots_visible_to_user(@package).paginate(page: @page, per_page: @images_per_page)
|
||||
end
|
||||
|
||||
# Show upload form for new images
|
||||
|
|
@ -331,4 +343,19 @@ class PackagesController < ApplicationController
|
|||
return false
|
||||
end
|
||||
|
||||
def screenshots_visible_to_user(package)
|
||||
if user_signed_in? and current_user.is_admin?
|
||||
# User is an admin and can view all screenshots
|
||||
package.screenshots
|
||||
# TODO: User logins and assigned screenshots will come in a later version
|
||||
# elsif user_signed_in?
|
||||
# package.screenshots.where(id: (current_user.screenshots.select(:id))) | \
|
||||
# package.screenshots.where(approved: true).order('created_at DESC')
|
||||
else
|
||||
package.screenshots.where(uploaderhash: session.id.to_s).or(
|
||||
package.screenshots.where(approved: true)
|
||||
)
|
||||
.order('created_at DESC')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue