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



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

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
28
29
30
31
32
# 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: import_order.id,
      import_by_user_id: spree_current_user.id,

      import_type: 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



54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/spree/admin/base_import_orders_controller.rb', line 54

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



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

def model_class
  SpreeCmCommissioner::Imports::ImportOrder
end

#object_nameObject



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

def object_name
  'spree_cm_commissioner_imports_import_order'
end

#permitted_resource_paramsObject



48
49
50
# File 'app/controllers/spree/admin/base_import_orders_controller.rb', line 48

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