2024-05-17 09:09:32 -04:00
|
|
|
require "ostruct"
|
|
|
|
|
|
|
|
class ImportsController < ApplicationController
|
2024-08-23 10:06:24 -04:00
|
|
|
before_action :set_import, except: %i[index new create]
|
2024-05-17 09:09:32 -04:00
|
|
|
|
|
|
|
def index
|
|
|
|
@imports = Current.family.imports
|
|
|
|
render layout: "with_sidebar"
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2024-06-11 18:47:38 -04:00
|
|
|
account = Current.family.accounts.find_by(id: params[:account_id])
|
|
|
|
@import = Import.new account: account
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
account = Current.family.accounts.find(params[:import][:account_id])
|
2024-08-16 20:00:16 +02:00
|
|
|
@import.update! account: account, col_sep: params[:import][:col_sep]
|
2024-05-17 09:09:32 -04:00
|
|
|
|
|
|
|
redirect_to load_import_path(@import), notice: t(".import_updated")
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
account = Current.family.accounts.find(params[:import][:account_id])
|
2024-08-16 20:00:16 +02:00
|
|
|
@import = Import.create! account: account, col_sep: params[:import][:col_sep]
|
2024-05-17 09:09:32 -04:00
|
|
|
|
|
|
|
redirect_to load_import_path(@import), notice: t(".import_created")
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@import.destroy!
|
|
|
|
redirect_to imports_url, notice: t(".import_destroyed"), status: :see_other
|
|
|
|
end
|
|
|
|
|
|
|
|
def load
|
|
|
|
end
|
|
|
|
|
2024-07-16 15:23:45 +02:00
|
|
|
def upload_csv
|
|
|
|
begin
|
2024-08-19 14:25:07 +01:00
|
|
|
@import.raw_file_str = import_params[:raw_file_str].read
|
2024-07-16 15:23:45 +02:00
|
|
|
rescue NoMethodError
|
2024-07-18 14:39:38 -04:00
|
|
|
flash.now[:alert] = "Please select a file to upload"
|
2024-07-16 15:23:45 +02:00
|
|
|
render :load, status: :unprocessable_entity and return
|
|
|
|
end
|
|
|
|
if @import.save
|
|
|
|
redirect_to configure_import_path(@import), notice: t(".import_loaded")
|
|
|
|
else
|
2024-07-18 14:39:38 -04:00
|
|
|
flash.now[:alert] = @import.errors.full_messages.to_sentence
|
2024-07-16 15:23:45 +02:00
|
|
|
render :load, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-17 09:09:32 -04:00
|
|
|
def load_csv
|
|
|
|
if @import.update(import_params)
|
|
|
|
redirect_to configure_import_path(@import), notice: t(".import_loaded")
|
|
|
|
else
|
2024-07-18 14:39:38 -04:00
|
|
|
flash.now[:alert] = @import.errors.full_messages.to_sentence
|
2024-05-17 09:09:32 -04:00
|
|
|
render :load, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def configure
|
|
|
|
unless @import.loaded?
|
|
|
|
redirect_to load_import_path(@import), alert: t(".invalid_csv")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_mappings
|
|
|
|
@import.update! import_params(@import.expected_fields.map(&:key))
|
|
|
|
redirect_to clean_import_path(@import), notice: t(".column_mappings_saved")
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean
|
|
|
|
unless @import.loaded?
|
|
|
|
redirect_to load_import_path(@import), alert: t(".invalid_csv")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_csv
|
|
|
|
update_params = import_params[:csv_update]
|
|
|
|
|
|
|
|
@import.update_csv! \
|
|
|
|
row_idx: update_params[:row_idx],
|
|
|
|
col_idx: update_params[:col_idx],
|
|
|
|
value: update_params[:value]
|
|
|
|
|
|
|
|
render :clean
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm
|
|
|
|
unless @import.cleaned?
|
|
|
|
redirect_to clean_import_path(@import), alert: t(".invalid_data")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish
|
|
|
|
if @import.valid?
|
|
|
|
@import.publish_later
|
|
|
|
redirect_to imports_path, notice: t(".import_published")
|
|
|
|
else
|
|
|
|
flash.now[:error] = t(".invalid_data")
|
|
|
|
render :confirm, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_import
|
|
|
|
@import = Current.family.imports.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_params(permitted_mappings = nil)
|
2024-08-19 14:25:07 +01:00
|
|
|
params.require(:import).permit(:raw_file_str, column_mappings: permitted_mappings, csv_update: [ :row_idx, :col_idx, :value ])
|
2024-05-17 09:09:32 -04:00
|
|
|
end
|
|
|
|
end
|