From a2b54626d021fd818fd1e6991d4df589099607c5 Mon Sep 17 00:00:00 2001 From: Misha Gorodnitzky Date: Wed, 27 Jul 2022 14:50:20 +0100 Subject: [PATCH] detect private current_* methods Some services may declare current_user (or current_* really) as private methods if there is no need to expose that method in the controller. Previously we would miss out on sending the user_id with the event if this was the case, this change allows us to detect these as private methods. For testing purposes, we've made the current_* methods private since that ensures we work with both private and public versions of these methods. --- lib/dfe/analytics/requests.rb | 4 ++-- spec/dummy/app/controllers/application_controller.rb | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/dfe/analytics/requests.rb b/lib/dfe/analytics/requests.rb index 91268a0b..3657722d 100644 --- a/lib/dfe/analytics/requests.rb +++ b/lib/dfe/analytics/requests.rb @@ -18,8 +18,8 @@ def trigger_request_event .with_response_details(response) .with_request_uuid(RequestLocals.fetch(:dfe_analytics_request_id) { nil }) - request_event.with_user(current_user) if respond_to? :current_user - request_event.with_namespace(current_namespace) if respond_to? :current_namespace + request_event.with_user(current_user) if respond_to?(:current_user, true) + request_event.with_namespace(current_namespace) if respond_to?(:current_namespace, true) DfE::Analytics::SendEvents.do([request_event.as_json]) end diff --git a/spec/dummy/app/controllers/application_controller.rb b/spec/dummy/app/controllers/application_controller.rb index 7d18d730..65322f4b 100644 --- a/spec/dummy/app/controllers/application_controller.rb +++ b/spec/dummy/app/controllers/application_controller.rb @@ -5,6 +5,8 @@ def index render plain: '' end + private + def current_user Struct.new(:id).new(1) end