Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/preview screen for import #19

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/controllers/importo/imports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ def new
@import = Import.new(kind: params[:kind], locale: I18n.locale)
end

def preview
@import = Import.find(params[:id])
if @import&.original&.attachment.present?
@original = Tempfile.new(['ActiveStorage', @import.original.filename.extension_with_delimiter])
@original.binmode
@import.original.download { |block| @original.write(block) }
@original.flush
@original.rewind
sheet = Roo::Excelx.new(@original.path)
@sheet_data = sheet.parse(headers: true)
end
redirect_to action: :new unless @sheet_data
end
def create
unless import_params
@import = Import.new(kind: params[:kind], locale: I18n.locale)
Expand All @@ -16,6 +29,18 @@ def create
return
end
@import = Import.new(import_params.merge(locale: I18n.locale, importo_ownable: Importo.config.current_import_owner))
if @import.valid?
@import.save!
redirect_to action: :preview, id: @import.id, kind: @import.kind
else
flash[:error] = t('.flash.error')
render :new
end
end

def upload
@import = Import.find(params[:id])
@import.confirm! if @import.can_confirm?
if @import.valid? && @import.schedule!
flash[:notice] = t('.flash.success', id: @import.id)
redirect_to action: :index
Expand All @@ -25,6 +50,13 @@ def create
end
end

def cancel
@import = Import.find(params[:id])
@import.original.purge if @import.concept?
flash[:notice] = t('.flash.cancel', id: @import.id)
redirect_to action: :new, kind: @import.kind
end

def undo
@import = Import.where(importo_ownable: Importo.config.current_import_owner).find(params[:id])
if @import.can_revert? && @import.revert
Expand Down
4 changes: 3 additions & 1 deletion app/jobs/importo/purge_import_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Importo
class PurgeImportJob < ApplicationJob
def perform(owner, months)
def perform(owner, months,state = nil)

imports = Import.where(importo_ownable: owner, created_at: ..months.months.ago.beginning_of_day)
imports = imports.where(state: state) if state

imports.each do |import|
import.original.purge
Expand Down
11 changes: 8 additions & 3 deletions app/models/importo/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class Import < Importo::ApplicationRecord
# Weird loading sequence error, is fixed by the lib/importo/helpers
end

state_machine :state, initial: :new do
state_machine :state, initial: :concept do
audit_trail class: ResourceStateTransition, as: :resource if "ResourceStateTransition".safe_constantize

state :confirmed
state :importing
state :scheduled
state :completed
Expand All @@ -36,11 +37,15 @@ class Import < Importo::ApplicationRecord
after_transition any => :reverting, do: :schedule_revert

event :schedule do
transition new: :scheduled
transition confirmed: :scheduled
end

event :confirm do
transition concept: :confirmed
end

