1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 23:59:40 +02:00

Support multi-currency transfers (#1175)

This commit is contained in:
Zach Gollwitzer 2024-09-13 11:45:19 -04:00 committed by GitHub
parent c28dd8f940
commit 949d3d80fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 5 additions and 18 deletions

View file

@ -40,6 +40,6 @@ class Account::TransfersController < ApplicationController
end
def transfer_params
params.require(:account_transfer).permit(:from_account_id, :to_account_id, :amount, :currency, :date, :name)
params.require(:account_transfer).permit(:from_account_id, :to_account_id, :amount, :date, :name)
end
end

View file

@ -204,7 +204,6 @@ class Account::Entry < ApplicationRecord
current_qty = account.holding_qty(account_trade.security)
if current_qty < account_trade.qty.abs
# i18n-tasks-use t('activerecord.errors.models.account/entry.attributes.base.invalid_sell_quantity')
errors.add(
:base,
:invalid_sell_quantity,

View file

@ -46,7 +46,7 @@ class Account::Transfer < ApplicationRecord
def build_from_accounts(from_account, to_account, date:, amount:, currency:, name:)
outflow = from_account.entries.build \
amount: amount.abs,
currency: currency,
currency: from_account.currency,
date: date,
name: name,
marked_as_transfer: true,
@ -54,7 +54,7 @@ class Account::Transfer < ApplicationRecord
inflow = to_account.entries.build \
amount: amount.abs * -1,
currency: currency,
currency: from_account.currency,
date: date,
name: name,
marked_as_transfer: true,
@ -72,27 +72,23 @@ class Account::Transfer < ApplicationRecord
def transaction_count
unless entries.size == 2
# i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_have_exactly_2_entries')
errors.add :entries, :must_have_exactly_2_entries
end
end
def from_different_accounts
accounts = entries.map { |e| e.account_id }.uniq
# i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_be_from_different_accounts')
errors.add :entries, :must_be_from_different_accounts if accounts.size < entries.size
end
def net_zero_flows
unless entries.sum(&:amount).zero?
# i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_have_an_inflow_and_outflow_that_net_to_zero')
errors.add :entries, :must_have_an_inflow_and_outflow_that_net_to_zero
end
end
def all_transactions_marked
unless entries.all?(&:marked_as_transfer)
# i18n-tasks-use t('activerecord.errors.models.account/transfer.attributes.entries.must_be_marked_as_transfer')
errors.add :entries, :must_be_marked_as_transfer
end
end

View file

@ -179,7 +179,6 @@ class Import < ApplicationRecord
begin
CSV.parse(raw_file_str || "", col_sep:)
rescue CSV::MalformedCSVError
# i18n-tasks-use t('activerecord.errors.models.import.attributes.raw_file_str.invalid_csv_format')
errors.add(:raw_file_str, :invalid_csv_format)
end
end

View file

@ -83,21 +83,17 @@ class TimeSeries::Trend
def values_must_be_of_same_type
unless current.class == previous.class || [ previous, current ].any?(&:nil?)
# i18n-tasks-use t('activemodel.errors.models.time_series/trend.attributes.current.must_be_of_the_same_type_as_previous')
errors.add :current, :must_be_of_the_same_type_as_previous
# i18n-tasks-use t('activemodel.errors.models.time_series/trend.attributes.previous.must_be_of_the_same_type_as_current')
errors.add :previous, :must_be_of_the_same_type_as_current
end
end
def values_must_be_of_known_type
unless current.is_a?(Money) || current.is_a?(Numeric) || current.nil?
# i18n-tasks-use t('activemodel.errors.models.time_series/trend.attributes.current.must_be_of_type_money_numeric_or_nil')
errors.add :current, :must_be_of_type_money_numeric_or_nil
end
unless previous.is_a?(Money) || previous.is_a?(Numeric) || previous.nil?
# i18n-tasks-use t('activemodel.errors.models.time_series/trend.attributes.previous.must_be_of_type_money_numeric_or_nil')
errors.add :previous, :must_be_of_type_money_numeric_or_nil
end
end

View file

@ -40,7 +40,6 @@ class TimeSeries::Value
def value_must_be_of_known_type
unless value.is_a?(Money) || value.is_a?(Numeric)
# i18n-tasks-use t('activemodel.errors.models.time_series/value.attributes.value.must_be_a_money_or_numeric')
errors.add :value, :must_be_a_money_or_numeric
end
end

View file

@ -55,7 +55,6 @@ class User < ApplicationRecord
def can_deactivate
if admin? && family.users.count > 1
# i18n-tasks-use t('activerecord.errors.models.user.attributes.base.cannot_deactivate_admin_with_other_users')
errors.add(:base, :cannot_deactivate_admin_with_other_users)
end
end
@ -84,7 +83,6 @@ class User < ApplicationRecord
def profile_image_size
if profile_image.attached? && profile_image.byte_size > 5.megabytes
# i18n-tasks-use t('activerecord.errors.models.user.attributes.profile_image.invalid_file_size')
errors.add(:profile_image, :invalid_file_size, max_megabytes: 5)
end
end

View file

@ -30,7 +30,6 @@
<%= f.collection_select :from_account_id, Current.family.accounts.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".from") }, required: true %>
<%= f.collection_select :to_account_id, Current.family.accounts.alphabetically, :id, :name, { prompt: t(".select_account"), label: t(".to") }, required: true %>
<%= money_field f, :amount_money, label: t(".amount"), required: true %>
<%= f.hidden_field :currency, value: Current.family.currency %>
<%= f.date_field :date, value: transfer.date, label: t(".date"), required: true, max: Date.current %>
</section>

View file

@ -26,6 +26,8 @@ search:
ignore_unused:
- 'activerecord.attributes.*' # i18n-tasks does not detect these on forms, forms validations (https://github.com/glebm/i18n-tasks/blob/0b4b483c82664f26c5696fb0f6aa1297356e4683/templates/config/i18n-tasks.yml#L146)
- 'activerecord.models.*' # i18n-tasks does not detect use in dynamic model names (e.g. object.model_name.human)
- 'activerecord.errors.models*'
- 'activemodel.errors.models.*'
- 'helpers.submit.*' # i18n-tasks does not detect used at forms
- 'helpers.label.*' # i18n-tasks does not detect used at forms
- 'accounts.show.sync_message_*' # messages generated in the sync ActiveJob

View file

@ -18,7 +18,6 @@ class Account::TransfersControllerTest < ActionDispatch::IntegrationTest
to_account_id: accounts(:credit_card).id,
date: Date.current,
amount: 100,
currency: "USD",
name: "Test Transfer"
}
}