mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-10 07:55:21 +02:00
Refactor into concern
This commit is contained in:
parent
8988f7bb6e
commit
1255f96ea1
5 changed files with 33 additions and 41 deletions
|
@ -1,5 +1,5 @@
|
|||
class Account < ApplicationRecord
|
||||
include Syncable, Monetizable, Issuable, Chartable, Enrichable, Linkable
|
||||
include Syncable, Monetizable, Issuable, Chartable, Enrichable, Linkable, Convertible
|
||||
|
||||
validates :name, :balance, :currency, presence: true
|
||||
|
||||
|
|
|
@ -20,15 +20,11 @@ class Account::Balance::Syncer
|
|||
update_account_info
|
||||
end
|
||||
|
||||
sync_exchange_rates
|
||||
account.sync_required_exchange_rates
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def sync_exchange_rates
|
||||
Account::ExchangeRateSync.new(account).sync_rates
|
||||
end
|
||||
|
||||
def sync_holdings
|
||||
@holdings = Account::Holding::Syncer.new(account, strategy: strategy).sync_holdings
|
||||
end
|
||||
|
|
28
app/models/account/convertible.rb
Normal file
28
app/models/account/convertible.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
module Account::Convertible
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def sync_required_exchange_rates
|
||||
unless requires_exchange_rates?
|
||||
Rails.logger.info("No exchange rate sync needed for account #{id}")
|
||||
return
|
||||
end
|
||||
|
||||
rates = ExchangeRate.find_rates(
|
||||
from: currency,
|
||||
to: target_currency,
|
||||
start_date: start_date,
|
||||
cache: true # caches from provider to DB
|
||||
)
|
||||
|
||||
Rails.logger.info("Synced #{rates.count} exchange rates for account #{id}")
|
||||
end
|
||||
|
||||
private
|
||||
def target_currency
|
||||
family.currency
|
||||
end
|
||||
|
||||
def requires_exchange_rates?
|
||||
currency != target_currency
|
||||
end
|
||||
end
|
|
@ -1,32 +0,0 @@
|
|||
class Account::ExchangeRateSync
|
||||
def initialize(account)
|
||||
@account = account
|
||||
end
|
||||
|
||||
def sync_rates
|
||||
Rails.logger.tagged("Account::ExchangeRateSync") do
|
||||
unless needs_rate_sync?
|
||||
Rails.logger.info("No exchange rate sync needed for account #{@account.id}")
|
||||
return
|
||||
end
|
||||
|
||||
rates = ExchangeRate.find_rates(
|
||||
from: @account.currency,
|
||||
to: target_currency,
|
||||
start_date: @account.start_date,
|
||||
cache: true # caches from provider to DB
|
||||
)
|
||||
|
||||
Rails.logger.info("Synced #{rates.count} exchange rates for account #{@account.id}")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def target_currency
|
||||
@account.family.currency
|
||||
end
|
||||
|
||||
def needs_rate_sync?
|
||||
@account.currency != target_currency
|
||||
end
|
||||
end
|
|
@ -1,7 +1,7 @@
|
|||
require "test_helper"
|
||||
require "ostruct"
|
||||
|
||||
class Account::ExchangeRateSyncTest < ActiveSupport::TestCase
|
||||
class Account::ConvertibleTest < ActiveSupport::TestCase
|
||||
include Account::EntriesTestHelper
|
||||
|
||||
setup do
|
||||
|
@ -43,7 +43,7 @@ class Account::ExchangeRateSyncTest < ActiveSupport::TestCase
|
|||
)
|
||||
|
||||
assert_difference "ExchangeRate.count", 7 do
|
||||
Account::ExchangeRateSync.new(@account).sync_rates
|
||||
@account.sync_required_exchange_rates
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -53,7 +53,7 @@ class Account::ExchangeRateSyncTest < ActiveSupport::TestCase
|
|||
@provider.expects(:fetch_exchange_rates).never
|
||||
|
||||
assert_no_difference "ExchangeRate.count" do
|
||||
Account::ExchangeRateSync.new(@account).sync_rates
|
||||
@account.sync_required_exchange_rates
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue