Commit f4b5fcbc authored by Sean McGivern's avatar Sean McGivern

Add columns for custom notification settings

Add columns for each custom notification level, defaulting to null. Read from
those columns if non-null, otherwise fall back to the serialized column. Writing
will write to the new column if `events` isn't manually set.
parent 051b8dc4
...@@ -41,10 +41,7 @@ class NotificationSetting < ActiveRecord::Base ...@@ -41,10 +41,7 @@ class NotificationSetting < ActiveRecord::Base
:success_pipeline :success_pipeline
].freeze ].freeze
store :events, accessors: EMAIL_EVENTS, coder: JSON store :events, coder: JSON
before_create :set_events
before_save :events_to_boolean
def self.find_or_create_for(source) def self.find_or_create_for(source)
setting = find_or_initialize_by(source: source) setting = find_or_initialize_by(source: source)
...@@ -56,20 +53,11 @@ class NotificationSetting < ActiveRecord::Base ...@@ -56,20 +53,11 @@ class NotificationSetting < ActiveRecord::Base
setting setting
end end
# Set all event attributes to false when level is not custom or being initialized for UX reasons
def set_events
return if custom?
self.events = {}
end
# Validates store accessors values as boolean
# It is a text field so it does not cast correct boolean values in JSON
def events_to_boolean
EMAIL_EVENTS.each do |event| EMAIL_EVENTS.each do |event|
bool = ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(public_send(event)) define_method(event) do
bool = super()
events[event] = bool bool.nil? ? !!events[event] : bool
end end
end end
...@@ -77,7 +65,8 @@ class NotificationSetting < ActiveRecord::Base ...@@ -77,7 +65,8 @@ class NotificationSetting < ActiveRecord::Base
# custom notifications enabled, as these are more like mentions than the other # custom notifications enabled, as these are more like mentions than the other
# custom settings. # custom settings.
def failed_pipeline def failed_pipeline
bool = super bool = read_attribute(:failed_pipeline)
bool = events[:failed_pipeline] if bool.nil?
bool.nil? || bool bool.nil? || bool
end end
......
class AddNotificationSettingColumns < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
COLUMNS = [
:new_note,
:new_issue,
:reopen_issue,
:close_issue,
:reassign_issue,
:new_merge_request,
:reopen_merge_request,
:close_merge_request,
:reassign_merge_request,
:merge_merge_request,
:failed_pipeline,
:success_pipeline
]
def change
COLUMNS.each do |column|
add_column(:notification_settings, column, :boolean)
end
end
end
...@@ -878,6 +878,18 @@ ActiveRecord::Schema.define(version: 20170606202615) do ...@@ -878,6 +878,18 @@ ActiveRecord::Schema.define(version: 20170606202615) do
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.text "events" t.text "events"
t.boolean "new_note"
t.boolean "new_issue"
t.boolean "reopen_issue"
t.boolean "close_issue"
t.boolean "reassign_issue"
t.boolean "new_merge_request"
t.boolean "reopen_merge_request"
t.boolean "close_merge_request"
t.boolean "reassign_merge_request"
t.boolean "merge_merge_request"
t.boolean "failed_pipeline"
t.boolean "success_pipeline"
end end
add_index "notification_settings", ["source_id", "source_type"], name: "index_notification_settings_on_source_id_and_source_type", using: :btree add_index "notification_settings", ["source_id", "source_type"], name: "index_notification_settings_on_source_id_and_source_type", using: :btree
......
...@@ -58,7 +58,10 @@ describe NotificationSettingsController do ...@@ -58,7 +58,10 @@ describe NotificationSettingsController do
expect(response.status).to eq 200 expect(response.status).to eq 200
expect(notification_setting.level).to eq("custom") expect(notification_setting.level).to eq("custom")
expect(notification_setting.events).to eq(custom_events)
custom_events.each do |event, value|
expect(notification_setting.send(event)).to eq(value)
end
end end
end end
end end
...@@ -86,7 +89,10 @@ describe NotificationSettingsController do ...@@ -86,7 +89,10 @@ describe NotificationSettingsController do
expect(response.status).to eq 200 expect(response.status).to eq 200
expect(notification_setting.level).to eq("custom") expect(notification_setting.level).to eq("custom")
expect(notification_setting.events).to eq(custom_events)
custom_events.each do |event, value|
expect(notification_setting.send(event)).to eq(value)
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment