From 16d9ed15af860ea70d3229d4fb229e3ddb5d45bd Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sun, 19 Feb 2023 13:23:01 -0500 Subject: [PATCH] Memoize current_user in actions (#508) * Memoize current_user in BrowserAction * Clarify what memoize is doing --- .../src/actions/mixins/api/auth/helpers.cr | 3 ++- src/browser_app_skeleton/src/actions/browser_action.cr.ecr | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/api_authentication_app_skeleton/src/actions/mixins/api/auth/helpers.cr b/src/api_authentication_app_skeleton/src/actions/mixins/api/auth/helpers.cr index 7ecd8803..6b51cb5a 100644 --- a/src/api_authentication_app_skeleton/src/actions/mixins/api/auth/helpers.cr +++ b/src/api_authentication_app_skeleton/src/actions/mixins/api/auth/helpers.cr @@ -1,5 +1,6 @@ module Api::Auth::Helpers - def current_user? : User? + # The 'memoize' macro makes sure only one query is issued to find the user + memoize def current_user? : User? auth_token.try do |value| user_from_auth_token(value) end diff --git a/src/browser_app_skeleton/src/actions/browser_action.cr.ecr b/src/browser_app_skeleton/src/actions/browser_action.cr.ecr index facfbce1..096be837 100644 --- a/src/browser_app_skeleton/src/actions/browser_action.cr.ecr +++ b/src/browser_app_skeleton/src/actions/browser_action.cr.ecr @@ -39,7 +39,8 @@ abstract class BrowserAction < Lucky::Action expose current_user # This method tells Authentic how to find the current user - private def find_current_user(id) : User? + # The 'memoize' macro makes sure only one query is issued to find the user + private memoize def find_current_user(id) : User? UserQuery.new.id(id).first? end <%- end -%>