The personal finance app for everyone (by everyone)
Ruby
Fixes & Improvements (#316)
soky srmfad241c416a1
diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rbindex 2688d0b1e5..57791639a8 100644--- a/app/controllers/reports_controller.rb+++ b/app/controllers/reports_controller.rb@@ -25,9 +25,6 @@ class ReportsController < ApplicationController# Calculate summary metrics@summary_metrics = build_summary_metrics- # Build comparison data- @comparison_data = build_comparison_data-# Build trend data (last 6 months)@trends_data = build_trends_data@@ -195,25 +192,6 @@ class ReportsController < ApplicationControllernilend- def build_comparison_data- currency_symbol = Money::Currency.new(Current.family.currency).symbol-- # Totals are BigDecimal amounts in dollars - pass directly to Money.new()- {- current: {- income: @current_income_totals.total,- expenses: @current_expense_totals.total,- net: @current_income_totals.total - @current_expense_totals.total- },- previous: {- income: @previous_income_totals.total,- expenses: @previous_expense_totals.total,- net: @previous_income_totals.total - @previous_expense_totals.total- },- currency_symbol: currency_symbol- }- end-def build_trends_data# Generate month-by-month data based on the current period filtertrends = []diff --git a/app/controllers/settings/providers_controller.rb b/app/controllers/settings/providers_controller.rbindex 69a52c87c1..d31f34a3e5 100644--- a/app/controllers/settings/providers_controller.rb+++ b/app/controllers/settings/providers_controller.rb@@ -28,9 +28,6 @@ class Settings::ProvidersController < ApplicationControllerupdated_fields = []- # This hash will store only the updates for dynamic (non-declared) fields- dynamic_updates = {}-# Perform all updates within a transaction for consistencySetting.transaction doprovider_params.each do |param_key, param_value|@@ -57,32 +54,13 @@ class Settings::ProvidersController < ApplicationController# This is safe and uses the proper setter.Setting.public_send("#{key_str}=", value)else- # If it's a dynamic field, add it to our batch hash- # to avoid the Read-Modify-Write conflict.- dynamic_updates[key_str] = value+ # If it's a dynamic field, set it as an individual entry+ # Each field is stored independently, preventing race conditions+ Setting[key_str] = valueendupdated_fields << param_keyend-- # Now, if we have any dynamic updates, apply them all at once- if dynamic_updates.any?- # 1. READ the current hash once- current_dynamic = Setting.dynamic_fields.dup-- # 2. MODIFY by merging changes- # Treat nil values as deletions to keep the hash clean- dynamic_updates.each do |key, value|- if value.nil?- current_dynamic.delete(key)- else- current_dynamic[key] = value- end- end-- # 3. WRITE the complete, merged hash back once- Setting.dynamic_fields = current_dynamic- endendif updated_fields.any?diff --git a/app/models/lunchflow_item/importer.rb b/app/models/lunchflow_item/importer.rbindex c0fbe94d98..56104c157a 100644--- a/app/models/lunchflow_item/importer.rb+++ b/app/models/lunchflow_item/importer.rb@@ -29,8 +29,12 @@ class LunchflowItem::Importeraccounts_failed = 0if accounts_data[:accounts].present?- # Get all existing lunchflow account IDs for this item (normalize to strings for comparison)- existing_account_ids = lunchflow_item.lunchflow_accounts.pluck(:account_id).map(&:to_s)+ # Get only linked lunchflow account IDs (ones actually imported/used by the user)+ # This prevents updating orphaned accounts from old behavior that saved everything+ existing_account_ids = lunchflow_item.lunchflow_accounts+ .joins(:account_provider)+ .pluck(:account_id)+ .map(&:to_s)accounts_data[:accounts].each do |account_data|account_id = account_data[:id]&.to_sdiff --git a/app/models/provider/configurable.rb b/app/models/provider/configurable.rbindex 680d2cc2ef..104ed107ad 100644--- a/app/models/provider/configurable.rb+++ b/app/models/provider/configurable.rb@@ -1,8 +1,8 @@# Module for providers to declare their configuration requirements## Providers can declare their own configuration fields without needing to modify-# the Setting model. Settings are stored dynamically using RailsSettings::Base's-# hash-style access (Setting[:key] = value).+# the Setting model. Settings are stored dynamically as individual entries using+# RailsSettings::Base's bracket-style access (Setting[:key] = value).## Configuration fields are automatically registered and displayed in the UI at# /settings/providers. The system checks Setting storage first, then ENV variables,@@ -186,8 +186,8 @@ module Provider::Configurable# Get the value for this field (Setting -> ENV -> default)def value- # First try Setting using dynamic hash-style access- # This works even without explicit field declarations in Setting model+ # First try Setting using dynamic bracket-style access+ # Each field is stored as an individual entry without explicit field declarationssetting_value = Setting[setting_key]return normalize_value(setting_value) if setting_value.present?diff --git a/app/models/setting.rb b/app/models/setting.rbindex dc1b025db2..4a11397047 100644--- a/app/models/setting.rb+++ b/app/models/setting.rb@@ -11,9 +11,8 @@ class Setting < RailsSettings::Basefield :openai_model, type: :string, default: ENV["OPENAI_MODEL"]field :brand_fetch_client_id, type: :string, default: ENV["BRAND_FETCH_CLIENT_ID"]- # Single hash field for all dynamic provider credentials and other dynamic settings- # This allows unlimited dynamic fields without declaring them upfront- field :dynamic_fields, type: :hash, default: {}+ # Dynamic fields are now stored as individual entries with "dynamic:" prefix+ # This prevents race conditions and ensures each field is independently managed# Onboarding and app settingsONBOARDING_STATES = %w[open closed invite_only].freeze@@ -50,7 +49,7 @@ class Setting < RailsSettings::Baseend# Support dynamic field access via bracket notation- # First checks if it's a declared field, then falls back to dynamic_fields hash+ # First checks if it's a declared field, then falls back to individual dynamic entriesdef [](key)key_str = key.to_s@@ -58,8 +57,8 @@ class Setting < RailsSettings::Baseif respond_to?(key_str)public_send(key_str)else- # Fall back to dynamic_fields hash- dynamic_fields[key_str]+ # Fall back to individual dynamic entry lookup+ find_by(var: dynamic_key_name(key_str))&.valueendend@@ -70,21 +69,26 @@ class Setting < RailsSettings::Baseif respond_to?("#{key_str}=")public_send("#{key_str}=", value)else- # Otherwise, manage in dynamic_fields hash- current_dynamic = dynamic_fields.dup+ # Store as individual dynamic entry+ dynamic_key = dynamic_key_name(key_str)if value.nil?- current_dynamic.delete(key_str) # treat nil as delete+ where(var: dynamic_key).destroy_all+ clear_cacheelse- current_dynamic[key_str] = value+ # Use upsert for atomic insert/update to avoid race conditions+ upsert({ var: dynamic_key, value: value.to_yaml }, unique_by: :var)+ clear_cacheend- self.dynamic_fields = current_dynamic # persists & busts cacheendend# Check if a dynamic field exists (useful to distinguish nil value vs missing key)def key?(key)key_str = key.to_s- respond_to?(key_str) || dynamic_fields.key?(key_str)+ return true if respond_to?(key_str)++ # Check if dynamic entry exists+ where(var: dynamic_key_name(key_str)).exists?end# Delete a dynamic field@@ -92,16 +96,23 @@ class Setting < RailsSettings::Basekey_str = key.to_sreturn nil if respond_to?(key_str) # Can't delete declared fields- current_dynamic = dynamic_fields.dup- value = current_dynamic.delete(key_str)- self.dynamic_fields = current_dynamic+ dynamic_key = dynamic_key_name(key_str)+ value = self[key_str]+ where(var: dynamic_key).destroy_all+ clear_cachevalueend# List all dynamic field keys (excludes declared fields)def dynamic_keys- dynamic_fields.keys+ where("var LIKE ?", "dynamic:%").pluck(:var).map { |var| var.sub(/^dynamic:/, "") }end++ private++ def dynamic_key_name(key_str)+ "dynamic:#{key_str}"+ endend# Validates OpenAI configuration requires model when custom URI base is setdiff --git a/app/views/lunchflow_items/select_accounts.html.erb b/app/views/lunchflow_items/select_accounts.html.erbindex 9642de59ea..48dc3caa5e 100644--- a/app/views/lunchflow_items/select_accounts.html.erb+++ b/app/views/lunchflow_items/select_accounts.html.erb@@ -16,10 +16,10 @@<div class="space-y-2"><% @available_accounts.each do |account| %><% has_blank_name = account[:name].blank? %>- <label class="flex items-start gap-3 p-3 border <%= has_blank_name ? 'border-error bg-error/5' : 'border-primary' %> rounded-lg <%= has_blank_name ? 'cursor-not-allowed opacity-60' : 'hover:bg-subtle cursor-pointer' %> transition-colors">+ <label class="flex items-start gap-3 p-3 border <%= has_blank_name ? "border-error bg-error/5" : "border-primary" %> rounded-lg <%= has_blank_name ? "cursor-not-allowed opacity-60" : "hover:bg-subtle cursor-pointer" %> transition-colors"><%= check_box_tag "account_ids[]", account[:id], false, disabled: has_blank_name, class: "mt-1" %><div class="flex-1">- <div class="font-medium text-sm <%= has_blank_name ? 'text-error' : 'text-primary' %>">+ <div class="font-medium text-sm <%= has_blank_name ? "text-error" : "text-primary" %>"><% if has_blank_name %><%= t(".no_name_placeholder") %><% else %>diff --git a/app/views/lunchflow_items/select_existing_account.html.erb b/app/views/lunchflow_items/select_existing_account.html.erbindex 7469b1c012..3dc7785170 100644--- a/app/views/lunchflow_items/select_existing_account.html.erb+++ b/app/views/lunchflow_items/select_existing_account.html.erb@@ -16,10 +16,10 @@<div class="space-y-2"><% @available_accounts.each do |account| %><% has_blank_name = account[:name].blank? %>- <label class="flex items-start gap-3 p-3 border <%= has_blank_name ? 'border-error bg-error/5' : 'border-primary' %> rounded-lg <%= has_blank_name ? 'cursor-not-allowed opacity-60' : 'hover:bg-subtle cursor-pointer' %> transition-colors">+ <label class="flex items-start gap-3 p-3 border <%= has_blank_name ? "border-error bg-error/5" : "border-primary" %> rounded-lg <%= has_blank_name ? "cursor-not-allowed opacity-60" : "hover:bg-subtle cursor-pointer" %> transition-colors"><%= radio_button_tag "lunchflow_account_id", account[:id], false, disabled: has_blank_name, class: "mt-1" %><div class="flex-1">- <div class="font-medium text-sm <%= has_blank_name ? 'text-error' : 'text-primary' %>">+ <div class="font-medium text-sm <%= has_blank_name ? "text-error" : "text-primary" %>"><% if has_blank_name %><%= t(".no_name_placeholder") %><% else %>diff --git a/app/views/reports/_comparison_chart.html.erb b/app/views/reports/_comparison_chart.html.erbdeleted file mode 100644index c48531eb98..0000000000--- a/app/views/reports/_comparison_chart.html.erb+++ /dev/null@@ -1,250 +0,0 @@-<%- currency = Current.family.currency-- # Helper to calculate percentage change and determine if it's good or bad- def comparison_class(current, previous, inverse: false)- return "text-primary" if previous.zero?-- change = current - previous- is_positive_change = change > 0-- # For expenses, lower is better (inverse logic)- is_good = inverse ? !is_positive_change : is_positive_change-- is_good ? "text-green-600" : "text-gray-600"- end-- def percentage_change(current, previous)- return 0 if previous.zero?- ((current - previous) / previous.abs * 100).round(1)- end-%>--<div>- <div class="flex items-center justify-between mb-6">- <h2 class="text-lg font-medium text-primary">- <%= t("reports.comparison.title") %>- </h2>- <p class="text-sm text-tertiary">- <%= t("reports.comparison.currency", symbol: comparison_data[:currency_symbol]) %>- </p>- </div>-- <div class="space-y-6">- <%# Income Comparison %>- <div>- <div class="flex items-center justify-between mb-3">- <h3 class="text-sm font-medium text-secondary flex items-center gap-2">- <%= icon("trending-up", class: "w-4 h-4 text-success") %>- <%= t("reports.comparison.income") %>- </h3>- </div>-- <div class="flex items-center justify-between mb-2">- <div class="flex items-center gap-3">- <span class="text-2xl font-semibold <%= comparison_class(comparison_data[:current][:income], comparison_data[:previous][:income]) %>">- <%= Money.new(comparison_data[:current][:income], currency).format %>- </span>- <% change = percentage_change(comparison_data[:current][:income], comparison_data[:previous][:income]) %>- <% if change != 0 %>- <% income_improved = comparison_data[:current][:income] > comparison_data[:previous][:income] %>- <div class="flex items-center gap-1.5">- <span class="text-sm font-medium <%= comparison_class(comparison_data[:current][:income], comparison_data[:previous][:income]) %>">- <%= change >= 0 ? "+" : "" %><%= change %>%- </span>- <span class="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium <%= income_improved ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' %>">- <%= icon(income_improved ? "trending-up" : "trending-down", class: "w-3 h-3") %>- <%= t(income_improved ? "reports.comparison.status.improved" : "reports.comparison.status.decreased") %>- </span>- </div>- <% end %>- </div>- <span class="text-sm text-tertiary">- <%= t("reports.comparison.previous") %>: <%= Money.new(comparison_data[:previous][:income], currency).format %>- </span>- </div>-- <%# Overlapping bars %>- <div class="relative h-10">- <%- current_income_abs = comparison_data[:current][:income].to_f.abs- previous_income_abs = comparison_data[:previous][:income].to_f.abs- max_income = [current_income_abs, previous_income_abs].max-- if max_income > 0- current_width = [3, (current_income_abs / max_income * 100)].max- previous_width = [3, (previous_income_abs / max_income * 100)].max- else- current_width = 0- previous_width = 0- end-- # Income: green if increased, gray/primary if decreased- income_increased = comparison_data[:current][:income] >= comparison_data[:previous][:income]- income_bar_color = income_increased ? "bg-green-500" : "bg-gray-600"- income_bg_color = income_increased ? "bg-green-200" : "bg-gray-300"- %>- <% if previous_width > 0 || current_width > 0 %>- <%# Previous period bar (background) %>- <% if previous_width > 0 %>- <div class="absolute top-0 left-0 h-10 <%= income_bg_color %> rounded-lg transition-all duration-500"- style="width: <%= previous_width %>%"></div>- <% end %>- <%# Current period bar (foreground) %>- <% if current_width > 0 %>- <div class="absolute top-2 left-0 h-6 <%= income_bar_color %> rounded-lg transition-all duration-500"- style="width: <%= current_width %>%"></div>- <% end %>- <% else %>- <div class="flex items-center justify-center h-10 text-sm text-tertiary">- <%= t("reports.comparison.no_data") %>- </div>- <% end %>- </div>- </div>-- <%# Expenses Comparison %>- <div>- <div class="flex items-center justify-between mb-3">- <h3 class="text-sm font-medium text-secondary flex items-center gap-2">- <%= icon("trending-down", class: "w-4 h-4 text-danger") %>- <%= t("reports.comparison.expenses") %>- </h3>- </div>-- <div class="flex items-center justify-between mb-2">- <div class="flex items-center gap-3">- <span class="text-2xl font-semibold <%= comparison_class(comparison_data[:current][:expenses], comparison_data[:previous][:expenses], inverse: true) %>">- <%= Money.new(comparison_data[:current][:expenses], currency).format %>- </span>- <% change = percentage_change(comparison_data[:current][:expenses], comparison_data[:previous][:expenses]) %>- <% if change != 0 %>- <% expenses_improved = comparison_data[:current][:expenses] < comparison_data[:previous][:expenses] %>- <div class="flex items-center gap-1.5">- <span class="text-sm font-medium <%= comparison_class(comparison_data[:current][:expenses], comparison_data[:previous][:expenses], inverse: true) %>">- <%= change >= 0 ? "+" : "" %><%= change %>%- </span>- <span class="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium <%= expenses_improved ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' %>">- <%= icon(expenses_improved ? "trending-down" : "trending-up", class: "w-3 h-3") %>- <%= t(expenses_improved ? "reports.comparison.status.reduced" : "reports.comparison.status.increased") %>- </span>- </div>- <% end %>- </div>- <span class="text-sm text-tertiary">- <%= t("reports.comparison.previous") %>: <%= Money.new(comparison_data[:previous][:expenses], currency).format %>- </span>- </div>-- <%# Overlapping bars %>- <div class="relative h-10">- <%- current_expenses_abs = comparison_data[:current][:expenses].to_f.abs- previous_expenses_abs = comparison_data[:previous][:expenses].to_f.abs- max_expenses = [current_expenses_abs, previous_expenses_abs].max-- if max_expenses > 0- current_width = [3, (current_expenses_abs / max_expenses * 100)].max- previous_width = [3, (previous_expenses_abs / max_expenses * 100)].max- else- current_width = 0- previous_width = 0- end-- # Expenses: green if decreased (inverse logic), gray/primary if increased- expenses_decreased = comparison_data[:current][:expenses] <= comparison_data[:previous][:expenses]- expenses_bar_color = expenses_decreased ? "bg-green-500" : "bg-gray-600"- expenses_bg_color = expenses_decreased ? "bg-green-200" : "bg-gray-300"- %>- <% if previous_width > 0 || current_width > 0 %>- <%# Previous period bar (background) %>- <% if previous_width > 0 %>- <div class="absolute top-0 left-0 h-10 <%= expenses_bg_color %> rounded-lg transition-all duration-500"- style="width: <%= previous_width %>%"></div>- <% end %>- <%# Current period bar (foreground) %>- <% if current_width > 0 %>- <div class="absolute top-2 left-0 h-6 <%= expenses_bar_color %> rounded-lg transition-all duration-500"- style="width: <%= current_width %>%"></div>- <% end %>- <% else %>- <div class="flex items-center justify-center h-10 text-sm text-tertiary">- <%= t("reports.comparison.no_data") %>- </div>- <% end %>- </div>- </div>-- <%# Net Savings Comparison %>- <div>- <div class="flex items-center justify-between mb-3">- <h3 class="text-sm font-medium text-secondary flex items-center gap-2">- <%= icon("piggy-bank", class: "w-4 h-4 text-primary") %>- <%= t("reports.comparison.net_savings") %>- </h3>- </div>-- <div class="flex items-center justify-between mb-2">- <div class="flex items-center gap-3">- <span class="text-2xl font-semibold <%= comparison_class(comparison_data[:current][:net], comparison_data[:previous][:net]) %>">- <%= Money.new(comparison_data[:current][:net], currency).format %>- </span>- <% change = percentage_change(comparison_data[:current][:net], comparison_data[:previous][:net]) %>- <% if change != 0 %>- <% net_improved = comparison_data[:current][:net] > comparison_data[:previous][:net] %>- <div class="flex items-center gap-1.5">- <span class="text-sm font-medium <%= comparison_class(comparison_data[:current][:net], comparison_data[:previous][:net]) %>">- <%= change >= 0 ? "+" : "" %><%= change %>%- </span>- <span class="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium <%= net_improved ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' %>">- <%= icon(net_improved ? "trending-up" : "trending-down", class: "w-3 h-3") %>- <%= t(net_improved ? "reports.comparison.status.improved" : "reports.comparison.status.decreased") %>- </span>- </div>- <% end %>- </div>- <span class="text-sm text-tertiary">- <%= t("reports.comparison.previous") %>: <%= Money.new(comparison_data[:previous][:net], currency).format %>- </span>- </div>-- <%# Overlapping bars %>- <div class="relative h-10">- <%- current_net_abs = comparison_data[:current][:net].to_f.abs- previous_net_abs = comparison_data[:previous][:net].to_f.abs- max_net = [current_net_abs, previous_net_abs].max-- if max_net > 0- current_width = [3, (current_net_abs / max_net * 100)].max- previous_width = [3, (previous_net_abs / max_net * 100)].max- else- current_width = 0- previous_width = 0- end-- # Net Savings: green if improved (increased), gray/primary if got worse- net_improved = comparison_data[:current][:net] >= comparison_data[:previous][:net]- net_bar_color = net_improved ? "bg-green-500" : "bg-gray-600"- net_bg_color = net_improved ? "bg-green-200" : "bg-gray-300"- %>- <% if previous_width > 0 || current_width > 0 %>- <%# Previous period bar (background) %>- <% if previous_width > 0 %>- <div class="absolute top-0 left-0 h-10 <%= net_bg_color %> rounded-lg transition-all duration-500"- style="width: <%= previous_width %>%"></div>- <% end %>- <%# Current period bar (foreground) %>- <% if current_width > 0 %>- <div class="absolute top-2 left-0 h-6 <%= net_bar_color %> rounded-lg transition-all duration-500"- style="width: <%= current_width %>%"></div>- <% end %>- <% else %>- <div class="flex items-center justify-center h-10 text-sm text-tertiary">- <%= t("reports.comparison.no_data") %>- </div>- <% end %>- </div>- </div>- </div>-</div>diff --git a/app/views/reports/index.html.erb b/app/views/reports/index.html.erbindex dd659bf874..6a75e28d42 100644--- a/app/views/reports/index.html.erb+++ b/app/views/reports/index.html.erb@@ -87,15 +87,6 @@} %></section>- <%# Comparison Chart %>- <section class="bg-container py-4 px-6 rounded-xl shadow-border-xs">- <%= render partial: "reports/comparison_chart", locals: {- comparison_data: @comparison_data,- period_type: @period_type,- start_date: @start_date- } %>- </section>-<%# Trends & Insights %><section class="bg-container py-4 px-6 rounded-xl shadow-border-xs"><%= render partial: "reports/trends_insights", locals: {diff --git a/app/views/settings/providers/_provider_form.html.erb b/app/views/settings/providers/_provider_form.html.erbindex 8f83a8f2e2..34054ead56 100644--- a/app/views/settings/providers/_provider_form.html.erb+++ b/app/views/settings/providers/_provider_form.html.erb@@ -73,7 +73,7 @@</div><% end %>- <% # Show configuration status %>+ <%# Show configuration status %><% if configuration.configured? %><div class="flex items-center gap-2 mt-4"><div class="w-2 h-2 bg-green-500 rounded-full"></div>diff --git a/config/initializers/plaid.rb b/config/initializers/plaid.rbindex f5c47fd2bd..a8b54e6bec 100644--- a/config/initializers/plaid.rb+++ b/config/initializers/plaid.rb@@ -6,10 +6,16 @@ end# Load Plaid configuration from adapters after initializationRails.application.config.after_initialize do+ # Skip if database is not ready (e.g., during db:create)+ next unless ActiveRecord::Base.connection.table_exists?("settings")+# Ensure provider adapters are loadedProvider::Factory.ensure_adapters_loaded# Reload configurations from settings/ENVProvider::PlaidAdapter.reload_configuration # US regionProvider::PlaidEuAdapter.reload_configuration # EU region+rescue ActiveRecord::NoDatabaseError, PG::ConnectionBad+ # Database doesn't exist yet, skip initialization+ nilenddiff --git a/config/locales/views/reports/en.yml b/config/locales/views/reports/en.ymlindex 3b4e952e22..87dd04b9a2 100644--- a/config/locales/views/reports/en.yml+++ b/config/locales/views/reports/en.yml@@ -24,20 +24,6 @@ en:income_minus_expenses: Income minus expensesof_budget_used: of budget usedno_budget_data: No budget data for this period- comparison:- title: Period Comparison- currency: "Currency: %{symbol}"- income: Income- expenses: Expenses- net_savings: Net Savings- current: Current Period- previous: Previous Period- no_data: No data available- status:- improved: Improved- decreased: Decreased- reduced: Reduced- increased: Increasedbudget_performance:title: Budget Performancespent: Spentdiff --git a/db/migrate/20251111084519_remove_orphaned_lunchflow_accounts.rb b/db/migrate/20251111084519_remove_orphaned_lunchflow_accounts.rbnew file mode 100644index 0000000000..80a2108548--- /dev/null+++ b/db/migrate/20251111084519_remove_orphaned_lunchflow_accounts.rb@@ -0,0 +1,28 @@+class RemoveOrphanedLunchflowAccounts < ActiveRecord::Migration[7.2]+ def up+ # Find all LunchflowAccount records that don't have an associated account_provider+ # These are "orphaned" accounts that were created during sync but never actually+ # imported/linked by the user due to old behavior that saved all accounts+ orphaned_accounts = LunchflowAccount.left_outer_joins(:account_provider)+ .where(account_providers: { id: nil })++ orphaned_count = orphaned_accounts.count++ if orphaned_count > 0+ Rails.logger.info "Removing #{orphaned_count} orphaned LunchflowAccount records (not linked via account_provider)"++ # Delete orphaned accounts+ orphaned_accounts.destroy_all++ Rails.logger.info "Successfully removed #{orphaned_count} orphaned LunchflowAccount records"+ else+ Rails.logger.info "No orphaned LunchflowAccount records found to remove"+ end+ end++ def down+ # Cannot restore orphaned accounts that were deleted+ # These were unused accounts from old behavior that shouldn't have been created+ Rails.logger.info "Cannot restore orphaned LunchflowAccount records (data migration is irreversible)"+ end+enddiff --git a/db/migrate/20251111094448_migrate_dynamic_fields_to_individual_entries.rb b/db/migrate/20251111094448_migrate_dynamic_fields_to_individual_entries.rbnew file mode 100644index 0000000000..2e49318592--- /dev/null+++ b/db/migrate/20251111094448_migrate_dynamic_fields_to_individual_entries.rb@@ -0,0 +1,35 @@+class MigrateDynamicFieldsToIndividualEntries < ActiveRecord::Migration[7.2]+ def up+ # Find the dynamic_fields setting record+ dynamic_fields_record = Setting.find_by(var: "dynamic_fields")+ return unless dynamic_fields_record++ # Parse the hash and create individual entries+ dynamic_fields_hash = dynamic_fields_record.value || {}+ dynamic_fields_hash.each do |key, value|+ Setting.create!(+ var: "dynamic:#{key}",+ value: value+ )+ end++ # Delete the old dynamic_fields record+ dynamic_fields_record.destroy+ end++ def down+ # Collect all dynamic: entries back into a hash+ dynamic_fields_hash = {}+ Setting.where("var LIKE ?", "dynamic:%").find_each do |record|+ key = record.var.sub(/^dynamic:/, "")+ dynamic_fields_hash[key] = record.value+ record.destroy+ end++ # Recreate the dynamic_fields record with the hash+ Setting.create!(+ var: "dynamic_fields",+ value: dynamic_fields_hash+ ) if dynamic_fields_hash.any?+ end+enddiff --git a/db/schema.rb b/db/schema.rbindex 86a1ec71d4..4743b4f024 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: 2025_11_10_104411) do+ActiveRecord::Schema[7.2].define(version: 2025_11_11_094448) do# These are extensions that must be enabled in order to support this databaseenable_extension "pgcrypto"enable_extension "plpgsql"@@ -687,6 +687,10 @@ ActiveRecord::Schema[7.2].define(version: 2025_11_10_104411) dot.datetime "created_at", null: falset.datetime "updated_at", null: falset.string "name"+ t.boolean "manual", default: false, null: false+ t.decimal "expected_amount_min", precision: 19, scale: 4+ t.decimal "expected_amount_max", precision: 19, scale: 4+ t.decimal "expected_amount_avg", precision: 19, scale: 4t.index ["family_id", "merchant_id", "amount", "currency"], name: "idx_recurring_txns_merchant", unique: true, where: "(merchant_id IS NOT NULL)"t.index ["family_id", "name", "amount", "currency"], name: "idx_recurring_txns_name", unique: true, where: "((name IS NOT NULL) AND (merchant_id IS NULL))"t.index ["family_id", "status"], name: "index_recurring_transactions_on_family_id_and_status"diff --git a/test/controllers/reports_controller_test.rb b/test/controllers/reports_controller_test.rbindex c2e7a8cb22..4c454bf6aa 100644--- a/test/controllers/reports_controller_test.rb+++ b/test/controllers/reports_controller_test.rb@@ -76,14 +76,6 @@ class ReportsControllerTest < ActionDispatch::IntegrationTestassert_select "h3", text: I18n.t("reports.summary.net_savings")end- test "index builds comparison data" do- get reports_path(period_type: :monthly)- assert_response :ok- assert_select "h2", text: I18n.t("reports.comparison.title")- assert_select "h3", text: I18n.t("reports.comparison.income")- assert_select "h3", text: I18n.t("reports.comparison.expenses")- end-test "index builds trends data" doget reports_path(period_type: :monthly)assert_response :okdiff --git a/test/controllers/settings/providers_controller_test.rb b/test/controllers/settings/providers_controller_test.rbindex 7f97d6de01..9443a5032f 100644--- a/test/controllers/settings/providers_controller_test.rb+++ b/test/controllers/settings/providers_controller_test.rb@@ -41,10 +41,10 @@ class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTestendtest "updates dynamic provider fields using batch update" do- # plaid_client_id is a dynamic field, so it should go through dynamic_fields hash+ # plaid_client_id is a dynamic field, stored as an individual entrywith_self_hosting do# Clear any existing plaid settings- Setting.dynamic_fields = {}+ Setting["plaid_client_id"] = nilpatch settings_providers_url, params: {setting: { plaid_client_id: "test_client_id" }@@ -52,14 +52,16 @@ class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTestassert_redirected_to settings_providers_urlassert_equal "test_client_id", Setting["plaid_client_id"]- assert_equal "test_client_id", Setting.dynamic_fields["plaid_client_id"]endendtest "batches multiple dynamic fields from same provider atomically" do- # Test that multiple fields from Plaid are updated together in one write operation+ # Test that multiple fields from Plaid are updated as individual entrieswith_self_hosting do- Setting.dynamic_fields = {}+ # Clear existing fields+ Setting["plaid_client_id"] = nil+ Setting["plaid_secret"] = nil+ Setting["plaid_environment"] = nilpatch settings_providers_url, params: {setting: {@@ -71,7 +73,7 @@ class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTestassert_redirected_to settings_providers_url- # All three should be present in dynamic_fields+ # All three should be present as individual entriesassert_equal "new_client_id", Setting["plaid_client_id"]assert_equal "new_secret", Setting["plaid_secret"]assert_equal "production", Setting["plaid_environment"]@@ -79,9 +81,14 @@ class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTestendtest "batches dynamic fields from multiple providers atomically" do- # Test that fields from different providers are all batched together+ # Test that fields from different providers are stored as individual entrieswith_self_hosting do- Setting.dynamic_fields = {}+ # Clear existing fields+ Setting["plaid_client_id"] = nil+ Setting["plaid_secret"] = nil+ Setting["plaid_eu_client_id"] = nil+ Setting["plaid_eu_secret"] = nil+ Setting["simplefin_setup_token"] = nilpatch settings_providers_url, params: {setting: {@@ -108,10 +115,8 @@ class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTest# Test that updating some fields doesn't overwrite other existing fieldswith_self_hosting do# Set initial fields- Setting.dynamic_fields = {- "existing_field_1" => "value1",- "plaid_client_id" => "old_client_id"- }+ Setting["existing_field_1"] = "value1"+ Setting["plaid_client_id"] = "old_client_id"# Update one field and add a new onepatch settings_providers_url, params: {@@ -162,7 +167,7 @@ class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTest# Set initial valuesSetting["plaid_client_id"] = "old_value"assert_equal "old_value", Setting["plaid_client_id"]- assert Setting.dynamic_fields.key?("plaid_client_id")+ assert Setting.key?("plaid_client_id")patch settings_providers_url, params: {setting: { plaid_client_id: " " } # Blank string with spaces@@ -170,18 +175,18 @@ class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTestassert_redirected_to settings_providers_urlassert_nil Setting["plaid_client_id"]- # Key should be removed from hash, not just set to nil- refute Setting.dynamic_fields.key?("plaid_client_id"),- "nil values should delete the key from dynamic_fields"+ # Entry should be removed, not just set to nil+ refute Setting.key?("plaid_client_id"),+ "nil values should delete the entry"endendtest "handles sequential updates to different dynamic fields safely" do# This test simulates what would happen if two requests tried to update- # different dynamic fields sequentially. With the batch update approach,- # all changes should be preserved.+ # different dynamic fields sequentially. With individual entries,+ # all changes should be preserved without conflicts.with_self_hosting do- Setting.dynamic_fields = { "existing_field" => "existing_value" }+ Setting["existing_field"] = "existing_value"# Simulate first request updating plaid fieldspatch settings_providers_url, params: {@@ -266,8 +271,8 @@ class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTesttest "logs errors when update fails" dowith_self_hosting do# Test that errors during update are properly logged and handled gracefully- # We'll force an error by making the dynamic_fields= setter raise- Setting.expects(:dynamic_fields=).raises(StandardError.new("Database error")).once+ # We'll force an error by making the []= method raise+ Setting.expects(:[]=).with("plaid_client_id", "test").raises(StandardError.new("Database error")).once# Mock logger to verify error is loggedRails.logger.expects(:error).with(regexp_matches(/Failed to update provider settings.*Database error/)).oncediff --git a/test/models/period_test.rb b/test/models/period_test.rbindex b7e3e4f5d0..fa1221eb94 100644--- a/test/models/period_test.rb+++ b/test/models/period_test.rb@@ -45,25 +45,11 @@ class PeriodTest < ActiveSupport::TestCaseassert_equal "Custom Period", period.labelend- test "comparison_label returns correct label for known period" do- period = Period.from_key("last_30_days")- assert_equal "vs. last month", period.comparison_label- end-- test "comparison_label returns date range for unknown period" do- start_date = Date.current - 15.days- end_date = Date.current- period = Period.new(start_date: start_date, end_date: end_date)- expected = "#{start_date.strftime("%b %d, %Y")} to #{end_date.strftime("%b %d, %Y")}"- assert_equal expected, period.comparison_label- end-test "all_time period can be created" doperiod = Period.from_key("all_time")assert_equal "all_time", period.keyassert_equal "All Time", period.labelassert_equal "All", period.label_short- assert_equal "vs. beginning", period.comparison_labelendtest "all_time period uses family's oldest entry date" dodiff --git a/test/models/setting_test.rb b/test/models/setting_test.rbindex bb0ffe8df9..cdadb0458b 100644--- a/test/models/setting_test.rb+++ b/test/models/setting_test.rb@@ -7,6 +7,11 @@ class SettingTest < ActiveSupport::TestCaseSetting.openai_model = nilend+ teardown do+ # Clean up dynamic fields after each test+ Setting.where("var LIKE ?", "dynamic:%").destroy_all+ end+test "validate_openai_config! passes when both uri base and model are set" doassert_nothing_raised doSetting.validate_openai_config!(uri_base: "https://api.example.com", model: "gpt-4")@@ -61,4 +66,69 @@ class SettingTest < ActiveSupport::TestCaseSetting.validate_openai_config!(uri_base: "", model: nil)endend++ # Dynamic field tests+ test "can set and get dynamic fields" do+ Setting["custom_key"] = "custom_value"+ assert_equal "custom_value", Setting["custom_key"]+ end++ test "can set and get multiple dynamic fields independently" do+ Setting["key1"] = "value1"+ Setting["key2"] = "value2"+ Setting["key3"] = "value3"++ assert_equal "value1", Setting["key1"]+ assert_equal "value2", Setting["key2"]+ assert_equal "value3", Setting["key3"]+ end++ test "setting nil value deletes dynamic field" do+ Setting["temp_key"] = "temp_value"+ assert_equal "temp_value", Setting["temp_key"]++ Setting["temp_key"] = nil+ assert_nil Setting["temp_key"]+ end++ test "can delete dynamic field" do+ Setting["delete_key"] = "delete_value"+ assert_equal "delete_value", Setting["delete_key"]++ value = Setting.delete("delete_key")+ assert_equal "delete_value", value+ assert_nil Setting["delete_key"]+ end++ test "key? returns true for existing dynamic field" do+ Setting["exists_key"] = "exists_value"+ assert Setting.key?("exists_key")+ end++ test "key? returns false for non-existing dynamic field" do+ assert_not Setting.key?("nonexistent_key")+ end++ test "dynamic_keys returns all dynamic field keys" do+ Setting["dynamic1"] = "value1"+ Setting["dynamic2"] = "value2"++ keys = Setting.dynamic_keys+ assert_includes keys, "dynamic1"+ assert_includes keys, "dynamic2"+ end++ test "declared fields take precedence over dynamic fields" do+ # Try to set a declared field using bracket notation+ Setting["openai_model"] = "custom-model"+ assert_equal "custom-model", Setting["openai_model"]+ assert_equal "custom-model", Setting.openai_model+ end++ test "cannot delete declared fields" do+ Setting.openai_model = "test-model"+ result = Setting.delete("openai_model")+ assert_nil result+ assert_equal "test-model", Setting.openai_model+ endend