gon_helper.rb 2.9 KB
Newer Older
G
gfyoung 已提交
1 2
# frozen_string_literal: true

3 4
# rubocop:disable Metrics/AbcSize

5 6
module Gitlab
  module GonHelper
7 8
    include WebpackHelper

9
    def add_gon_variables
S
Sam Rose 已提交
10
      gon.api_version            = 'v4'
11
      gon.default_avatar_url     = default_avatar_url
12
      gon.max_file_size          = Gitlab::CurrentSettings.max_attachment_size
13
      gon.asset_host             = ActionController::Base.asset_host
14
      gon.webpack_public_path    = webpack_public_path
15
      gon.relative_url_root      = Gitlab.config.gitlab.relative_url_root
16
      gon.shortcuts_path         = Gitlab::Routing.url_helpers.help_page_path('shortcuts')
17
      gon.user_color_scheme      = Gitlab::ColorSchemes.for_user(current_user).css_class
18

19 20
      if Gitlab.config.sentry.enabled
        gon.sentry_dsn           = Gitlab.config.sentry.clientside_dsn
21 22 23
        gon.sentry_environment   = Gitlab.config.sentry.environment
      end

24
      gon.gitlab_url             = Gitlab.config.gitlab.url
25
      gon.revision               = Gitlab.revision
26
      gon.gitlab_logo            = ActionController::Base.helpers.asset_path('gitlab_logo.png')
27
      gon.sprite_icons           = IconsHelper.sprite_icon_path
T
Tim Zallmann 已提交
28
      gon.sprite_file_icons      = IconsHelper.sprite_file_icons_path
T
Tim Zallmann 已提交
29
      gon.emoji_sprites_css_path = ActionController::Base.helpers.stylesheet_path('emoji_sprites')
M
Mike Greiling 已提交
30
      gon.test_env               = Rails.env.test?
31
      gon.suggested_label_colors = LabelsHelper.suggested_colors
F
Fabian Schneider 已提交
32
      gon.first_day_of_week      = current_user&.first_day_of_week || Gitlab::CurrentSettings.first_day_of_week
33
      gon.ee                     = Gitlab.ee?
34 35 36

      if current_user
        gon.current_user_id = current_user.id
C
Clement Ho 已提交
37
        gon.current_username = current_user.username
38
        gon.current_user_fullname = current_user.name
39
        gon.current_user_avatar_url = current_user.avatar_url
40 41
      end
    end
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

    # Exposes the state of a feature flag to the frontend code.
    #
    # name - The name of the feature flag, e.g. `my_feature`.
    # args - Any additional arguments to pass to `Feature.enabled?`. This allows
    #        you to check if a flag is enabled for a particular user.
    def push_frontend_feature_flag(name, *args)
      var_name = name.to_s.camelize(:lower)
      enabled = Feature.enabled?(name, *args)

      # Here the `true` argument signals gon that the value should be merged
      # into any existing ones, instead of overwriting them. This allows you to
      # use this method to push multiple feature flags.
      gon.push({ features: { var_name => enabled } }, true)
    end
57 58 59 60 61 62 63 64 65 66

    def default_avatar_url
      # We can't use ActionController::Base.helpers.image_url because it
      # doesn't return an actual URL because request is nil for some reason.
      #
      # We also can't use Gitlab::Utils.append_path because the image path
      # may be an absolute URL.
      URI.join(Gitlab.config.gitlab.url,
               ActionController::Base.helpers.image_path('no_avatar.png')).to_s
    end
67 68
  end
end