Read about our upcoming Code of Conduct on this issue

gon_helper.rb 3.19 KB
Newer Older
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
Sam Rose's avatar
Sam Rose committed
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
Tim Zallmann's avatar
Tim Zallmann committed
28
      gon.sprite_file_icons      = IconsHelper.sprite_file_icons_path
Tim Zallmann's avatar
Tim Zallmann committed
29
      gon.emoji_sprites_css_path = ActionController::Base.helpers.stylesheet_path('emoji_sprites')
Mike Greiling's avatar
Mike Greiling committed
30
      gon.test_env               = Rails.env.test?
31
      gon.suggested_label_colors = LabelsHelper.suggested_colors
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
Clement Ho's avatar
Clement Ho committed
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
      end
41
42
43
44

      # Initialize gon.features with any flags that should be
      # made globally available to the frontend
      push_frontend_feature_flag(:suppress_ajax_navigation_errors, default_enabled: true)
45
      push_frontend_feature_flag(:snippets_vue, default_enabled: false)
46
    end
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

    # 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
62
63
64
65
66
67
68
69
70
71

    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
72
73
  end
end