Class: Decidim::Admin::ImportsController

Inherits:
ApplicationController show all
Includes:
ComponentPathHelper
Defined in:
app/controllers/decidim/admin/imports_controller.rb

Overview

This controller allows admins to import resources from a file.

Instance Method Summary collapse

Methods inherited from ApplicationController

#permission_class_chain, #permission_scope, #user_has_no_permission_path, #user_not_authorized_path

Instance Method Details

#createObject

Raises:

  • (ActionController::RoutingError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/decidim/admin/imports_controller.rb', line 21

def create
  enforce_permission_to :import, :component_data, component: current_component
  raise ActionController::RoutingError, "Not Found" unless import_manifest

  @form = form(import_manifest.form_class).from_params(
    params,
    current_component: current_component,
    current_organization: current_organization
  )

  CreateImport.call(@form) do
    on(:ok) do |imported_data|
      flash[:notice] = t("decidim.admin.imports.notice",
                         count: imported_data.length,
                         resource_name: import_manifest.message(:resource_name, count: imported_data.length))
      redirect_to manage_component_path(current_component)
    end

    on(:invalid) do
      flash.now[:alert] = t("decidim.admin.imports.error")
      render :new
    end
  end
end

#exampleObject

Raises:

  • (ActionController::RoutingError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/decidim/admin/imports_controller.rb', line 46

def example
  enforce_permission_to :import, :component_data, component: current_component
  raise ActionController::RoutingError, "Not Found" unless import_manifest

  @form = form(Decidim::Admin::ImportExampleForm).from_params(params).with_context(
    current_component: current_component,
    current_organization: current_organization
  )

  respond_to do |format|
    @form.available_formats.each do |key, mime|
      format.public_send(key) do
        CreateImportExample.call(@form) do
          on(:ok) do |data|
            filename = "#{current_component.manifest_name}-#{import_manifest.name}-example.#{key}"
            send_data data.read, disposition: :attachment, filename: filename, type: mime
          end

          on(:invalid) do
            flash[:alert] = t("decidim.admin.imports.example_error")
            redirect_to admin_imports_path(current_component, name: import_name)
          end
        end
      end
    end
  end
end

#newObject

Raises:

  • (ActionController::RoutingError)


11
12
13
14
15
16
17
18
19
# File 'app/controllers/decidim/admin/imports_controller.rb', line 11

def new
  enforce_permission_to :import, :component_data, component: current_component
  raise ActionController::RoutingError, "Not Found" unless import_manifest

  @form = form(import_manifest.form_class).from_params(
    { name: import_manifest.name },
    current_component: current_component
  )
end