BigW Consortium Gitlab

preferences_helper.rb 2 KB
Newer Older
1 2
# Helper methods for per-User preferences
module PreferencesHelper
3 4
  def layout_choices
    [
5 6
        ['Fixed', :fixed],
        ['Fluid', :fluid]
7 8 9
    ]
  end

10 11 12
  # Maps `dashboard` values to more user-friendly option text
  DASHBOARD_CHOICES = {
    projects: 'Your Projects (default)',
13 14
    stars:    'Starred Projects',
    project_activity: "Your Projects' Activity",
15 16 17
    starred_project_activity: "Starred Projects' Activity",
    groups: "Your Groups",
    todos: "Your Todos"
18
  }.with_indifferent_access.freeze
19

20 21 22
  # Returns an Array usable by a select field for more user-friendly option text
  def dashboard_choices
    defined = User.dashboards
23

24 25
    if defined.size != DASHBOARD_CHOICES.size
      # Ensure that anyone adding new options updates this method too
26
      raise "`User` defines #{defined.size} dashboard choices," \
27
        " but `DASHBOARD_CHOICES` defined #{DASHBOARD_CHOICES.size}."
28
    else
29 30 31 32
      defined.map do |key, _|
        # Use `fetch` so `KeyError` gets raised when a key is missing
        [DASHBOARD_CHOICES.fetch(key), key]
      end
33 34 35
    end
  end

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

44 45 46 47
  def user_application_theme
    @user_application_theme ||= Gitlab::Themes.for_user(current_user).css_class
  end

48 49
  def user_color_scheme
    Gitlab::ColorSchemes.for_user(current_user).css_class
50 51
  end

52
  def default_project_view
53
    return anonymous_project_view unless current_user
54 55 56

    user_view = current_user.project_view

57
    if can?(current_user, :download_code, @project)
58 59 60
      user_view
    elsif user_view == "activity"
      "activity"
61
    elsif can?(current_user, :read_wiki, @project)
62 63 64 65 66 67
      "wiki"
    elsif @project.feature_available?(:issues, current_user)
      "projects/issues/issues"
    else
      "customize_workflow"
    end
68
  end
69

70
  def anonymous_project_view
71 72 73 74 75
    if !@project.empty_repo? && can?(current_user, :download_code, @project)
      'files'
    else
      'activity'
    end
76
  end
77
end