preferences_helper.rb 2.27 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
# Helper methods for per-User preferences
module PreferencesHelper
5 6
  def layout_choices
    [
7 8
        ['Fixed', :fixed],
        ['Fluid', :fluid]
9 10 11
    ]
  end

Robert Speicher's avatar
Robert Speicher committed
12 13
  # Maps `dashboard` values to more user-friendly option text
  DASHBOARD_CHOICES = {
14 15 16 17 18 19 20
    projects: _("Your Projects (default)"),
    stars:    _("Starred Projects"),
    project_activity: _("Your Projects' Activity"),
    starred_project_activity: _("Starred Projects' Activity"),
    groups: _("Your Groups"),
    todos: _("Your Todos"),
    issues: _("Assigned Issues"),
21 22
    merge_requests: _("Assigned Merge Requests"),
    operations: _("Operations Dashboard")
Robert Speicher's avatar
Robert Speicher committed
23
  }.with_indifferent_access.freeze
24

Robert Speicher's avatar
Robert Speicher committed
25 26
  # Returns an Array usable by a select field for more user-friendly option text
  def dashboard_choices
27
    dashboards = User.dashboards.keys
28

29 30 31 32 33 34
    validate_dashboard_choices!(dashboards)
    dashboards -= excluded_dashboard_choices

    dashboards.map do |key|
      # Use `fetch` so `KeyError` gets raised when a key is missing
      [DASHBOARD_CHOICES.fetch(key), key]
35 36 37
    end
  end

38 39
  def project_view_choices
    [
40
      ['Files and Readme (default)', :files],
41 42
      ['Activity', :activity],
      ['Readme', :readme]
43 44 45
    ]
  end

46 47
  def first_day_of_week_choices
    [
48 49
      [_('Sunday'), 0],
      [_('Monday'), 1]
50 51 52
    ]
  end

53
  def first_day_of_week_choices_with_default
54
    first_day_of_week_choices.unshift([_('System default (%{default})') % { default: default_first_day_of_week }, nil])
55 56
  end

57 58 59 60
  def user_application_theme
    @user_application_theme ||= Gitlab::Themes.for_user(current_user).css_class
  end

61 62
  def user_color_scheme
    Gitlab::ColorSchemes.for_user(current_user).css_class
63
  end
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

  private

  # Ensure that anyone adding new options updates `DASHBOARD_CHOICES` too
  def validate_dashboard_choices!(user_dashboards)
    if user_dashboards.size != DASHBOARD_CHOICES.size
      raise "`User` defines #{user_dashboards.size} dashboard choices," \
        " but `DASHBOARD_CHOICES` defined #{DASHBOARD_CHOICES.size}."
    end
  end

  # List of dashboard choice to be excluded from CE.
  # EE would override this.
  def excluded_dashboard_choices
    ['operations']
  end
80 81 82 83

  def default_first_day_of_week
    first_day_of_week_choices.rassoc(Gitlab::CurrentSettings.first_day_of_week).first
  end
84
end