dawarich/app/controllers/imports_controller.rb

72 lines
1.7 KiB
Ruby
Raw Normal View History

2024-05-18 09:00:44 -04:00
# frozen_string_literal: true
2024-03-15 18:27:31 -04:00
class ImportsController < ApplicationController
include ActiveStorage::SetCurrent
2024-03-15 18:27:31 -04:00
before_action :authenticate_user!
before_action :authenticate_active_user!, only: %i[new create]
2025-04-18 13:48:02 -04:00
before_action :set_import, only: %i[show edit update destroy]
2024-03-15 18:27:31 -04:00
def index
2024-09-08 10:52:35 -04:00
@imports =
current_user
.imports
.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
2025-04-18 13:48:02 -04:00
def edit; end
2024-03-15 18:27:31 -04:00
def new
@import = Import.new
end
2025-04-18 13:48:02 -04:00
def update
@import.update(import_params)
redirect_to imports_url, notice: 'Import was successfully updated.', status: :see_other
end
2024-03-15 18:27:31 -04:00
def create
files = import_params[:files].reject(&:blank?)
files.each do |file|
import = current_user.imports.build(
name: file.original_filename,
2024-03-24 13:55:35 -04:00
source: params[:import][:source]
)
2024-03-15 18:27: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
import.save!
end
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
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-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