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

#createObject



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

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 @import.valid? && @import.schedule!
    redirect_to importo.new_import_path(params[:kind] || @import.kind)
  else
    Signum.error(Current.user, text: t('.flash.error', error: @import.errors&.full_messages&.join('.')))
    render :new
  end
end

#destroyObject



37
38
39
40
41
42
43
# File 'app/controllers/importo/imports_controller.rb', line 37

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



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

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



57
58
59
# File 'app/controllers/importo/imports_controller.rb', line 57

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

#sampleObject



45
46
47
48
49
# File 'app/controllers/importo/imports_controller.rb', line 45

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



28
29
30
31
32
33
34
35
# File 'app/controllers/importo/imports_controller.rb', line 28

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