Class: Decidim::Admin::ImportForm

Inherits:
Form
  • Object
show all
Defined in:
decidim-admin/app/forms/decidim/admin/import_form.rb

Constant Summary collapse

ACCEPTED_MIME_TYPES =
Decidim::Admin::Import::Readers::ACCEPTED_MIME_TYPES

Instance Method Summary collapse

Instance Method Details

#accepted_mime_typeObject


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'decidim-admin/app/forms/decidim/admin/import_form.rb', line 33

def accepted_mime_type
  accepted_mime_types = ACCEPTED_MIME_TYPES.values
  return true if accepted_mime_types.include?(mime_type)
  # Avoid duplicating error messages
  return false if errors[:file].present?

  errors.add(
    :file,
    I18n.t(
      "activemodel.errors.new_import.attributes.file.invalid_mime_type",
      valid_mime_types: ACCEPTED_MIME_TYPES.keys.map do |m|
        I18n.t("decidim.admin.new_import.accepted_mime_types.#{m}")
      end.join(", ")
    )
  )
  false
end

#check_invalid_linesObject


17
18
19
20
21
22
23
# File 'decidim-admin/app/forms/decidim/admin/import_form.rb', line 17

def check_invalid_lines
  return if file.blank? || !accepted_mime_type

  importer.prepare
  invalid_lines = importer.invalid_lines
  errors.add(:file, I18n.t("decidim.admin.imports.invalid_lines", invalid_lines: invalid_lines.join(","))) unless invalid_lines.empty?
end

#creator_classObject


57
58
59
60
61
# File 'decidim-admin/app/forms/decidim/admin/import_form.rb', line 57

def creator_class
  return creator.constantize if creator.is_a?(String)

  creator
end

#creatorsObject


51
52
53
54
55
# File 'decidim-admin/app/forms/decidim/admin/import_form.rb', line 51

def creators
  @creators ||= current_component.manifest.import_manifests.map do |manifest|
    { creator: manifest.creator, name: manifest.creator.to_s.split("::").last.downcase }
  end
end

#file_pathObject


25
26
27
# File 'decidim-admin/app/forms/decidim/admin/import_form.rb', line 25

def file_path
  file&.path
end

#importerObject


70
71
72
# File 'decidim-admin/app/forms/decidim/admin/import_form.rb', line 70

def importer
  @importer ||= importer_for(file_path, mime_type)
end

#importer_for(filepath, mime_type) ⇒ Object


74
75
76
77
78
79
80
81
82
# File 'decidim-admin/app/forms/decidim/admin/import_form.rb', line 74

def importer_for(filepath, mime_type)
  context[:user_group] = user_group
  Import::ImporterFactory.build(
    filepath,
    mime_type,
    context: context,
    creator: creator_class
  )
end

#mime_typeObject


29
30
31
# File 'decidim-admin/app/forms/decidim/admin/import_form.rb', line 29

def mime_type
  file&.content_type
end

#user_groupObject


63
64
65
66
67
68
# File 'decidim-admin/app/forms/decidim/admin/import_form.rb', line 63

def user_group
  @user_group ||= Decidim::UserGroup.find_by(
    organization: current_organization,
    id: user_group_id.to_i
  )
end