The personal finance app for everyone (by everyone)
Ruby
Add support for provider selection (#346)
soky srm0d798c522abf
diff --git a/app/controllers/settings/hostings_controller.rb b/app/controllers/settings/hostings_controller.rbindex 07aaa5b10e..2fb0c9df7f 100644--- a/app/controllers/settings/hostings_controller.rb+++ b/app/controllers/settings/hostings_controller.rb@@ -10,10 +10,26 @@ class Settings::HostingsController < ApplicationController[ "Home", root_path ],[ "Self-Hosting", nil ]]- twelve_data_provider = Provider::Registry.get_provider(:twelve_data)- @twelve_data_usage = twelve_data_provider&.usage- @yahoo_finance_provider = Provider::Registry.get_provider(:yahoo_finance)+ # Determine which providers are currently selected+ exchange_rate_provider = ENV["EXCHANGE_RATE_PROVIDER"].presence || Setting.exchange_rate_provider+ securities_provider = ENV["SECURITIES_PROVIDER"].presence || Setting.securities_provider++ # Show Twelve Data settings if either provider is set to twelve_data+ @show_twelve_data_settings = exchange_rate_provider == "twelve_data" || securities_provider == "twelve_data"++ # Show Yahoo Finance settings if either provider is set to yahoo_finance+ @show_yahoo_finance_settings = exchange_rate_provider == "yahoo_finance" || securities_provider == "yahoo_finance"++ # Only fetch provider data if we're showing the section+ if @show_twelve_data_settings+ twelve_data_provider = Provider::Registry.get_provider(:twelve_data)+ @twelve_data_usage = twelve_data_provider&.usage+ end++ if @show_yahoo_finance_settings+ @yahoo_finance_provider = Provider::Registry.get_provider(:yahoo_finance)+ endenddef update@@ -34,6 +50,14 @@ class Settings::HostingsController < ApplicationControllerSetting.twelve_data_api_key = hosting_params[:twelve_data_api_key]end+ if hosting_params.key?(:exchange_rate_provider)+ Setting.exchange_rate_provider = hosting_params[:exchange_rate_provider]+ end++ if hosting_params.key?(:securities_provider)+ Setting.securities_provider = hosting_params[:securities_provider]+ end+if hosting_params.key?(:openai_access_token)token_param = hosting_params[:openai_access_token].to_s.strip# Ignore blanks and redaction placeholders to prevent accidental overwrite@@ -71,7 +95,7 @@ class Settings::HostingsController < ApplicationControllerprivatedef hosting_params- params.require(:setting).permit(:onboarding_state, :require_email_confirmation, :brand_fetch_client_id, :twelve_data_api_key, :openai_access_token, :openai_uri_base, :openai_model)+ params.require(:setting).permit(:onboarding_state, :require_email_confirmation, :brand_fetch_client_id, :twelve_data_api_key, :openai_access_token, :openai_uri_base, :openai_model, :exchange_rate_provider, :securities_provider)enddef ensure_admindiff --git a/app/models/exchange_rate/provided.rb b/app/models/exchange_rate/provided.rbindex 4235bba839..6eadf92b8e 100644--- a/app/models/exchange_rate/provided.rb+++ b/app/models/exchange_rate/provided.rb@@ -3,7 +3,7 @@ module ExchangeRate::Providedclass_methods dodef provider- provider = ENV["EXCHANGE_RATE_PROVIDER"] || "twelve_data"+ provider = ENV["EXCHANGE_RATE_PROVIDER"].presence || Setting.exchange_rate_providerregistry = Provider::Registry.for_concept(:exchange_rates)registry.get_provider(provider.to_sym)enddiff --git a/app/models/security/provided.rb b/app/models/security/provided.rbindex 4ff84425e2..58b7f50e62 100644--- a/app/models/security/provided.rb+++ b/app/models/security/provided.rb@@ -5,7 +5,7 @@ module Security::Providedclass_methods dodef provider- provider = ENV["SECURITIES_PROVIDER"] || "twelve_data"+ provider = ENV["SECURITIES_PROVIDER"].presence || Setting.securities_providerregistry = Provider::Registry.for_concept(:securities)registry.get_provider(provider.to_sym)enddiff --git a/app/models/setting.rb b/app/models/setting.rbindex 4a11397047..94a2bdfc19 100644--- a/app/models/setting.rb+++ b/app/models/setting.rb@@ -11,6 +11,10 @@ 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"]+ # Provider selection+ field :exchange_rate_provider, type: :string, default: ENV.fetch("EXCHANGE_RATE_PROVIDER", "twelve_data")+ field :securities_provider, type: :string, default: ENV.fetch("SECURITIES_PROVIDER", "twelve_data")+# Dynamic fields are now stored as individual entries with "dynamic:" prefix# This prevents race conditions and ensures each field is independently manageddiff --git a/app/views/settings/hostings/_provider_selection.html.erb b/app/views/settings/hostings/_provider_selection.html.erbnew file mode 100644index 0000000000..55dde1e88a--- /dev/null+++ b/app/views/settings/hostings/_provider_selection.html.erb@@ -0,0 +1,51 @@+<div class="space-y-4">+ <div>+ <h2 class="font-medium mb-1"><%= t(".title") %></h2>+ <p class="text-secondary text-sm mb-4"><%= t(".description") %></p>+ </div>++ <%= styled_form_with model: Setting.new,+ url: settings_hosting_path,+ method: :patch,+ data: {+ controller: "auto-submit-form",+ "auto-submit-form-trigger-event-value": "change"+ } do |form| %>+ <div class="space-y-4">+ <%= form.select :exchange_rate_provider,+ [+ [t(".providers.twelve_data"), "twelve_data"],+ [t(".providers.yahoo_finance"), "yahoo_finance"]+ ],+ { label: t(".exchange_rate_provider_label") },+ {+ value: ENV.fetch("EXCHANGE_RATE_PROVIDER", Setting.exchange_rate_provider),+ disabled: ENV["EXCHANGE_RATE_PROVIDER"].present?,+ data: { "auto-submit-form-target": "auto" }+ } %>++ <%= form.select :securities_provider,+ [+ [t(".providers.twelve_data"), "twelve_data"],+ [t(".providers.yahoo_finance"), "yahoo_finance"]+ ],+ { label: t(".securities_provider_label") },+ {+ value: ENV.fetch("SECURITIES_PROVIDER", Setting.securities_provider),+ disabled: ENV["SECURITIES_PROVIDER"].present?,+ data: { "auto-submit-form-target": "auto" }+ } %>+ </div>++ <% if ENV["EXCHANGE_RATE_PROVIDER"].present? || ENV["SECURITIES_PROVIDER"].present? %>+ <div class="mt-4 bg-warning-50 border border-warning-200 rounded-lg p-3">+ <div class="flex items-start gap-2">+ <%= icon("alert-circle", class: "w-5 h-5 text-warning-600 mt-0.5 shrink-0") %>+ <p class="text-sm text-warning-800">+ <%= t(".env_configured_message") %>+ </p>+ </div>+ </div>+ <% end %>+ <% end %>+</div>diff --git a/app/views/settings/hostings/_yahoo_finance_settings.html.erb b/app/views/settings/hostings/_yahoo_finance_settings.html.erbindex 4bd4969456..33676f5477 100644--- a/app/views/settings/hostings/_yahoo_finance_settings.html.erb+++ b/app/views/settings/hostings/_yahoo_finance_settings.html.erb@@ -6,7 +6,7 @@<% if @yahoo_finance_provider&.healthy? %><div class="space-y-4"><div class="flex items-center space-x-3">- <div class="w-3 h-3 bg-green-500 rounded-full"></div>+ <div class="w-3 h-3 bg-success rounded-full"></div><p class="text-sm text-secondary"><%= t(".status_active") %></p>@@ -15,18 +15,19 @@<% else %><div class="space-y-4"><div class="flex items-center space-x-3">- <div class="w-3 h-3 bg-red-500 rounded-full"></div>+ <div class="w-3 h-3 bg-destructive rounded-full"></div><p class="text-sm text-secondary"><%= t(".status_inactive") %></p></div>- <div class="bg-red-50 border border-red-200 rounded-lg p-3">- <div class="flex">- <div class="ml-3">- <h3 class="text-sm font-medium text-red-800">+ <div class="bg-destructive-50 border border-destructive-200 rounded-lg p-3">+ <div class="flex items-start gap-2">+ <%= icon("alert-circle", class: "w-5 h-5 text-destructive-600 mt-0.5 shrink-0") %>+ <div>+ <h3 class="text-sm font-medium text-destructive-800"><%= t(".connection_failed") %></h3>- <div class="mt-2 text-sm text-red-700">+ <div class="mt-2 text-sm text-destructive-700"><p><%= t(".troubleshooting") %></p></div></div>diff --git a/app/views/settings/hostings/show.html.erb b/app/views/settings/hostings/show.html.erbindex b406d67d37..0d67f44369 100644--- a/app/views/settings/hostings/show.html.erb+++ b/app/views/settings/hostings/show.html.erb@@ -7,8 +7,13 @@<% end %><%= settings_section title: t(".financial_data_providers") do %><div class="space-y-6">- <%= render "settings/hostings/yahoo_finance_settings" %>- <%= render "settings/hostings/twelve_data_settings" %>+ <%= render "settings/hostings/provider_selection" %>+ <% if @show_yahoo_finance_settings %>+ <%= render "settings/hostings/yahoo_finance_settings" %>+ <% end %>+ <% if @show_twelve_data_settings %>+ <%= render "settings/hostings/twelve_data_settings" %>+ <% end %></div><% end %><%= settings_section title: t(".invites") do %>diff --git a/config/locales/views/settings/hostings/en.yml b/config/locales/views/settings/hostings/en.ymlindex 1751c79cf2..1e7c83059c 100644--- a/config/locales/views/settings/hostings/en.yml+++ b/config/locales/views/settings/hostings/en.yml@@ -25,6 +25,15 @@ en:confirm_clear_cache:title: Clear data cache?body: Are you sure you want to clear the data cache? This will remove all exchange rates, security prices, account balances, and other data. This action cannot be undone.+ provider_selection:+ title: Provider Selection+ description: Choose which service to use for fetching exchange rates and security prices. Yahoo Finance is free and doesn't require an API key. Twelve Data requires a free API key but may offer more data coverage.+ exchange_rate_provider_label: Exchange Rate Provider+ securities_provider_label: Securities (Stock Prices) Provider+ env_configured_message: Provider selection is disabled because environment variables (EXCHANGE_RATE_PROVIDER or SECURITIES_PROVIDER) are set. To enable selection here, remove these environment variables from your configuration.+ providers:+ twelve_data: Twelve Data+ yahoo_finance: Yahoo Financebrand_fetch_settings:description: Enter the Client ID provided by Brand Fetchlabel: Client ID