Class: Spree::Admin::BaseImportOrdersController

Inherits:
ResourceController
  • Object
show all
Defined in:
app/controllers/spree/admin/base_import_orders_controller.rb

Instance Method Summary collapse

Instance Method Details

#build_import_order(name, imported_file) ⇒ Object



33
34
35
36
37
# File 'app/controllers/spree/admin/base_import_orders_controller.rb', line 33

def build_import_order(name, imported_file)
  import_order = model_class.new_order.new(name: name)
  import_order.imported_file.attach(imported_file) if imported_file.present?
  import_order
end

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/spree/admin/base_import_orders_controller.rb', line 8

def create
  name = permitted_resource_params[:name]
  imported_file = permitted_resource_params[:imported_file]

  unless imported_file&.original_filename&.match?(/\.(csv)$/)
    flash[:error] = I18n.t('import_orders.invalid_file')
    redirect_to collection_url and return
  end

  import_order = build_import_order(name, imported_file)

  if import_order.save
    SpreeCmCommissioner::ImportOrderJob.perform_later(import_order.id, spree_current_user.id, import_order.import_type)
    flash[:success] = I18n.t('import_orders.success_message')
  else
    flash[:error] = I18n.t('import_orders.error_message')
  end

  redirect_to collection_url
end

#downloadObject

GET: /admin/orders/import_new_orders/:id/download GET: /admin/orders/import_existing_orders/:id/download



49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/spree/admin/base_import_orders_controller.rb', line 49

def download
  result = SpreeCmCommissioner::ImportedCsvDownloader.call(import_order_id: params[:id])

  if result.success?
    send_data result.file_data, filename: result.filename,
                                type: result.content_type,
                                disposition: 'attachment'
  else
    flash[:error] = result.error
    redirect_to collection_url
  end
end

#model_classObject



39
40
41
# File 'app/controllers/spree/admin/base_import_orders_controller.rb', line 39

def model_class
  SpreeCmCommissioner::Imports::ImportOrder
end

#object_nameObject



29
30
31
# File 'app/controllers/spree/admin/base_import_orders_controller.rb', line 29

def object_name
  'spree_cm_commissioner_imports_import_order'
end

#permitted_resource_paramsObject



43
44
45
# File 'app/controllers/spree/admin/base_import_orders_controller.rb', line 43

def permitted_resource_params
  params.require(object_name).permit(:name).merge(imported_file: params[:imported_file])
end

#showObject



4
5
6
# File 'app/controllers/spree/admin/base_import_orders_controller.rb', line 4

def show
  @import ||= model_class.find(params[:id])
end