gitlab-org--gitlab-foss/app/controllers/concerns/with_performance_bar.rb
Sean McGivern f9c456bd0c Make performance bar enabled checks consistent
Previously, we called the `peek_enabled?` method like so:

    prepend_before_action :set_peek_request_id, if: :peek_enabled?

Now we don't have a `set_peek_request_id` method, so we don't need that
line. However, the `peek_enabled?` part had a side-effect: it would also
populate the request store cache for whether the performance bar was
enabled for the current request or not.

This commit makes that side-effect explicit, and replaces all uses of
`peek_enabled?` with the more explicit
`Gitlab::PerformanceBar.enabled_for_request?`. There is one spec that
still sets `SafeRequestStore[:peek_enabled]` directly, because it is
contrasting behaviour with and without a request store enabled.

The upshot is:

1. We still set the value in one place. We make it more explicit that
   that's what we're doing.
2. Reading that value uses a consistent method so it's easier to find in
   future.
2019-08-28 17:25:02 +01:00

31 lines
778 B
Ruby

# frozen_string_literal: true
module WithPerformanceBar
extend ActiveSupport::Concern
included do
before_action :set_peek_enabled_for_current_request
end
private
def set_peek_enabled_for_current_request
Gitlab::SafeRequestStore.fetch(:peek_enabled) { cookie_or_default_value }
end
# Needed for Peek's routing to work;
# Peek::ResultsController#restrict_non_access calls this method.
def peek_enabled?
Gitlab::PerformanceBar.enabled_for_request?
end
def cookie_or_default_value
return false unless Gitlab::PerformanceBar.enabled_for_user?(current_user)
if cookies[:perf_bar_enabled].present?
cookies[:perf_bar_enabled] == 'true'
else
cookies[:perf_bar_enabled] = 'true' if Rails.env.development?
end
end
end