Class: Decidim::Admin::ImportsController

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

Overview

This controller allows admins to import resources from a file.

Instance Method Summary collapse

Methods included from ComponentPathHelper

#can_be_managed?, #main_component_path, #main_component_url, #manage_component_path

Methods inherited from ApplicationController

#permission_class_chain, #permission_scope, #user_has_no_permission_path, #user_not_authorized_path

Methods included from HttpCachingDisabler

#disable_http_caching

Methods included from NeedsSnippets

#snippets

Methods included from RegistersPermissions

register_permissions

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#createObject


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

def create
  enforce_permission_to :import, :component_data, component: current_component

  @form = form(Admin::ImportForm).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",
                         number: imported_data.length,
                         resource_name: imported_data.first.resource_manifest.name.pluralize)
      redirect_to manage_component_path(current_component)
    end

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

#newObject


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

def new
  enforce_permission_to :import, :component_data, component: current_component
  @form = form(Admin::ImportForm).from_params(
    {
      # We need to set "default" creator because form-class doesn't have context / current_component
      # when it sets it's default values.
      creator: current_component.manifest.import_manifests.first.creator
    },
    current_component: current_component
  )
end