The personal finance app for everyone
Add default currencies to forms based on preference (#994)
Zach Gollwitzere9f42c1a6560
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rbindex 9eaada46..4f4c2b1e 100644--- a/app/controllers/accounts_controller.rb+++ b/app/controllers/accounts_controller.rb@@ -23,10 +23,7 @@ class AccountsController < ApplicationControllerenddef new- @account = Account.new(- balance: nil,- accountable: Accountable.from_type(params[:type])&.new- )+ @account = Account.new(accountable: Accountable.from_type(params[:type])&.new)if params[:institution_id]@account.institution = Current.family.institutions.find_by(id: params[:institution_id])diff --git a/app/helpers/forms_helper.rb b/app/helpers/forms_helper.rbindex 356f928c..0d2fe24c 100644--- a/app/helpers/forms_helper.rb+++ b/app/helpers/forms_helper.rb@@ -25,6 +25,7 @@ module FormsHelperrender partial: "shared/money_field", locals: {form: form,money_method: money_method,+ default_currency: options[:default_currency] || "USD",disable_currency: options[:disable_currency] || false,hide_currency: options[:hide_currency] || false,label: options[:label] || "Amount"diff --git a/app/views/accounts/edit.html.erb b/app/views/accounts/edit.html.erbindex 0b14157e..77b03030 100644--- a/app/views/accounts/edit.html.erb+++ b/app/views/accounts/edit.html.erb@@ -7,7 +7,7 @@<%= styled_form_with model: @account, class: "space-y-4", data: { turbo_frame: "_top" } do |f| %><%= f.text_field :name, label: t(".name") %>- <%= money_with_currency_field f, :balance_money, label: t(".balance"), disable_currency: true %>+ <%= money_with_currency_field f, :balance_money, label: t(".balance"), default_currency: @account.currency, disable_currency: true %><div class="relative"><%= f.collection_select :institution_id, Current.family.institutions.alphabetically, :id, :name, { include_blank: t(".ungrouped"), label: t(".institution") } %>diff --git a/app/views/accounts/new.html.erb b/app/views/accounts/new.html.erbindex 04590414..ff011fc7 100644--- a/app/views/accounts/new.html.erb+++ b/app/views/accounts/new.html.erb@@ -79,7 +79,7 @@<%= f.text_field :name, placeholder: t(".name.placeholder"), required: "required", label: t(".name.label"), autofocus: true %><%= f.collection_select :institution_id, Current.family.institutions.alphabetically, :id, :name, { include_blank: t(".ungrouped"), label: t(".institution") } %><%= render "accounts/accountables/#{permitted_accountable_partial(@account.accountable_type)}", f: f %>- <%= money_with_currency_field f, :balance_money, label: t(".balance"), required: "required" %>+ <%= money_with_currency_field f, :balance_money, label: t(".balance"), required: "required", default_currency: Current.family.currency %><div><%= check_box_tag :add_start_values, class: "maybe-checkbox maybe-checkbox--light peer mb-1" %>diff --git a/app/views/shared/_money_field.html.erb b/app/views/shared/_money_field.html.erbindex 7b3288b0..fc662791 100644--- a/app/views/shared/_money_field.html.erb+++ b/app/views/shared/_money_field.html.erb@@ -1,16 +1,19 @@-<%# locals: (form:, money_method:, default_currency: "USD", disable_currency: false, hide_currency: false, label: nil) %>+<%# locals: (form:, money_method:, default_currency:, disable_currency: false, hide_currency: false, label: nil) %>+<% fallback_label = t(".money-label") %>+<% currency = form.object.send(money_method)&.currency || Money::Currency.new(default_currency) %>+<div class="form-field pr-0" data-controller="money-field"><%= form.label label || fallback_label, { class: "form-field__label" } %><div class="flex items-center gap-1"><div class="flex items-center grow gap-1">- <span class="text-gray-500 text-sm" data-money-field-target="symbol">$</span>- <%= money_field form, money_method, { inline: true, "data-money-field-target" => "amount", default_currency: default_currency } %>+ <span class="text-gray-500 text-sm" data-money-field-target="symbol"><%= currency.symbol %></span>+ <%= money_field form, money_method, { inline: true, "data-money-field-target" => "amount", default_currency: currency } %></div><% unless hide_currency %><div>- <%= currency_select form, :currency, { inline: true }, {+ <%= currency_select form, :currency, { inline: true, selected: currency.iso_code }, {class: "form-field__input text-right pr-8 disabled:text-gray-500",disabled: disable_currency,data: {diff --git a/app/views/transactions/_form.html.erb b/app/views/transactions/_form.html.erbindex 501fd3ac..c474e676 100644--- a/app/views/transactions/_form.html.erb+++ b/app/views/transactions/_form.html.erb@@ -13,7 +13,7 @@<section class="space-y-2"><%= f.text_field :name, label: t(".description"), placeholder: t(".description_placeholder"), required: true %><%= f.collection_select :account_id, Current.family.accounts.alphabetically, :id, :name, { prompt: t(".account_prompt"), label: t(".account") }, required: true %>- <%= money_with_currency_field f, :amount_money, label: t(".amount"), required: true %>+ <%= money_with_currency_field f, :amount_money, label: t(".amount"), required: true, default_currency: @entry.account&.currency || Current.family.currency %><%= f.hidden_field :entryable_type, value: "Account::Transaction" %><%= f.fields_for :entryable do |ef| %><%= ef.collection_select :category_id, Current.family.categories.alphabetically, :id, :name, { prompt: t(".category_prompt"), label: t(".category") } %>diff --git a/app/views/transactions/new.html.erb b/app/views/transactions/new.html.erbindex ba675f63..cd86807c 100644--- a/app/views/transactions/new.html.erb+++ b/app/views/transactions/new.html.erb@@ -1,7 +1,7 @@<%= modal do %><article class="mx-auto p-4 space-y-4 w-screen max-w-xl"><header class="flex justify-between">- <h2 class="font-medium text-xl">New transaction</h2>+ <h2 class="font-medium text-xl"><%= t(".new_transaction") %></h2><%= lucide_icon "x", class: "w-5 h-5 text-gray-500", data: { action: "click->modal#close" } %></header>diff --git a/config/locales/views/transactions/en.yml b/config/locales/views/transactions/en.ymlindex 00b4474f..5b253e12 100644--- a/config/locales/views/transactions/en.yml+++ b/config/locales/views/transactions/en.yml@@ -40,6 +40,8 @@ en:transaction: transactionmark_transfers:success: Marked as transfer+ new:+ new_transaction: New transactionpagination:rows_per_page: Rows per pageunmark_transfers:diff --git a/db/migrate/20240717113535_remove_default_from_account_balance.rb b/db/migrate/20240717113535_remove_default_from_account_balance.rbnew file mode 100644index 00000000..3a6dbad0--- /dev/null+++ b/db/migrate/20240717113535_remove_default_from_account_balance.rb@@ -0,0 +1,6 @@+class RemoveDefaultFromAccountBalance < ActiveRecord::Migration[7.2]+ def change+ change_column_default :accounts, :balance, from: "0.0", to: nil+ change_column_default :accounts, :currency, from: "USD", to: nil+ end+enddiff --git a/db/schema.rb b/db/schema.rbindex eebb6a59..39a0d8b1 100644--- a/db/schema.rb+++ b/db/schema.rb@@ -10,7 +10,7 @@## It's strongly recommended that you check this file into your version control system.-ActiveRecord::Schema[7.2].define(version: 2024_07_10_184249) do+ActiveRecord::Schema[7.2].define(version: 2024_07_17_113535) do# These are extensions that must be enabled in order to support this databaseenable_extension "pgcrypto"enable_extension "plpgsql"@@ -113,12 +113,12 @@ ActiveRecord::Schema[7.2].define(version: 2024_07_10_184249) dot.datetime "updated_at", null: falset.string "accountable_type"t.uuid "accountable_id"- t.decimal "balance", precision: 19, scale: 4, default: "0.0"- t.string "currency", default: "USD"+ t.decimal "balance", precision: 19, scale: 4+ t.string "currency"t.boolean "is_active", default: true, null: falset.date "last_sync_date"t.uuid "institution_id"- t.virtual "classification", type: :string, as: "\nCASE\n WHEN ((accountable_type)::text = ANY ((ARRAY['Loan'::character varying, 'CreditCard'::character varying, 'OtherLiability'::character varying])::text[])) THEN 'liability'::text\n ELSE 'asset'::text\nEND", stored: true+ t.virtual "classification", type: :string, as: "\nCASE\n WHEN ((accountable_type)::text = ANY (ARRAY[('Loan'::character varying)::text, ('CreditCard'::character varying)::text, ('OtherLiability'::character varying)::text])) THEN 'liability'::text\n ELSE 'asset'::text\nEND", stored: truet.index ["accountable_type"], name: "index_accounts_on_accountable_type"t.index ["family_id"], name: "index_accounts_on_family_id"t.index ["institution_id"], name: "index_accounts_on_institution_id"diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rbindex 31fd9b2f..9f58c36a 100644--- a/test/controllers/accounts_controller_test.rb+++ b/test/controllers/accounts_controller_test.rb@@ -86,6 +86,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTestaccount: {accountable_type: "Depository",balance: 200,+ currency: "USD",subtype: "checking",institution_id: institutions(:chase).id}@@ -102,6 +103,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTestaccount: {accountable_type: "Depository",balance: 200,+ currency: "USD",subtype: "checking",institution_id: institutions(:chase).id,start_balance: 100,diff --git a/test/fixtures/accounts.yml b/test/fixtures/accounts.ymlindex 74e09ddb..eeb616f3 100644--- a/test/fixtures/accounts.yml+++ b/test/fixtures/accounts.yml@@ -2,6 +2,7 @@ other_asset:family: dylan_familyname: Collectable Accountbalance: 550+ currency: USDaccountable_type: OtherAssetaccountable: one@@ -9,6 +10,7 @@ other_liability:family: dylan_familyname: IOU (personal debt to friend)balance: 200+ currency: USDaccountable_type: OtherLiabilityaccountable: one@@ -16,6 +18,7 @@ depository:family: dylan_familyname: Checking Accountbalance: 5000+ currency: USDaccountable_type: Depositoryaccountable: oneinstitution: chase@@ -24,6 +27,7 @@ credit_card:family: dylan_familyname: Credit Cardbalance: 1000+ currency: USDaccountable_type: CreditCardaccountable: oneinstitution: chase@@ -31,31 +35,31 @@ credit_card:investment:family: dylan_familyname: Robinhood Brokerage Account- currency: USDbalance: 10000+ currency: USDaccountable_type: Investmentaccountable: oneloan:family: dylan_familyname: Mortgage Loan- currency: USDbalance: 500000+ currency: USDaccountable_type: Loanaccountable: oneproperty:family: dylan_familyname: 123 Maybe Court- currency: USDbalance: 550000+ currency: USDaccountable_type: Propertyaccountable: onevehicle:family: dylan_familyname: Honda Accord- currency: USDbalance: 18000+ currency: USDaccountable_type: Vehicleaccountable: one