Class: Admin::ImportsController

Inherits:
BaseController
  • Object
show all
Defined in:
lib/generators/open_loam/install/templates/admin/imports_controller.rb

Overview

Generic CSV importer, scoped by entity_type — manager-only (a privileged bulk mutation). Flow: new (upload) → preview (map columns) → create (dry-run summary, or a backgrounded run reporting live progress). The mapping only offers fields the manager may write (OpenLoam::Import.allowed_targets); a crafted target is refused by the engine.

Constant Summary

Constants included from Pagination

Pagination::PER_PAGE

Instance Method Summary collapse

Methods inherited from BaseController

skip_authorization!

Methods included from Pagination

#paginate

Instance Method Details

#create ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/open_loam/install/templates/admin/imports_controller.rb', line 26

def create
  @csv = params[:csv].to_s
  mapping = params[:mapping].to_unsafe_h.reject { |_h, target| target.blank? }
  match_key = params[:match_key].presence

  if params[:commit] == "Dry run"
    run_dry(mapping, match_key)
  else
    ImportJob.perform_later(entity_type: @entity_type, blob_id: staged_csv_blob(@csv).id, mapping: mapping,
                            match_key: match_key, tenant_id: current_tenant.id, actor_id: current_actor.id)
    redirect_to admin_progress_jobs_path, notice: "Import started — watch it progress under Tasks."
  end
rescue OpenLoam::Error => error
  redirect_to new_admin_import_path(entity_type: @entity_type), alert: error.message
end

#download_errors ⇒ Object

The failed rows as a fix-and-re-upload CSV.



43
44
45
46
47
48
49
50
51
52
# File 'lib/generators/open_loam/install/templates/admin/imports_controller.rb', line 43

def download_errors
  @entity_type = params[:entity_type]
  model = OpenLoam::Import.allowed_model(@entity_type)
  csv = params[:csv].to_s
  mapping = params[:mapping].to_unsafe_h.reject { |_h, target| target.blank? }
  result = OpenLoam::Import.run(csv, model: model, mapping: mapping, actor: current_actor,
                            match_key: params[:match_key].presence, dry_run: true)
  send_data OpenLoam::Import.error_csv(result, csv),
            filename: "#{@entity_type.underscore}-import-errors.csv", type: "text/csv"
end

#new ⇒ Object



11
12
13
# File 'lib/generators/open_loam/install/templates/admin/imports_controller.rb', line 11

def new
  @entity_type = params[:entity_type]
end

#preview ⇒ Object

Parse the uploaded file, show the mapping form + a few sample rows.



16
17
18
19
20
21
22
23
24
# File 'lib/generators/open_loam/install/templates/admin/imports_controller.rb', line 16

def preview
  @csv = read_upload
  preview = OpenLoam::Import.preview(@csv)
  @headers = preview[:headers]
  @sample = preview[:rows]
  @targets = OpenLoam::Import.allowed_targets(@model, current_actor).to_a.sort
rescue OpenLoam::Error => error
  redirect_to new_admin_import_path(entity_type: @entity_type), alert: error.message
end