BigW Consortium Gitlab

preferences_helper.rb 1.83 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 39
      ['Files and Readme (default)', :files],
      ['Activity view', :activity]
40 41 42
    ]
  end

43 44
  def user_color_scheme
    Gitlab::ColorSchemes.for_user(current_user).css_class
45 46
  end

47
  def default_project_view
48
    return anonymous_project_view unless current_user
49 50 51 52 53 54 55 56 57 58 59 60 61 62

    user_view = current_user.project_view

    if @project.feature_available?(:repository, current_user)
      user_view
    elsif user_view == "activity"
      "activity"
    elsif @project.wiki_enabled?
      "wiki"
    elsif @project.feature_available?(:issues, current_user)
      "projects/issues/issues"
    else
      "customize_workflow"
    end
63
  end
64

65 66
  def anonymous_project_view
    @project.empty_repo? || !can?(current_user, :download_code, @project) ? 'activity' : 'readme'
67
  end
68
end