From e94c1028c1e829f899b0d0f56313cc62df6f9a0a Mon Sep 17 00:00:00 2001 From: Sean McGivern Date: Thu, 8 Jun 2017 17:10:35 +0100 Subject: Deserialise existing custom notification settings Create a post-deployment migration to update all existing notification settings with at least one custom level enabled to the new format. Also handle the same conversion when updating settings, to catch any stragglers. --- app/models/notification_setting.rb | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/notification_setting.rb b/app/models/notification_setting.rb index 2a53484f96f..b0df7aeb323 100644 --- a/app/models/notification_setting.rb +++ b/app/models/notification_setting.rb @@ -42,6 +42,7 @@ class NotificationSetting < ActiveRecord::Base ].freeze store :events, coder: JSON + before_save :convert_events def self.find_or_create_for(source) setting = find_or_initialize_by(source: source) @@ -53,21 +54,42 @@ class NotificationSetting < ActiveRecord::Base setting end - EMAIL_EVENTS.each do |event| + # 1. Check if this event has a value stored in its database column. + # 2. If it does, return that value. + # 3. If it doesn't (the value is nil), return the value from the serialized + # JSON hash in `events`. + (EMAIL_EVENTS - [:failed_pipeline]).each do |event| define_method(event) do bool = super() bool.nil? ? !!events[event] : bool end + + alias_method :"#{event}?", event end # Allow people to receive failed pipeline notifications if they already have # custom notifications enabled, as these are more like mentions than the other # custom settings. def failed_pipeline - bool = read_attribute(:failed_pipeline) + bool = super bool = events[:failed_pipeline] if bool.nil? bool.nil? || bool end + alias_method :failed_pipeline?, :failed_pipeline + + def event_enabled?(event) + respond_to?(event) && public_send(event) + end + + def convert_events + return if events_before_type_cast.nil? + + EMAIL_EVENTS.each do |event| + write_attribute(event, public_send(event)) + end + + write_attribute(:events, nil) + end end -- cgit v1.2.1