Address remaining CodeRabbit comments from PR #267 #351 (#451)

* Address remaining CodeRabbit comments from PR #267

This commit addresses the remaining unresolved code review comments:

1. Fix down migration in drop_was_merged_from_transactions.rb
   - Add null: false, default: false constraints to match original column
   - Ensures proper rollback compatibility

2. Fix bare rescue in maps_helper.rb compute_duplicate_only_flag
   - Replace bare rescue with rescue StandardError => e
   - Add proper logging for debugging
   - Follows Ruby best practices by being explicit about exception handling

These changes improve code quality and follow Rails/Ruby best practices.

* Refactor `SimplefinItemsController` and add tests for balances sync and account relinking behavior

- Replaced direct sync execution with `SyncJob` for asynchronous handling of balances sync.
- Updated account relinking logic to prevent disabling accounts with other active provider links.
- Removed unused `compute_relink_candidates` method.
- Added tests to verify `balances` action enqueues `SyncJob` and relinking respects account-provider relationships.

* Refactor balances sync to use runtime-only `balances_only` flag

- Replaced persistent `sync_stats` usage with runtime `balances_only?` predicate via `define_singleton_method`.
- Updated `SimplefinItemsController` `balances` action to pass `balances_only` flag to `SyncJob`.
- Enhanced `SyncJob` to attach transient `balances_only?` flag for execution.
- Adjusted `SimplefinItem::Syncer` logic to rely on the runtime `balances_only?` method.
- Updated controller tests to validate runtime flag usage in `SyncJob`.

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com>
This commit is contained in:
LPW
2025-12-15 03:47:16 -05:00
committed by GitHub
parent 92cf98551b
commit 4d3d9d10df
6 changed files with 110 additions and 119 deletions

View File

@@ -12,12 +12,11 @@ class SimplefinItem::Syncer
begin
if simplefin_item.simplefin_accounts.joins(:account).count == 0
sync.update!(status_text: "Discovering accounts (balances only)...") if sync.respond_to?(:status_text)
# Pre-mark the sync as balances_only so downstream completion code does not
# bump last_synced_at. The importer also sets this flag, but setting it here
# guarantees the guard is present even if the importer exits early.
if sync.respond_to?(:sync_stats)
existing = (sync.sync_stats || {})
sync.update_columns(sync_stats: existing.merge("balances_only" => true))
# Pre-mark the sync as balances_only for runtime only (no persistence)
begin
sync.define_singleton_method(:balances_only?) { true }
rescue => e
Rails.logger.warn("SimplefinItem::Syncer: failed to attach balances_only? flag: #{e.class} - #{e.message}")
end
SimplefinItem::Importer.new(simplefin_item, simplefin_provider: simplefin_item.simplefin_provider, sync: sync).import_balances_only
finalize_setup_counts(sync)
@@ -30,7 +29,7 @@ class SimplefinItem::Syncer
end
# Balances-only fast path
if sync.respond_to?(:sync_stats) && (sync.sync_stats || {})["balances_only"]
if sync.respond_to?(:balances_only?) && sync.balances_only?
sync.update!(status_text: "Refreshing balances only...") if sync.respond_to?(:status_text)
begin
# Use the Importer to run balances-only path