Class: NotificationSetting

Inherits:
ApplicationRecord show all
Includes:
FromUnion
Defined in:
app/models/notification_setting.rb

Constant Summary collapse

EMAIL_EVENTS =

NOTE: Applicable unfound_translations.rb also needs to be updated when below events are changed.

[
  :new_release,
  :new_note,
  :new_issue,
  :reopen_issue,
  :close_issue,
  :reassign_issue,
  :issue_due,
  :new_merge_request,
  :push_to_merge_request,
  :reopen_merge_request,
  :close_merge_request,
  :reassign_merge_request,
  :change_reviewer_merge_request,
  :merge_merge_request,
  :failed_pipeline,
  :fixed_pipeline,
  :success_pipeline,
  :moved_project,
  :merge_when_pipeline_succeeds
].freeze
EXCLUDED_WATCHER_EVENTS =
[
  :push_to_merge_request,
  :issue_due,
  :success_pipeline
].freeze

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.allowed_fields(source = nil) ⇒ Object


64
65
66
# File 'app/models/notification_setting.rb', line 64

def self.allowed_fields(source = nil)
  NotificationSetting.email_events(source).dup + %i(level notification_email)
end

.email_events(source = nil) ⇒ Object


60
61
62
# File 'app/models/notification_setting.rb', line 60

def self.email_events(source = nil)
  EMAIL_EVENTS
end

.find_or_create_for(source) ⇒ Object


78
79
80
81
82
83
84
85
86
# File 'app/models/notification_setting.rb', line 78

def self.find_or_create_for(source)
  setting = find_or_initialize_by(source: source)

  unless setting.persisted?
    setting.save
  end

  setting
end

Instance Method Details

#email_eventsObject


68
69
70
# File 'app/models/notification_setting.rb', line 68

def email_events
  self.class.email_events(source)
end

#event_enabled?(event) ⇒ Boolean

Returns:

  • (Boolean)

105
106
107
108
109
110
111
# File 'app/models/notification_setting.rb', line 105

def event_enabled?(event)
  # We override these two attributes, so we can't use read_attribute
  return failed_pipeline if event.to_sym == :failed_pipeline
  return fixed_pipeline if event.to_sym == :fixed_pipeline

  has_attribute?(event) && !!read_attribute(event)
end

#failed_pipelineObject Also known as: failed_pipeline?

Allow people to receive both failed pipeline/fixed pipeline notifications if they already have custom notifications enabled, as these are more like mentions than the other custom settings.


91
92
93
94
95
# File 'app/models/notification_setting.rb', line 91

def failed_pipeline
  bool = super

  bool.nil? || bool
end

#fixed_pipelineObject Also known as: fixed_pipeline?


98
99
100
101
102
# File 'app/models/notification_setting.rb', line 98

def fixed_pipeline
  bool = super

  bool.nil? || bool
end

#notification_email_verifiedObject


113
114
115
116
117
118
# File 'app/models/notification_setting.rb', line 113

def notification_email_verified
  return if user.temp_oauth_email?
  return if notification_email.empty?

  errors.add(:notification_email, _("must be an email you have verified")) unless user.verified_emails.include?(notification_email)
end