Skip to content

Commit

Permalink
WIP default values for empty scenarios not working
Browse files Browse the repository at this point in the history
  • Loading branch information
louispt1 committed Nov 19, 2024
1 parent db5029f commit 452ccc9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions app/controllers/api/v3/inputs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,34 @@ 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
end
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
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit 452ccc9

Please sign in to comment.