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

Inherits:
Form show all
Includes:
HasUploadValidations, 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

Constants included from AttributeObject::TypeMap

AttributeObject::TypeMap::Boolean, AttributeObject::TypeMap::Decimal

Instance Attribute Summary

Attributes inherited from AttributeObject::Form

#context

Instance Method Summary collapse

Methods included from HasUploadValidations

#attached_uploader, #maximum_avatar_size, #maximum_upload_size

Methods included from TranslatableAttributes

#default_locale?

Methods inherited from AttributeObject::Form

ensure_hash, from_model, from_params, hash_from, infer_model_name, #map_model, mimic, mimicked_model_name, model_name, #persisted?, #to_key, #to_model, #to_param, #valid?, #with_context

Methods included from AttributeObject::Model

#[], #[]=, #attributes, #attributes_with_values, #initialize, #to_h

Instance Method Details

#default_localeObject


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

def default_locale
  current_participatory_space.organization.default_locale
end

#document_textObject


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

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

#document_typeObject


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

def document_type
  document.content_type
end

#document_type_must_be_validObject


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

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


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

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

#i18n_valid_mime_types_textObject


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

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:


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

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


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

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