The personal finance app for everyone (by everyone)
Ruby
Use `dependent: :purge_later` for ActiveRecord attachments (#882)
Juan José Mata0fb9d60ee65a
diff --git a/app/models/account.rb b/app/models/account.rbindex dd95023a2d..d2a86eaf28 100644--- a/app/models/account.rb+++ b/app/models/account.rb@@ -36,7 +36,7 @@ class Account < ApplicationRecordmanual.where.not(status: :pending_deletion)}- has_one_attached :logo+ has_one_attached :logo, dependent: :purge_laterdelegated_type :accountable, types: Accountable::TYPES, dependent: :destroydelegate :subtype, to: :accountable, allow_nil: truediff --git a/app/models/coinbase_item.rb b/app/models/coinbase_item.rbindex 667223a23c..f67aeb7534 100644--- a/app/models/coinbase_item.rb+++ b/app/models/coinbase_item.rb@@ -24,7 +24,7 @@ class CoinbaseItem < ApplicationRecordvalidates :api_secret, presence: truebelongs_to :family- has_one_attached :logo+ has_one_attached :logo, dependent: :purge_laterhas_many :coinbase_accounts, dependent: :destroyhas_many :accounts, through: :coinbase_accountsdiff --git a/app/models/coinstats_item.rb b/app/models/coinstats_item.rbindex 72da54d1ff..c2c9f51fd1 100644--- a/app/models/coinstats_item.rb+++ b/app/models/coinstats_item.rb@@ -22,7 +22,7 @@ class CoinstatsItem < ApplicationRecordvalidates :api_key, presence: truebelongs_to :family- has_one_attached :logo+ has_one_attached :logo, dependent: :purge_laterhas_many :coinstats_accounts, dependent: :destroyhas_many :accounts, through: :coinstats_accountsdiff --git a/app/models/enable_banking_item.rb b/app/models/enable_banking_item.rbindex 8e84cdb621..7de761bde7 100644--- a/app/models/enable_banking_item.rb+++ b/app/models/enable_banking_item.rb@@ -17,7 +17,7 @@ class EnableBankingItem < ApplicationRecordvalidates :client_certificate, presence: true, on: :createbelongs_to :family- has_one_attached :logo+ has_one_attached :logo, dependent: :purge_laterhas_many :enable_banking_accounts, dependent: :destroyhas_many :accounts, through: :enable_banking_accountsdiff --git a/app/models/lunchflow_item.rb b/app/models/lunchflow_item.rbindex 9f4f4cc7ab..1165ba8361 100644--- a/app/models/lunchflow_item.rb+++ b/app/models/lunchflow_item.rb@@ -14,7 +14,7 @@ class LunchflowItem < ApplicationRecordvalidates :api_key, presence: true, on: :createbelongs_to :family- has_one_attached :logo+ has_one_attached :logo, dependent: :purge_laterhas_many :lunchflow_accounts, dependent: :destroyhas_many :accounts, through: :lunchflow_accountsdiff --git a/app/models/mercury_item.rb b/app/models/mercury_item.rbindex a6bcaeb99d..2c781265ac 100644--- a/app/models/mercury_item.rb+++ b/app/models/mercury_item.rb@@ -21,7 +21,7 @@ class MercuryItem < ApplicationRecordvalidates :token, presence: true, on: :createbelongs_to :family- has_one_attached :logo+ has_one_attached :logo, dependent: :purge_laterhas_many :mercury_accounts, dependent: :destroyhas_many :accounts, through: :mercury_accountsdiff --git a/app/models/pdf_import.rb b/app/models/pdf_import.rbindex 0e0250462c..90cb3379d4 100644--- a/app/models/pdf_import.rb+++ b/app/models/pdf_import.rb@@ -1,5 +1,5 @@class PdfImport < Import- has_one_attached :pdf_file+ has_one_attached :pdf_file, dependent: :purge_latervalidates :document_type, inclusion: { in: DOCUMENT_TYPES }, allow_nil: truediff --git a/app/models/plaid_item.rb b/app/models/plaid_item.rbindex c7f3251340..4ac59d0cfd 100644--- a/app/models/plaid_item.rb+++ b/app/models/plaid_item.rb@@ -17,7 +17,7 @@ class PlaidItem < ApplicationRecordbefore_destroy :remove_plaid_itembelongs_to :family- has_one_attached :logo+ has_one_attached :logo, dependent: :purge_laterhas_many :plaid_accounts, dependent: :destroyhas_many :legacy_accounts, through: :plaid_accounts, source: :accountdiff --git a/app/models/simplefin_item.rb b/app/models/simplefin_item.rbindex 07039ad77e..253b6098c3 100644--- a/app/models/simplefin_item.rb+++ b/app/models/simplefin_item.rb@@ -20,7 +20,7 @@ class SimplefinItem < ApplicationRecordbefore_destroy :remove_simplefin_itembelongs_to :family- has_one_attached :logo+ has_one_attached :logo, dependent: :purge_laterhas_many :simplefin_accounts, dependent: :destroyhas_many :legacy_accounts, through: :simplefin_accounts, source: :accountdiff --git a/app/models/snaptrade_item.rb b/app/models/snaptrade_item.rbindex 365d6af3f3..0c627d7d3e 100644--- a/app/models/snaptrade_item.rb+++ b/app/models/snaptrade_item.rb@@ -29,7 +29,7 @@ class SnaptradeItem < ApplicationRecord# via ensure_user_registered!, so we don't validate them on createbelongs_to :family- has_one_attached :logo+ has_one_attached :logo, dependent: :purge_laterhas_many :snaptrade_accounts, dependent: :destroyhas_many :linked_accounts, through: :snaptrade_accountsdiff --git a/app/models/user.rb b/app/models/user.rbindex 3ab9276f86..df8f9fc5e2 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -59,7 +59,7 @@ class User < ApplicationRecordUser.exists? ? fallback_role : :super_adminend- has_one_attached :profile_image do |attachable|+ has_one_attached :profile_image, dependent: :purge_later do |attachable|attachable.variant :thumbnail, resize_to_fill: [ 300, 300 ], convert: :webp, saver: { quality: 80 }attachable.variant :small, resize_to_fill: [ 72, 72 ], convert: :webp, saver: { quality: 80 }, preprocessed: trueenddiff --git a/lib/generators/provider/family/templates/item_model.rb.tt b/lib/generators/provider/family/templates/item_model.rb.ttindex 9cfbf19971..9d4a8e4377 100644--- a/lib/generators/provider/family/templates/item_model.rb.tt+++ b/lib/generators/provider/family/templates/item_model.rb.tt@@ -27,7 +27,7 @@ class <%= class_name %>Item < ApplicationRecord<% end -%>belongs_to :family- has_one_attached :logo+ has_one_attached :logo, dependent: :purge_laterhas_many :<%= file_name %>_accounts, dependent: :destroyhas_many :accounts, through: :<%= file_name %>_accountsdiff --git a/test/models/account_test.rb b/test/models/account_test.rbindex 8a52192af3..5a41f432ec 100644--- a/test/models/account_test.rb+++ b/test/models/account_test.rb@@ -1,7 +1,7 @@require "test_helper"class AccountTest < ActiveSupport::TestCase- include SyncableInterfaceTest, EntriesTestHelper+ include SyncableInterfaceTest, EntriesTestHelper, ActiveJob::TestHelpersetup do@account = @syncable = accounts(:depository)@@ -155,4 +155,21 @@ class AccountTest < ActiveSupport::TestCaseassert @account.taxable?assert_not @account.tax_advantaged?end++ test "destroying account purges attached logo" do+ @account.logo.attach(+ io: StringIO.new("fake-logo-content"),+ filename: "logo.png",+ content_type: "image/png"+ )++ attachment_id = @account.logo.id+ assert ActiveStorage::Attachment.exists?(attachment_id)++ perform_enqueued_jobs do+ @account.destroy!+ end++ assert_not ActiveStorage::Attachment.exists?(attachment_id)+ endenddiff --git a/test/models/pdf_import_test.rb b/test/models/pdf_import_test.rbindex a2491ee1a7..37281b5cc8 100644--- a/test/models/pdf_import_test.rb+++ b/test/models/pdf_import_test.rb@@ -132,4 +132,21 @@ class PdfImportTest < ActiveSupport::TestCaseassert_nil @import.accountassert_not_includes @import.mapping_steps, Import::AccountMappingend++ test "destroying import purges attached pdf_file" do+ @import.pdf_file.attach(+ io: StringIO.new("fake-pdf-content"),+ filename: "statement.pdf",+ content_type: "application/pdf"+ )++ attachment_id = @import.pdf_file.id+ assert ActiveStorage::Attachment.exists?(attachment_id)++ perform_enqueued_jobs do+ @import.destroy!+ end++ assert_not ActiveStorage::Attachment.exists?(attachment_id)+ endenddiff --git a/test/models/user_test.rb b/test/models/user_test.rbindex a1c0c496e6..53b49ed9a4 100644--- a/test/models/user_test.rb+++ b/test/models/user_test.rb@@ -1,10 +1,17 @@require "test_helper"class UserTest < ActiveSupport::TestCase+ include ActiveJob::TestHelper+def setup@user = users(:family_admin)end+ def teardown+ clear_enqueued_jobs+ clear_performed_jobs+ end+test "should be valid" doassert @user.valid?, @user.errors.full_messages.to_sentenceend@@ -348,4 +355,45 @@ class UserTest < ActiveSupport::TestCaseassert_equal :member, User.role_for_new_family_creator(fallback_role: :member)assert_equal "custom_role", User.role_for_new_family_creator(fallback_role: "custom_role")end++ # ActiveStorage attachment cleanup tests+ test "purging a user removes attached profile image" do+ user = users(:family_admin)+ user.profile_image.attach(+ io: StringIO.new("profile-image-data"),+ filename: "profile.png",+ content_type: "image/png"+ )++ attachment_id = user.profile_image.id+ assert ActiveStorage::Attachment.exists?(attachment_id)++ perform_enqueued_jobs do+ user.purge+ end++ assert_not User.exists?(user.id)+ assert_not ActiveStorage::Attachment.exists?(attachment_id)+ end++ test "purging the last user cascades to remove family and its export attachments" do+ family = Family.create!(name: "Solo Family", locale: "en", date_format: "%m-%d-%Y", currency: "USD")+ user = User.create!(family: family, email: "[email protected]", password: "password123")+ export = family.family_exports.create!+ export.export_file.attach(+ io: StringIO.new("export-data"),+ filename: "export.zip",+ content_type: "application/zip"+ )++ export_attachment_id = export.export_file.id+ assert ActiveStorage::Attachment.exists?(export_attachment_id)++ perform_enqueued_jobs do+ user.purge+ end++ assert_not Family.exists?(family.id)+ assert_not ActiveStorage::Attachment.exists?(export_attachment_id)+ endend