From 452ccc9523f27adab2d2fb5b83b8986655c29a62 Mon Sep 17 00:00:00 2001 From: louispt1 Date: Mon, 28 Oct 2024 09:04:04 +0100 Subject: [PATCH] WIP default values for empty scenarios not working --- app/controllers/api/v3/inputs_controller.rb | 20 +++++++++++--------- config/routes.rb | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/controllers/api/v3/inputs_controller.rb b/app/controllers/api/v3/inputs_controller.rb index a30d5e177..6786df728 100644 --- a/app/controllers/api/v3/inputs_controller.rb +++ b/app/controllers/api/v3/inputs_controller.rb @@ -100,7 +100,6 @@ def generate_csv(inputs) csv << csv_headers cached_values = Input.cache(@scenario.parent) user_values = @scenario.user_values - inputs.each do |key, input| add_csv_row(csv, key, input, cached_values, user_values) end @@ -108,24 +107,27 @@ def generate_csv(inputs) end def csv_headers - ["Key", "Min", "Max", "Default", "User Value", "Unit", "Share Group"] + ["Key", "Unit", "Start Year Value", "Scenario Value", "Min", "Max", "Share Group"] end def add_csv_row(csv, key, input, cached_values, user_values) input_data = input.instance_variable_get(:@input) return if input_data.nil? - values = cached_values.read(@scenario.parent, input_data) - default_value = input.instance_variable_get(:@default_values_from).call(values) + puts(input.data.inspect) + + # Provide a safe fallback for missing values + values = cached_values&.read(@scenario.parent, input_data) || {} + default_value = input.instance_variable_get(:@default_values_from)&.call(values) || "" csv << [ key, - input_data.min_value, - input_data.max_value, - default_value, + input_data.unit || "", + default_value || "", user_values[input_data.key] || "", - input_data.unit, - input_data.share_group + input_data.min_value || "", + input_data.max_value || "", + input_data.share_group || "" ] end end diff --git a/config/routes.rb b/config/routes.rb index 7ef9004a4..a6c6e6ce7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -85,7 +85,7 @@ get 'converters', to: redirect('/api/v3/scenarios/%{scenario_id}/nodes') get 'converters/:id', to: redirect('/api/v3/scenarios/%{scenario_id}/nodes/%{id}') - resources :inputs, :only => [:index, :show], defaults: { format: :json} + resources :inputs, only: [:index, :show], defaults: { format: :json }, constraints: { format: /(json|csv)/ } resource :version, :only => [:create, :show, :update], controller: 'scenario_version_tags'