Class: NotificationSetting

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

Constant Summary collapse

EMAIL_EVENTS =
[
  :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,
  :merge_merge_request,
  :failed_pipeline,
  :fixed_pipeline,
  :success_pipeline,
  :moved_project
].freeze
EXCLUDED_WATCHER_EVENTS =
[
  :push_to_merge_request,
  :issue_due,
  :success_pipeline
].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

.allowed_fields(source = nil) ⇒ Object


58
59
60
# File 'app/models/notification_setting.rb', line 58

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

.email_events(source = nil) ⇒ Object

Update unfound_translations.rb when events are changed


54
55
56
# File 'app/models/notification_setting.rb', line 54

def self.email_events(source = nil)
  EMAIL_EVENTS
end

.find_or_create_for(source) ⇒ Object


72
73
74
75
76
77
78
79
80
# File 'app/models/notification_setting.rb', line 72

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


62
63
64
# File 'app/models/notification_setting.rb', line 62

def email_events
  self.class.email_events(source)
end

#event_enabled?(event) ⇒ Boolean

Returns:

  • (Boolean)

99
100
101
102
103
104
105
# File 'app/models/notification_setting.rb', line 99

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.


85
86
87
88
89
# File 'app/models/notification_setting.rb', line 85

def failed_pipeline
  bool = super

  bool.nil? || bool
end

#fixed_pipelineObject Also known as: fixed_pipeline?


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

def fixed_pipeline
  bool = super

  bool.nil? || bool
end

#owns_notification_emailObject


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

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

  errors.add(:notification_email, _("is not an email you own")) unless user.verified_emails.include?(notification_email)
end