Class: Decidim::Proposals::Admin::ImportParticipatoryTextForm

Inherits:
Form
  • Object
show all
Includes:
TranslatableAttributes
Defined in:
decidim-proposals/app/forms/decidim/proposals/admin/import_participatory_text_form.rb

Overview

A form object to be used when admin users want to import a collection of proposals from a participatory text.

Constant Summary collapse

ACCEPTED_MIME_TYPES =

WARNING: consider adding/removing the relative translation key at decidim.assemblies.admin.new_import.accepted_types when modifying this hash

Decidim::Proposals::DocToMarkdown::ACCEPTED_MIME_TYPES

Instance Method Summary collapse

Methods included from TranslatableAttributes

#default_locale?

Instance Method Details

#default_localeObject


57
58
59
# File 'decidim-proposals/app/forms/decidim/proposals/admin/import_participatory_text_form.rb', line 57

def default_locale
  current_participatory_space.organization.default_locale
end

#document_textObject


61
62
63
# File 'decidim-proposals/app/forms/decidim/proposals/admin/import_participatory_text_form.rb', line 61

def document_text
  @document_text ||= document&.read
end

#document_typeObject


41
42
43
# File 'decidim-proposals/app/forms/decidim/proposals/admin/import_participatory_text_form.rb', line 41

def document_type
  document.content_type
end

#document_type_must_be_validObject


30
31
32
33
34
# File 'decidim-proposals/app/forms/decidim/proposals/admin/import_participatory_text_form.rb', line 30

def document_type_must_be_valid
  return if valid_mime_types.include?(document_type)

  errors.add(:document, i18n_invalid_document_type_text)
end

#i18n_invalid_document_type_textObject


45
46
47
48
49
# File 'decidim-proposals/app/forms/decidim/proposals/admin/import_participatory_text_form.rb', line 45

def i18n_invalid_document_type_text
  I18n.t("invalid_document_type",
         scope: "activemodel.errors.models.participatory_text.attributes.document",
         valid_mime_types: i18n_valid_mime_types_text)
end

#i18n_valid_mime_types_textObject


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

def i18n_valid_mime_types_text
  ACCEPTED_MIME_TYPES.keys.map do |mime_type|
    I18n.t(mime_type, scope: "decidim.proposals.admin.participatory_texts.new_import.accepted_mime_types")
  end.join(", ")
end

#new_participatory_text?Boolean

Assume it's a NEW participatory_text if there are no proposals Validate document presence while CREATING proposals from document Allow skipping document validation while UPDATING title/description

Returns:

  • (Boolean)

26
27
28
# File 'decidim-proposals/app/forms/decidim/proposals/admin/import_participatory_text_form.rb', line 26

def new_participatory_text?
  Decidim::Proposals::Proposal.where(component: current_component).blank?
end

#valid_mime_typesObject

Return ACCEPTED_MIME_TYPES plus `text/plain` for better markdown support


37
38
39
# File 'decidim-proposals/app/forms/decidim/proposals/admin/import_participatory_text_form.rb', line 37

def valid_mime_types
  ACCEPTED_MIME_TYPES.values + [Decidim::Proposals::DocToMarkdown::TEXT_PLAIN_MIME_TYPE] + ["application/octet-stream"]
end