event :import do
transition new: :importing
transition confirmed: :importing
transition scheduled: :importing
transition failed: :importing
end
Expand Down
31 changes: 31 additions & 0 deletions app/views/importo/imports/preview.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
= sts.form_for(@import, method: :post, url: upload_import_path(@import)) do |f|
= sts.card :"importo_imports #{@import.kind}", title: t('.title', kind: @import&.kind.capitalize), icon: 'fad fa-file-spreadsheet' do |card|
- card.action
/ = f.button 'Cancel', value: 'cancel'
= f.submit 'Upload', value: 'upload'
= link_to(t('.cancel'), cancel_import_path(@import), method: :post, class: 'button')
.px-4.sm:px-6.lg:px-8
/ .sm:flex.sm:items-center
/ .sm:flex-auto
/ h1.text-base.font-semibold.leading-6.text-gray-900
/ | Users
/ p.mt-2.text-sm.text-gray-700
/ | A list of all the users in your account including their name, title, email and role.
/ .mt-4.sm:ml-16.sm:mt-0.sm:flex-none
/ button.block.rounded-md.bg-indigo-600.px-3.py-2.text-center.text-sm.font-semibold.text-white.shadow-sm.hover:bg-indigo-500.focus-visible:outline.focus-visible:outline-2.focus-visible:outline-offset-2.focus-visible:outline-indigo-600[type="button"]
/ | Add user
.mt-8.flow-root
.-mx-4.-my-2.overflow-x-auto.sm:-mx-6.lg:-mx-8
.inline-block.min-w-full.py-2.align-middle.bg-white.dark:bg-gray-900.dark:border-gray-700
table.min-w-full.divide-y.divide-gray-20.border-2
thead.bg-gray-50
tr
- @sheet_data.first.keys.each_with_index do |header, index|
th.px-3.py-3.5.text-left.text-sm.font-semibold.border-2.text-gray-600[scope="col"]
= header
tbody.divide-y.divide-gray-200.bg-white
- @sheet_data.reject! { |h| h.keys == h.values }&.each do |h|
tr
- h.each do |key, value|
td.whitespace-nowrap.px-3.py-4.text-sm.text-gray-500.border-2
= value
12 changes: 11 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ en:
helpers:
submit:
importo/import:
create: "Import"
create: "Preview"
importo:
sheet:
results:
Expand All @@ -14,6 +14,8 @@ en:
explanation: Purpose
example: Example
imports:
cancel:
success: "Import canceled for id %{id}, Redirecting to new"
index:
title: Import results
new:
Expand All @@ -29,6 +31,14 @@ en:
no_file: Import failed, please upload a file.
error: Import failed, there were problems.
success: "Import scheduled with id %{id}, you will get an email with the results."
preview:
title: "%{kind} Import Preview"
cancel: Cancel
upload:
flash:
no_file: Import failed, please upload a file.
error: Import failed, there were problems.
success: "Import scheduled with id %{id}, you will get an email with the results."
errors:
structure_invalid: "The structure is invalid, these are the invalid headers: %{invalid_headers}"
importers:
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
resources :imports, except: %i[new] do
member do
post :undo
post :upload
post :cancel
end
end
get ':kind/new', to: 'imports#new', as: :new_import
get ':kind/sample', to: 'imports#sample', as: :sample_import
get ':kind/export', to: 'imports#export', as: :export
get ':kind/:id/preview', to: 'imports#preview', as: :preview
root to: 'imports#index'
end
6 changes: 6 additions & 0 deletions test/models/importo/import_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ImportTest < ActiveSupport::TestCase
import = Import.new(importo_ownable: Account.create(name: 'test'), kind: 'account')
import.original.attach(io: simple_sheet([%w[id name description], %w[aid atest atest-description]]), filename: 'simple_sheet.xlsx')
assert import.save, import.errors.messages
import.confirm
import.schedule
assert_equal 'scheduled', import.state
import.import
Expand All @@ -53,6 +54,7 @@ class ImportTest < ActiveSupport::TestCase
import = Import.new(importo_ownable: Account.create(name: 'test'), kind: 'account')
import.original.attach(io: simple_sheet([%w[id name description], ['aid', '<strong>a</strong>test', '<strong>atest</strong>-description']]), filename: 'simple_sheet.xlsx')
assert import.save, import.errors.messages
import.confirm
import.schedule
assert_equal 'scheduled', import.state
import.import
Expand All @@ -73,6 +75,7 @@ class ImportTest < ActiveSupport::TestCase
import = Import.new(importo_ownable: Account.create(name: 'test'), kind: 'no_header_account')
import.original.attach(io: simple_sheet([%w[aid atest atest-description], %w[bid btest btest-description]]), filename: 'simple_sheet.xlsx')
assert import.save
import.confirm
import.schedule
assert_equal 'scheduled', import.state
import.import
Expand Down Expand Up @@ -129,6 +132,7 @@ class ImportTest < ActiveSupport::TestCase
import = Import.new(importo_ownable: Account.create(name: 'test'), kind: 'translated_account', locale: 'nl')
import.original.attach(io: simple_sheet([%w[], %w[a b c], ['Record ID', 'Naam', 'Omschrijving'], %w[aid atest atest-description]]), filename: 'simple_sheet.xlsx')
assert import.save, import.errors.messages
import.confirm
import.schedule
assert_equal 'scheduled', import.state
import.import
Expand All @@ -140,6 +144,7 @@ class ImportTest < ActiveSupport::TestCase
import = Import.new(importo_ownable: Account.create(name: 'test'), kind: 'translated_account')
import.original.attach(io: simple_sheet([%w[], %w[a b c], ['Record ID', 'Naam', 'Omschrijving'], %w[aid atest atest-description]]), filename: 'simple_sheet.xlsx')
assert import.save, import.errors.messages
import.confirm
import.schedule
assert_equal 'scheduled', import.state
import.import
Expand All @@ -151,6 +156,7 @@ class ImportTest < ActiveSupport::TestCase
import = Import.new(importo_ownable: Account.create(name: 'test'), kind: 'translated_account')
import.original.attach(io: simple_sheet([%w[], %w[a b c], ['Record ID', 'Naam', 'Description'], %w[aid atest atest-description]]), filename: 'simple_sheet.xlsx')
assert import.save, import.errors.messages
import.confirm
import.schedule
assert_equal 'scheduled', import.state
import.import
Expand Down