Leverage Rails' caching when checking for admin

The privileges of a user should never change within a request
life cycle. The `spree_roles` association is very small, between 0 and 2
items are quickly searched in memory without the need of additional
database queries.

From memory, I've seen a lot of spree_roles queries in log files per
request. This should reduce it to one.
This commit is contained in:
Maikel Linke
2024-12-19 09:47:43 +11:00
parent 54f83b45c8
commit 5db8cb452e
3 changed files with 1 additions and 4 deletions

View File

@@ -60,7 +60,7 @@ module Spree
# Checks whether the specified user is a superadmin, with full control of the instance
def admin?
spree_roles.where(name: "admin").any?
spree_roles.any? { |role| role.name == "admin" }
end
# Send devise-based user emails asyncronously via ActiveJob

View File

@@ -148,7 +148,6 @@ module Admin
order_cycles: 6,
proxy_orders: 1,
schedules: 1,
spree_roles: 9,
spree_variants: 8,
tags: 1
},

View File

@@ -140,8 +140,6 @@ RSpec.describe Reporting::Reports::OrdersAndDistributors::Base do
subject # build context first
expect { subject.table_rows }.to query_database [
"Spree::Role Exists?",
"Spree::Role Exists?",
"SQL",
"Spree::LineItem Load",
"Spree::PaymentMethod Load",