Class: UserPreference

Inherits:
ApplicationRecord show all
Defined in:
app/models/user_preference.rb

Constant Summary collapse

NOTES_FILTERS =

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.

{ all_notes: 0, only_comments: 1, only_activity: 2 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.notes_filtersObject


27
28
29
30
31
32
33
# File 'app/models/user_preference.rb', line 27

def notes_filters
  {
    s_('Notes|Show all activity') => NOTES_FILTERS[:all_notes],
    s_('Notes|Show comments only') => NOTES_FILTERS[:only_comments],
    s_('Notes|Show history only') => NOTES_FILTERS[:only_activity]
  }
end

Instance Method Details

#notes_filter_for(resource) ⇒ Object

Returns the current discussion filter for a given issuable or issuable type.


50
51
52
# File 'app/models/user_preference.rb', line 50

def notes_filter_for(resource)
  self[notes_filter_field_for(resource)]
end

#set_notes_filter(filter_id, issuable) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/user_preference.rb', line 36

def set_notes_filter(filter_id, issuable)
  # No need to update the column if the value is already set.
  if filter_id && NOTES_FILTERS.value?(filter_id)
    field = notes_filter_field_for(issuable)
    self[field] = filter_id

    save if attribute_changed?(field)
  end

  notes_filter_for(issuable)
end