2024-05-18 09:00:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-03-15 18:27:31 -04:00
|
|
|
class ImportsController < ApplicationController
|
2025-04-03 12:41:05 -04:00
|
|
|
include ActiveStorage::SetCurrent
|
|
|
|
|
|
2024-03-15 18:27:31 -04:00
|
|
|
before_action :authenticate_user!
|
2025-02-19 15:23:11 -05:00
|
|
|
before_action :authenticate_active_user!, only: %i[new create]
|
2024-05-18 09:00:44 -04:00
|
|
|
before_action :set_import, only: %i[show destroy]
|
2024-03-15 18:27:31 -04:00
|
|
|
|
|
|
|
|
def index
|
2024-09-08 10:52:35 -04:00
|
|
|
@imports =
|
|
|
|
|
current_user
|
|
|
|
|
.imports
|
2025-04-03 12:41:05 -04:00
|
|
|
.select(:id, :name, :source, :created_at, :processed)
|
2024-09-08 10:52:35 -04:00
|
|
|
.order(created_at: :desc)
|
|
|
|
|
.page(params[:page])
|
2024-03-15 18:27:31 -04:00
|
|
|
end
|
|
|
|
|
|
2024-05-18 09:00:44 -04:00
|
|
|
def show; end
|
2024-03-15 18:27:31 -04:00
|
|
|
|
|
|
|
|
def new
|
|
|
|
|
@import = Import.new
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
files = import_params[:files].reject(&:blank?)
|
2024-04-25 16:28:34 -04:00
|
|
|
|
2025-03-23 16:00:31 -04:00
|
|
|
files.each do |file|
|
|
|
|
|
import = current_user.imports.build(
|
2024-03-24 13:05:39 -04:00
|
|
|
name: file.original_filename,
|
2024-03-24 13:55:35 -04:00
|
|
|
source: params[:import][:source]
|
2024-03-24 13:05:39 -04:00
|
|
|
)
|
2024-03-15 18:27:31 -04:00
|
|
|
|
2025-03-23 16:00:31 -04:00
|
|
|
import.file.attach(io: file, filename: file.original_filename, content_type: file.content_type)
|
2024-05-31 17:18:57 -04:00
|
|
|
|
2025-03-23 16:00:31 -04:00
|
|
|
import.save!
|
2024-04-06 16:31:33 -04:00
|
|
|
end
|
|
|
|
|
|
2024-03-24 13:05:39 -04:00
|
|
|
redirect_to imports_url, notice: "#{files.size} files are queued to be imported in background", status: :see_other
|
2024-03-15 18:27:31 -04:00
|
|
|
rescue StandardError => e
|
2024-03-24 13:05:39 -04:00
|
|
|
Import.where(user: current_user, name: files.map(&:original_filename)).destroy_all
|
2024-03-24 13:55:35 -04:00
|
|
|
|
2024-03-15 18:27:31 -04:00
|
|
|
flash.now[:error] = e.message
|
|
|
|
|
|
|
|
|
|
redirect_to new_import_path, notice: e.message, status: :unprocessable_entity
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
2025-02-15 12:49:30 -05:00
|
|
|
Imports::Destroy.new(current_user, @import).call
|
2024-06-30 11:47:36 -04:00
|
|
|
|
2024-05-18 09:00:44 -04:00
|
|
|
redirect_to imports_url, notice: 'Import was successfully destroyed.', status: :see_other
|
2024-03-15 18:27:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def set_import
|
|
|
|
|
@import = Import.find(params[:id])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def import_params
|
|
|
|
|
params.require(:import).permit(:source, files: [])
|
|
|
|
|
end
|
|
|
|
|
end
|