cfa0149564
Add a beacon to persist timezones when these change (or the first time)
51 lines
1.0 KiB
Ruby
51 lines
1.0 KiB
Ruby
class User::Settings < ApplicationRecord
|
|
belongs_to :user
|
|
|
|
enum :bundle_email_frequency, %i[ never every_few_hours daily weekly ],
|
|
default: :every_few_hours, prefix: :bundle_email
|
|
|
|
after_update :review_pending_bundles, if: :saved_change_to_bundle_email_frequency?
|
|
|
|
def bundle_aggregation_period
|
|
case bundle_email_frequency
|
|
when "every_few_hours"
|
|
4.hours
|
|
when "daily"
|
|
1.day
|
|
when "weekly"
|
|
1.week
|
|
else
|
|
1.day
|
|
end
|
|
end
|
|
|
|
def bundling_emails?
|
|
!bundle_email_never?
|
|
end
|
|
|
|
def timezone
|
|
ActiveSupport::TimeZone[timezone_name] if timezone_name.present?
|
|
end
|
|
|
|
private
|
|
def review_pending_bundles
|
|
if bundling_emails?
|
|
flush_pending_bundles
|
|
else
|
|
cancel_pending_bundles
|
|
end
|
|
end
|
|
|
|
def cancel_pending_bundles
|
|
user.notification_bundles.pending.find_each do |bundle|
|
|
bundle.destroy
|
|
end
|
|
end
|
|
|
|
def flush_pending_bundles
|
|
user.notification_bundles.pending.find_each do |bundle|
|
|
bundle.deliver_later
|
|
end
|
|
end
|
|
end
|