Class: Importo::ImportsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/importo/imports_controller.rb

Instance Method Summary collapse

Methods included from MaintenanceStandards

#flash_and_redirect

Instance Method Details

#cancelObject



51
52
53
54
55
56
57
# File 'app/controllers/importo/imports_controller.rb', line 51

def cancel
  @import = Import.find(params[:id])
  @import.original.purge if @import.concept?
  Signum.error(Current.user, text: t('.flash.cancel', id: @import.id))
  @import.destroy!
  redirect_to action: :new, kind: @import.kind
end

#createObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/importo/imports_controller.rb', line 29

def create
  unless import_params
    @import = Import.new(kind: params[:kind], locale: I18n.locale)
    Signum.error(Current.user, text: t(".flash.no_file"))
    render :new
    return
  end
  @import = Import.new(import_params.merge(locale: I18n.locale,
                                           importo_ownable: Importo.config.current_import_owner.call))
  if params["commit"] == "Upload" && @import.valid? && @import.save!
    @import.confirm!
    @import.schedule!
    redirect_to importo.new_import_path(params[:kind] || @import.kind)                                         
  elsif params["commit"] == "Preview" && @import.valid?
    @import.save!
    redirect_to action: :preview, id: @import.id, kind: @import.kind 
  else
    Signum.error(Current.user, text: t(".flash.error", error: @import.errors&.full_messages&.join(".")))
    render :new
  end
end

#destroyObject



80
81
82
83
84
85
86
# File 'app/controllers/importo/imports_controller.rb', line 80

def destroy
  @import = Import.find(params[:id])
  redirect_to(action: :index, alert: "Not allowed") && return unless Importo.config.admin_can_destroy.call(@import)

  @import.destroy
  redirect_to action: :index
end

#exportObject



94
95
96
97
98
# File 'app/controllers/importo/imports_controller.rb', line 94

def export
  import = Import.new(kind: params[:kind], locale: I18n.locale)
  send_data import.importer.export_file.read,
    type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename: import.importer.file_name("export")
end

#indexObject



100
101
102
# File 'app/controllers/importo/imports_controller.rb', line 100

def index
  @imports = Importo.config.admin_visible_imports.call.order(created_at: :desc).limit(50)
end

#newObject



7
8
9
# File 'app/controllers/importo/imports_controller.rb', line 7

def new
  @import = Import.new(kind: params[:kind], locale: I18n.locale)
end

#previewObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/importo/imports_controller.rb', line 11

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)
    @check_header = @sheet_data.reject{ |h| h.keys == h.values }.map{|h| h.transform_values(&:to_s).compact_blank}.reduce({}, :merge).keys
  end
  if @sheet_data.nil? || @sheet_data.reject{ |h| h.keys == h.values }.blank?
    Signum.error(Current.user, text: t('.no_file'))
    redirect_to action: :new
  end
end

#sampleObject



88
89
90
91
92
# File 'app/controllers/importo/imports_controller.rb', line 88

def sample
  import = Import.new(kind: params[:kind], locale: I18n.locale)
  send_data import.importer.sample_file.read,
    type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename: import.importer.file_name("sample")
end

#undoObject



59
60
61
62
63
64
65
66
# File 'app/controllers/importo/imports_controller.rb', line 59

def undo
  @import = Import.where(importo_ownable: Importo.config.current_import_owner.call).find(params[:id])
  if @import.can_revert? && @import.revert
    redirect_to action: :index, notice: "Import reverted"
  else
    redirect_to action: :index, alert: 'Import could not be reverted'
  end
end

#uploadObject



68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/importo/imports_controller.rb', line 68

def upload
  @import = Import.find(params[:id])
  @import.checked_columns = params[:selected_items]&.reject { |element| element == "0" }
  @import.confirm! if @import.can_confirm?
  if @import.valid? && @import.schedule!
    redirect_to action: :index
  else
    Signum.error(Current.user, text: t('.flash.error', id: @import.id))
    render :new
  end
end