mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-25 08:09:38 +02:00
Account namespace updates: part 1 (#893)
* Rename accountable types * Merge conflicts * Fix broken tests * Add back sidebar changes
This commit is contained in:
parent
778098ebb0
commit
a947db92b2
54 changed files with 349 additions and 184 deletions
87
db/migrate/20240619125949_rename_accountable_tables.rb
Normal file
87
db/migrate/20240619125949_rename_accountable_tables.rb
Normal file
|
@ -0,0 +1,87 @@
|
|||
class RenameAccountableTables < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
rename_table :account_depositories, :depositories
|
||||
rename_table :account_investments, :investments
|
||||
rename_table :account_credits, :credit_cards
|
||||
rename_table :account_properties, :properties
|
||||
rename_table :account_vehicles, :vehicles
|
||||
rename_table :account_loans, :loans
|
||||
rename_table :account_cryptos, :cryptos
|
||||
rename_table :account_other_assets, :other_assets
|
||||
rename_table :account_other_liabilities, :other_liabilities
|
||||
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
update_accountable_types(
|
||||
'Account::Depository' => 'Depository',
|
||||
'Account::Investment' => 'Investment',
|
||||
'Account::Credit' => 'CreditCard',
|
||||
'Account::Property' => 'Property',
|
||||
'Account::Vehicle' => 'Vehicle',
|
||||
'Account::Loan' => 'Loan',
|
||||
'Account::Crypto' => 'Crypto',
|
||||
'Account::OtherAsset' => 'OtherAsset',
|
||||
'Account::OtherLiability' => 'OtherLiability'
|
||||
)
|
||||
|
||||
remove_column :accounts, :classification, :virtual
|
||||
|
||||
change_table :accounts do |t|
|
||||
t.virtual(
|
||||
:classification,
|
||||
type: :string,
|
||||
stored: true,
|
||||
as: <<-SQL
|
||||
CASE
|
||||
WHEN accountable_type IN ('Loan', 'CreditCard', 'OtherLiability')
|
||||
THEN 'liability'
|
||||
ELSE 'asset'
|
||||
END
|
||||
SQL
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
dir.down do
|
||||
update_accountable_types(
|
||||
'Depository' => 'Account::Depository',
|
||||
'Investment' => 'Account::Investment',
|
||||
'CreditCard' => 'Account::Credit',
|
||||
'Property' => 'Account::Property',
|
||||
'Vehicle' => 'Account::Vehicle',
|
||||
'Loan' => 'Account::Loan',
|
||||
'Crypto' => 'Account::Crypto',
|
||||
'OtherAsset' => 'Account::OtherAsset',
|
||||
'OtherLiability' => 'Account::OtherLiability'
|
||||
)
|
||||
|
||||
remove_column :accounts, :classification, :virtual
|
||||
|
||||
change_table :accounts do |t|
|
||||
t.virtual(
|
||||
:classification,
|
||||
type: :string,
|
||||
stored: true,
|
||||
as: <<-SQL
|
||||
CASE
|
||||
WHEN accountable_type IN ('Account::Loan', 'Account::Credit', 'Account::OtherLiability')
|
||||
THEN 'liability'
|
||||
ELSE 'asset'
|
||||
END
|
||||
SQL
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_accountable_types(mapping)
|
||||
Account.reset_column_information
|
||||
|
||||
mapping.each do |old_type, new_type|
|
||||
Account.where(accountable_type: old_type).update_all(accountable_type: new_type)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue