fe6df7085f
Schema: - add account_id to tables it was missing from - make account_id a required column everywhere - add [account_id] indexes, or add `account_id` to existing indices Models: - add `belongs_to :account` to all models (default to using a domain model's account whenever possible) - add account_id in all the necessary fixtures - add account_id to insert_all hashes - pass account_id to a few initialize calls Miscellaneous: - update the import script to set account_id Note that I'm not adding account_id to the join tables primarily because I couldn't think of an easy way to populate it without making it a full Join model, and that was more work than I have time to take on right now.
58 lines
1.2 KiB
Ruby
58 lines
1.2 KiB
Ruby
class User::Settings < ApplicationRecord
|
|
belongs_to :account, default: -> { user.account }
|
|
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? && !user.system? && user.active?
|
|
end
|
|
|
|
def timezone
|
|
if timezone_name.present?
|
|
ActiveSupport::TimeZone[timezone_name] || default_timezone
|
|
else
|
|
default_timezone
|
|
end
|
|
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(&:flush)
|
|
end
|
|
|
|
def default_timezone
|
|
ActiveSupport::TimeZone["UTC"]
|
|
end
|
|
end
|