Avoid N+1 SQL queries with CanCanCan accessibility checks

This commit is contained in:
Christoph Haas 2021-03-01 00:49:25 +01:00
parent f154c1838c
commit b36a49a6cd
6 changed files with 14 additions and 12 deletions

View file

@ -288,8 +288,9 @@ class PackagesController < ApplicationController
# Return packages matching the criteria given by parameters
def query_packages
# Eager load the screenshots to avoid N+1 queries to check Screenshot.accessibly_by later
packages = Package.includes(:screenshots).order(visits: :desc)
# text search
if params[:search].present?
logger.debug "Searching for #{params[:search]}"
@ -298,13 +299,14 @@ class PackagesController < ApplicationController
case params[:show]
when 'with'
packages = packages.with_screenshots
# Enrich the result with the screenshots readable by the current user (CanCanCan)
packages = packages.where(screenshots: Screenshot.accessible_by(current_ability, :view))
logger.debug 'Limiting packages to those with screenshots'
when 'without'
packages = packages.without_screenshots
logger.debug 'Limiting packages to those without screenshots'
end
return packages
end

View file

@ -1,7 +1,7 @@
class WelcomeController < ApplicationController
def home
# TODO: Only approved screenshots!
@newest_upload = Screenshot.newest_upload
@newest_upload = Screenshot.newest.accessible_by(current_ability, :view).first
@most_popular_package = Package.with_screenshots.order(visits: :desc).first

View file

@ -48,7 +48,7 @@ class Package < ApplicationRecord
def self.with_screenshots
# Query for all packages who's ID appears in a screenshot's "package_id" field
subselect = Screenshot.select(:package_id)
where(id: subselect )
where(id: subselect)
end
# Return a list of packages that have screenshots to be moderated
@ -71,7 +71,7 @@ class Package < ApplicationRecord
# Return a query of all approved/public screenshots of this package
def screenshots_pending
self.screenshots.where('approved=false')
self.screenshots.where(approved: false)
end
# Return a list of packages that have unapproved screenshots

View file

@ -63,9 +63,9 @@ class Screenshot < ApplicationRecord
self.save!
end
# Get the newest screenshot regardless of the package it belongs to
def self.newest_upload
self.order(created_at: :desc).where(approved: true).first
# Get the newest screenshots regardless of the package it belongs to
def self.newest
self.order(created_at: :desc)
end
# Return the part of the version up to the first - or +

View file

@ -18,7 +18,7 @@
.cell.pkgcard data-equalizer-watch=true
a.black href=package_path(name: pkg.name)
.image
= small_img(pkg.screenshots.accessible_by(current_ability, :view).first, cls: '')
= small_img(pkg.screenshots.first, cls: '')
.text.pkgname
= pkg.name
.text
@ -29,7 +29,7 @@
.small-12.medium-4.cell.pkgcard
a.black href=package_path(name: pkg.name)
.image
= small_img(pkg.screenshots.accessible_by(current_ability, :view).first, cls: '')
= small_img(pkg.screenshots.first, cls: '')
.small-12.medium-8.cell
h2
a href=package_path(name: pkg.name)

View file

@ -25,7 +25,7 @@
.text
= screenshot.caption
// Has the unmoderated screenshot been uploaded by the current user?
- if current_user.screenshots.include?(screenshot)
- if current_user && current_user.screenshots.include?(screenshot)
.text
span.label.secondary
' Uploaded by you