e51f0bfee7
Account incineration could leave orphaned records due to missing cascade declarations and async jobs failing when the account was already gone. Cascade fixes: - Add Account::Searchable concern to clean up Search::Query (delete_all) and Search::Record (destroy_all, respects SQLite FTS dependent: :destroy) - Add before_destroy in Account::Storage to delete storage entries - Suppress storage entry recording during incineration so attachment purge callbacks don't create entries or enqueue materialize jobs - Guard Access#clean_inaccessible_data_later with unless user.destroyed? to avoid enqueuing pointless jobs during user cascade Job tenancy: - Extract AccountTenanted concern from the global ActiveJob initializer into ApplicationJob (include) with targeted prepends for ActionMailer::MailDeliveryJob and Turbo broadcast jobs - Defer account resolution from deserialize to perform so that missing accounts raise DeserializationError inside the execution path where discard_on can handle it Tests: - Comprehensive incineration test covering 35 model types with before/after assertions and full enqueued job processing - Mailer deliver_later test verifying account context survives job serialization for multi-account users - Turbo broadcast test verifying account-scoped URLs in rendered partials
57 lines
1.6 KiB
Ruby
57 lines
1.6 KiB
Ruby
class Account < ApplicationRecord
|
|
include Account::Storage, Cancellable, Entropic, Incineratable, MultiTenantable, Searchable, Seedeable
|
|
|
|
has_one :join_code, dependent: :destroy
|
|
has_many :users, dependent: :destroy
|
|
has_many :boards, dependent: :destroy
|
|
has_many :cards, dependent: :destroy
|
|
has_many :webhooks, dependent: :destroy
|
|
has_many :tags, dependent: :destroy
|
|
has_many :columns, dependent: :destroy
|
|
has_many :entropies, dependent: :destroy
|
|
has_many :exports, class_name: "Account::Export", dependent: :destroy
|
|
has_many :imports, class_name: "Account::Import", dependent: :destroy
|
|
|
|
scope :importing, -> { left_joins(:imports).where(account_imports: { status: %i[pending processing failed] }) }
|
|
scope :active, -> { where.missing(:cancellation).and(where.not(id: importing)) }
|
|
|
|
before_create :assign_external_account_id
|
|
after_create :create_join_code
|
|
|
|
validates :name, presence: true
|
|
|
|
class << self
|
|
def create_with_owner(account:, owner:)
|
|
create!(**account).tap do |account|
|
|
account.users.create!(role: :system, name: "System")
|
|
account.users.create!(**owner.with_defaults(role: :owner, verified_at: Time.current))
|
|
end
|
|
end
|
|
end
|
|
|
|
def slug
|
|
"/#{AccountSlug.encode(external_account_id)}"
|
|
end
|
|
|
|
def account
|
|
self
|
|
end
|
|
|
|
def system_user
|
|
users.find_by!(role: :system)
|
|
end
|
|
|
|
def active?
|
|
!cancelled? && !importing?
|
|
end
|
|
|
|
def importing?
|
|
imports.where(status: %i[pending processing failed]).exists?
|
|
end
|
|
|
|
private
|
|
def assign_external_account_id
|
|
self.external_account_id ||= ExternalIdSequence.next
|
|
end
|
|
end
|