Class: Importance::ImportsController

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

Instance Method Summary collapse

Instance Method Details

#importObject

Import page. Load the file according to the mapping and import it. Mappings param is of the form mappings = target_attribute mappings = “first_name”, mappings = “”, mappings = “last_name” …



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/importance/imports_controller.rb', line 58

def import
  @layout = "Importance::#{Importance.configuration.layout.to_s.camelize}Layout".constantize
  @importer = Importance.configuration.importers[session[:importer].to_sym]
  @importer.add_spreadsheet(session[:path])

  if @importer.nil?
    flash[:alert] = t("importance.errors.no_importer")
    render :map, status: :unprocessable_entity and return
  end

  if params[:mappings].nil?
    flash[:alert] = t("importance.errors.no_mappings")
    render :map, status: :unprocessable_entity and return
  end

  @mappings = params[:mappings].permit!.to_h.map { |k, v| [ k.to_i, v ] }.to_h

  @importer.importer_attributes.each do |attribute|
    next if attribute.options[:optional]
    next if @mappings.values.include?(attribute.key.to_s)

    flash[:alert] = t("importance.errors.missing_mapping", attribute: attribute.labels.first)
    render :map, status: :unprocessable_entity and return
  end

  @mappings.each do |column_index, attribute_name|
    next if attribute_name == ""
    next if @mappings.values.count(attribute_name) <= 1

    attribute_label = @importer.importer_attributes.find { |attr| attr.key.to_s == attribute_name }.labels.first
    flash[:alert] = t("importance.errors.duplicate_mapping", attribute: attribute_label)
    render :map, status: :unprocessable_entity and return
  end

  if @importer.setup_callback
    instance_exec(&@importer.setup_callback)
  end

  records_to_import = []

  @importer.each_processed_row(session[:path], @mappings) do |record|
    records_to_import << record

    if @importer.batch && records_to_import.size >= @importer.batch
      instance_exec(records_to_import, &@importer.perform_callback)
      records_to_import = []
    end
  end

  if records_to_import.any?
    instance_exec(records_to_import, &@importer.perform_callback)
  end

  if @importer.teardown_callback
    instance_exec(&@importer.teardown_callback)
  else
    redirect_to (session[:redirect_url] || main_app.root_path), notice: t("importance.success.import_completed")
  end
rescue => e
  if @importer.error_callback
    instance_exec(e, &@importer.error_callback)
  end
end

#mapObject

Mapping page. Load headers and samples, display the form.



43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/importance/imports_controller.rb', line 43

def map
  @layout = "Importance::#{Importance.configuration.layout.to_s.camelize}Layout".constantize
  @importer = Importance.configuration.importers[session[:importer].to_sym]

  if @importer.nil?
    flash[:alert] = t("importance.errors.no_importer")
    redirect_to session[:redirect_url] and return
  end

  @importer.add_spreadsheet(session[:path])
end

#submitObject

Form submission target. Persist the file and redirect to the mapping page.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/importance/imports_controller.rb', line 7

def submit
  upload = params[:file]
  session[:redirect_url] = params[:redirect_url]
  session[:importer] = params[:importer].to_sym

  if upload.nil?
    flash[:alert] = t("importance.errors.no_file")
    redirect_to session[:redirect_url] and return
  end

  upload_extension = File.extname(upload.original_filename).downcase
  supported_extensions = [ ".xlsx", ".xls", ".csv" ]

  if !supported_extensions.include?(upload_extension)
    flash[:alert] = t("importance.errors.invalid_file_type", types: supported_extensions.join(", "))
    redirect_to session[:redirect_url] and return
  end

  system_tmp_dir = Dir.tmpdir
  upload_path = upload.tempfile.path
  persist_filename = "#{SecureRandom.uuid}#{upload_extension}"

  persist_path =  File.join(system_tmp_dir, persist_filename)
  session[:path] = persist_path

  if !File.exist?(upload_path)
    flash[:alert] = t("importance.errors.no_file")
    redirect_to session[:redirect_url] and return
  end

  FileUtils.mv(upload_path, persist_path)

  redirect_to map_path
end