debian-mirror-gitlab/app/helpers/preferences_helper.rb

153 lines
4.5 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
# Helper methods for per-User preferences
module PreferencesHelper
2015-10-24 18:46:33 +05:30
def layout_choices
[
2022-03-02 08:16:31 +05:30
[s_('Layout|Fixed'), :fixed],
[s_('Layout|Fluid'), :fluid]
2015-10-24 18:46:33 +05:30
]
end
2015-09-11 14:41:01 +05:30
# Returns an Array usable by a select field for more user-friendly option text
def dashboard_choices
2018-12-13 13:39:08 +05:30
dashboards = User.dashboards.keys
2015-09-11 14:41:01 +05:30
2018-12-13 13:39:08 +05:30
validate_dashboard_choices!(dashboards)
dashboards -= excluded_dashboard_choices
dashboards.map do |key|
# Use `fetch` so `KeyError` gets raised when a key is missing
2020-04-22 19:07:51 +05:30
[localized_dashboard_choices.fetch(key), key]
2015-09-11 14:41:01 +05:30
end
end
2020-04-22 19:07:51 +05:30
# Maps `dashboard` values to more user-friendly option text
def localized_dashboard_choices
{
projects: _("Your Projects (default)"),
stars: _("Starred Projects"),
project_activity: _("Your Projects' Activity"),
starred_project_activity: _("Starred Projects' Activity"),
2021-04-17 20:07:23 +05:30
followed_user_activity: _("Followed Users' Activity"),
2020-04-22 19:07:51 +05:30
groups: _("Your Groups"),
todos: _("Your To-Do List"),
issues: _("Assigned Issues"),
2021-04-29 21:17:54 +05:30
merge_requests: _("Assigned merge requests"),
2020-04-22 19:07:51 +05:30
operations: _("Operations Dashboard")
}.with_indifferent_access.freeze
end
2015-09-11 14:41:01 +05:30
def project_view_choices
[
2022-03-02 08:16:31 +05:30
[s_('ProjectView|Files and Readme (default)'), :files],
[s_('ProjectView|Activity'), :activity],
[s_('ProjectView|Readme'), :readme]
2015-09-11 14:41:01 +05:30
]
end
2019-03-02 22:35:43 +05:30
def first_day_of_week_choices
[
[_('Sunday'), 0],
2019-07-07 11:18:12 +05:30
[_('Monday'), 1],
[_('Saturday'), 6]
2019-03-02 22:35:43 +05:30
]
end
def first_day_of_week_choices_with_default
first_day_of_week_choices.unshift([_('System default (%{default})') % { default: default_first_day_of_week }, nil])
end
2018-03-17 18:26:18 +05:30
def user_application_theme
@user_application_theme ||= Gitlab::Themes.for_user(current_user).css_class
end
2022-05-07 20:08:51 +05:30
def user_application_dark_mode?
user_application_theme == 'gl-dark'
end
2021-01-03 14:25:43 +05:30
def user_application_theme_css_filename
@user_application_theme_css_filename ||= Gitlab::Themes.for_user(current_user).css_filename
2020-11-24 15:15:51 +05:30
end
2021-09-04 01:27:46 +05:30
def user_theme_primary_color
Gitlab::Themes.for_user(current_user).primary_color
end
2015-09-25 12:07:36 +05:30
def user_color_scheme
Gitlab::ColorSchemes.for_user(current_user).css_class
2015-09-11 14:41:01 +05:30
end
2018-12-13 13:39:08 +05:30
2020-03-13 15:44:24 +05:30
def user_tab_width
Gitlab::TabWidth.css_class_for_user(current_user)
end
2022-06-21 17:19:12 +05:30
def user_diffs_colors
{
deletion: current_user&.diffs_deletion_color.presence,
addition: current_user&.diffs_addition_color.presence
}.compact
end
def custom_diff_color_classes
return if request.path == profile_preferences_path
classes = []
classes << 'diff-custom-addition-color' if current_user&.diffs_addition_color.presence
classes << 'diff-custom-deletion-color' if current_user&.diffs_deletion_color.presence
classes
end
2019-07-07 11:18:12 +05:30
def language_choices
2020-07-28 23:09:34 +05:30
options_for_select(
2021-06-08 01:23:25 +05:30
selectable_locales_with_translation_level.sort,
2020-07-28 23:09:34 +05:30
current_user.preferred_language
)
2019-07-07 11:18:12 +05:30
end
2020-11-24 15:15:51 +05:30
def integration_views
[].tap do |views|
2021-09-04 01:27:46 +05:30
views << { name: 'gitpod', message: gitpod_enable_description, message_url: gitpod_url_placeholder, help_link: help_page_path('integration/gitpod.md') } if Gitlab::CurrentSettings.gitpod_enabled
2021-01-29 00:20:46 +05:30
views << { name: 'sourcegraph', message: sourcegraph_url_message, message_url: Gitlab::CurrentSettings.sourcegraph_url, help_link: help_page_path('user/profile/preferences.md', anchor: 'sourcegraph') } if Gitlab::Sourcegraph.feature_available? && Gitlab::CurrentSettings.sourcegraph_enabled
2020-11-24 15:15:51 +05:30
end
end
2018-12-13 13:39:08 +05:30
private
2021-09-04 01:27:46 +05:30
def gitpod_url_placeholder
Gitlab::CurrentSettings.gitpod_url.presence || 'https://gitpod.io/'
end
2018-12-13 13:39:08 +05:30
# Ensure that anyone adding new options updates `DASHBOARD_CHOICES` too
def validate_dashboard_choices!(user_dashboards)
2020-04-22 19:07:51 +05:30
if user_dashboards.size != localized_dashboard_choices.size
2018-12-13 13:39:08 +05:30
raise "`User` defines #{user_dashboards.size} dashboard choices," \
2020-04-22 19:07:51 +05:30
" but `localized_dashboard_choices` defined #{localized_dashboard_choices.size}."
2018-12-13 13:39:08 +05:30
end
end
# List of dashboard choice to be excluded from CE.
# EE would override this.
def excluded_dashboard_choices
['operations']
end
2019-03-02 22:35:43 +05:30
def default_first_day_of_week
first_day_of_week_choices.rassoc(Gitlab::CurrentSettings.first_day_of_week).first
end
2021-06-08 01:23:25 +05:30
def selectable_locales_with_translation_level
Gitlab::I18n.selectable_locales.map do |code, language|
[
s_("i18n|%{language} (%{percent_translated}%% translated)") % {
language: language,
percent_translated: Gitlab::I18n.percentage_translated_for(code)
},
code
]
end
end
2015-09-11 14:41:01 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
PreferencesHelper.prepend_mod_with('PreferencesHelper')