1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 05:09:38 +02:00

Add default currencies to forms based on preference (#994)

* Add default currencies to forms based on preference

* Remove dev debugging
This commit is contained in:
Zach Gollwitzer 2024-07-17 08:57:17 -04:00 committed by GitHub
parent e51806b98b
commit e9f42c1a65
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 35 additions and 20 deletions

View file

@ -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])

View file

@ -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"

View file

@ -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") } %>

View file

@ -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" %>

View file

@ -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: {

View file

@ -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") } %>

View file

@ -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>

View file

@ -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:

View file

@ -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

8
db/schema.rb generated
View file

@ -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"

View file

@ -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,

View file

@ -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