From e9f42c1a65600f1028bf7493da315d00f6234e64 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Wed, 17 Jul 2024 08:57:17 -0400 Subject: [PATCH] Add default currencies to forms based on preference (#994) * Add default currencies to forms based on preference * Remove dev debugging --- app/controllers/accounts_controller.rb | 5 +---- app/helpers/forms_helper.rb | 1 + app/views/accounts/edit.html.erb | 2 +- app/views/accounts/new.html.erb | 2 +- app/views/shared/_money_field.html.erb | 11 +++++++---- app/views/transactions/_form.html.erb | 2 +- app/views/transactions/new.html.erb | 2 +- config/locales/views/transactions/en.yml | 2 ++ ...0717113535_remove_default_from_account_balance.rb | 6 ++++++ db/schema.rb | 8 ++++---- test/controllers/accounts_controller_test.rb | 2 ++ test/fixtures/accounts.yml | 12 ++++++++---- 12 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 db/migrate/20240717113535_remove_default_from_account_balance.rb diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 9eaada46..4f4c2b1e 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -23,10 +23,7 @@ class AccountsController < ApplicationController end def 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.rb index 356f928c..0d2fe24c 100644 --- a/app/helpers/forms_helper.rb +++ b/app/helpers/forms_helper.rb @@ -25,6 +25,7 @@ module FormsHelper render 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.erb index 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 %>
<%= 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.erb index 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 %>
<%= 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.erb index 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) %> +
<%= form.label label || fallback_label, { class: "form-field__label" } %>
- $ - <%= money_field form, money_method, { inline: true, "data-money-field-target" => "amount", default_currency: default_currency } %> + <%= currency.symbol %> + <%= money_field form, money_method, { inline: true, "data-money-field-target" => "amount", default_currency: currency } %>
<% unless hide_currency %>
- <%= 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.erb index 501fd3ac..c474e676 100644 --- a/app/views/transactions/_form.html.erb +++ b/app/views/transactions/_form.html.erb @@ -13,7 +13,7 @@
<%= 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.erb index ba675f63..cd86807c 100644 --- a/app/views/transactions/new.html.erb +++ b/app/views/transactions/new.html.erb @@ -1,7 +1,7 @@ <%= modal do %>
-

New transaction

+

<%= t(".new_transaction") %>

<%= lucide_icon "x", class: "w-5 h-5 text-gray-500", data: { action: "click->modal#close" } %>
diff --git a/config/locales/views/transactions/en.yml b/config/locales/views/transactions/en.yml index 00b4474f..5b253e12 100644 --- a/config/locales/views/transactions/en.yml +++ b/config/locales/views/transactions/en.yml @@ -40,6 +40,8 @@ en: transaction: transaction mark_transfers: success: Marked as transfer + new: + new_transaction: New transaction pagination: rows_per_page: Rows per page unmark_transfers: diff --git a/db/migrate/20240717113535_remove_default_from_account_balance.rb b/db/migrate/20240717113535_remove_default_from_account_balance.rb new file mode 100644 index 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 +end diff --git a/db/schema.rb b/db/schema.rb index 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 database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -113,12 +113,12 @@ ActiveRecord::Schema[7.2].define(version: 2024_07_10_184249) do t.datetime "updated_at", null: false t.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: false t.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: true t.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.rb index 31fd9b2f..9f58c36a 100644 --- a/test/controllers/accounts_controller_test.rb +++ b/test/controllers/accounts_controller_test.rb @@ -86,6 +86,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest account: { accountable_type: "Depository", balance: 200, + currency: "USD", subtype: "checking", institution_id: institutions(:chase).id } @@ -102,6 +103,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest account: { 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.yml index 74e09ddb..eeb616f3 100644 --- a/test/fixtures/accounts.yml +++ b/test/fixtures/accounts.yml @@ -2,6 +2,7 @@ other_asset: family: dylan_family name: Collectable Account balance: 550 + currency: USD accountable_type: OtherAsset accountable: one @@ -9,6 +10,7 @@ other_liability: family: dylan_family name: IOU (personal debt to friend) balance: 200 + currency: USD accountable_type: OtherLiability accountable: one @@ -16,6 +18,7 @@ depository: family: dylan_family name: Checking Account balance: 5000 + currency: USD accountable_type: Depository accountable: one institution: chase @@ -24,6 +27,7 @@ credit_card: family: dylan_family name: Credit Card balance: 1000 + currency: USD accountable_type: CreditCard accountable: one institution: chase @@ -31,31 +35,31 @@ credit_card: investment: family: dylan_family name: Robinhood Brokerage Account - currency: USD balance: 10000 + currency: USD accountable_type: Investment accountable: one loan: family: dylan_family name: Mortgage Loan - currency: USD balance: 500000 + currency: USD accountable_type: Loan accountable: one property: family: dylan_family name: 123 Maybe Court - currency: USD balance: 550000 + currency: USD accountable_type: Property accountable: one vehicle: family: dylan_family name: Honda Accord - currency: USD balance: 18000 + currency: USD accountable_type: Vehicle accountable: one