2018-10-23 09:49:45 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class UserPreference < ApplicationRecord
|
2018-10-23 09:49:45 +00:00
|
|
|
# We could use enums, but Rails 4 doesn't support multiple
|
|
|
|
# enum options with same name for multiple fields, also it creates
|
|
|
|
# extra methods that aren't really needed here.
|
2018-11-05 12:30:14 +00:00
|
|
|
NOTES_FILTERS = { all_notes: 0, only_comments: 1, only_activity: 2 }.freeze
|
2018-10-23 09:49:45 +00:00
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates :issue_notes_filter, :merge_request_notes_filter, inclusion: { in: NOTES_FILTERS.values }, presence: true
|
2020-02-06 12:10:29 +00:00
|
|
|
validates :tab_width, numericality: {
|
|
|
|
only_integer: true,
|
|
|
|
greater_than_or_equal_to: Gitlab::TabWidth::MIN,
|
|
|
|
less_than_or_equal_to: Gitlab::TabWidth::MAX
|
|
|
|
}
|
2018-10-23 09:49:45 +00:00
|
|
|
|
2020-05-28 21:08:22 +00:00
|
|
|
enum experience_level: { novice: 0, experienced: 1 }
|
|
|
|
|
2020-02-06 12:10:29 +00:00
|
|
|
default_value_for :tab_width, value: Gitlab::TabWidth::DEFAULT, allows_nil: false
|
2019-05-07 21:58:16 +00:00
|
|
|
default_value_for :timezone, value: Time.zone.tzinfo.name, allows_nil: false
|
|
|
|
default_value_for :time_display_relative, value: true, allows_nil: false
|
|
|
|
default_value_for :time_format_in_24h, value: false, allows_nil: false
|
2020-01-06 15:07:26 +00:00
|
|
|
default_value_for :render_whitespace_in_code, value: false, allows_nil: false
|
2019-05-07 21:58:16 +00:00
|
|
|
|
2018-10-23 09:49:45 +00:00
|
|
|
class << self
|
|
|
|
def notes_filters
|
|
|
|
{
|
|
|
|
s_('Notes|Show all activity') => NOTES_FILTERS[:all_notes],
|
2018-11-05 12:30:14 +00:00
|
|
|
s_('Notes|Show comments only') => NOTES_FILTERS[:only_comments],
|
|
|
|
s_('Notes|Show history only') => NOTES_FILTERS[:only_activity]
|
2018-10-23 09:49:45 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_notes_filter(filter_id, issuable)
|
|
|
|
# No need to update the column if the value is already set.
|
2019-07-24 18:11:18 +00:00
|
|
|
if filter_id && NOTES_FILTERS.value?(filter_id)
|
2018-10-23 09:49:45 +00:00
|
|
|
field = notes_filter_field_for(issuable)
|
|
|
|
self[field] = filter_id
|
|
|
|
|
|
|
|
save if attribute_changed?(field)
|
|
|
|
end
|
|
|
|
|
|
|
|
notes_filter_for(issuable)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the current discussion filter for a given issuable
|
|
|
|
# or issuable type.
|
|
|
|
def notes_filter_for(resource)
|
|
|
|
self[notes_filter_field_for(resource)]
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def notes_filter_field_for(resource)
|
|
|
|
field_key =
|
|
|
|
if resource.is_a?(Issuable)
|
|
|
|
resource.model_name.param_key
|
|
|
|
else
|
|
|
|
resource
|
|
|
|
end
|
|
|
|
|
|
|
|
"#{field_key}_notes_filter"
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
UserPreference.prepend_if_ee('EE::UserPreference')
|