Skip to content

Commit

Permalink
upgraded the export, for log and csv, added edit
Browse files Browse the repository at this point in the history
  • Loading branch information
ColasPinson committed Aug 18, 2022
1 parent 5ed9f3f commit 02f25f5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 19 deletions.
29 changes: 29 additions & 0 deletions app/controllers/optimization_exports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "csv"

class OptimizationExportsController < ApplicationController
def new
optim_id = params[:optimization_id]
format = params[:format]

optim = Optimization.find(optim_id)
authorize optim

if format == "log"
path = "#{Rails.root}/log/optimizations/optim_#{optim_id}.log"
if File.exist?(path)
send_file(path)
else
redirect_to optimization_path(optim), notice: "There isn't a log file"
end
elsif format == "csv"
attributes = %w{kind config inputs outputs}
content = CSV.generate(headers: true) do |csv|
csv << attributes
csv << attributes.map{ |attr| optim.send(attr) }
end
send_data content, filename: "optim_#{optim_id}.csv"
end
end
end
18 changes: 8 additions & 10 deletions app/controllers/optimizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ def select
def show
end

def download
authorize Optimization.find(params[:optimization_id])
path = "#{Rails.root}/log/optimizations/optim_#{params[:optimization_id]}.log"
if File.exist?(path)
send_file(path)
else
redirect_to optimizations_url, notice: "There isn't a log file"
end
end

def new
@optimization = Optimization.new
authorize @optimization
Expand Down Expand Up @@ -89,6 +79,14 @@ def create
end
end

def edit
set_optimization
if params[:cancel_button]
skip_authorization
redirect_to optimizations_url, notice: "Optimization update cancelled."
end
end

def compare
@compare_optimizations_list = []
if params[:optim_list].length > 1
Expand Down
1 change: 1 addition & 0 deletions app/views/optimizations/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2>Add inputs to optimization #<%= @optimization.id %></h2>
4 changes: 3 additions & 1 deletion app/views/optimizations/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<h2>Optimization Details, entry #<%= @optimization.id %>
</h2>

<%= link_to "Download Log", optimization_download_url(@optimization.id), class: "btn btn-primary ml-2" %>
<%= link_to "Download Log", optimization_download_url(@optimization.id, format: "log"), class: "btn btn-secondary ml-2" %>
<%= link_to "Get CSV", optimization_download_url(@optimization.id, format: "csv"), class: "btn btn-secondary ml-2" %>
<%= link_to "Add Input", edit_optimization_path(@optimization), class: "btn btn-primary ml-2"%>
<%= content_tag :div,
id: "optimization_plot",
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
post 'select'
get 'compare'
end
get 'download' => 'optimizations#download'
get 'download', to: 'optimization_exports#new'
end

namespace :api do
Expand Down
7 changes: 0 additions & 7 deletions test/controllers/optimizations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ class OptimizationsControllerTest < ActionDispatch::IntegrationTest
end
end

test "should get log file" do
skip_if_segomoe_not_installed
@ack.create_optimizer
get optimization_download_path(@ack.id)
assert_response :success
end

test "should get new" do
get new_optimization_url
assert_response :success
Expand Down

0 comments on commit 02f25f5

Please sign in to comment.