Class: Decidim::Importers::ImportManifest

Inherits:
Object
  • Object
show all
Includes:
AttributeObject::Model
Defined in:
decidim-core/lib/decidim/importers/import_manifest.rb

Overview

For importing data from files to components. Every resource type should specify it is own creator, which will be responsible for producing (creating) and finishing (saving) the imported resource.

Defined Under Namespace

Classes: ImportManifestMessages

Constant Summary

Constants included from AttributeObject::TypeMap

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AttributeObject::Model

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

Constructor Details

#initialize(name, manifest) ⇒ ImportManifest

Initializes the manifest.

name - The name of the export artifact. It should be unique in the

space or component.

manifest - The parent manifest where this import manifest belongs to.



23
24
25
26
27
28
# File 'decidim-core/lib/decidim/importers/import_manifest.rb', line 23

def initialize(name, manifest)
  super()
  @name = name.to_sym
  @manifest = manifest
  @messages = ImportManifestMessages.new
end

Instance Attribute Details

#manifestObject (readonly)

Returns the value of attribute manifest.



11
12
13
# File 'decidim-core/lib/decidim/importers/import_manifest.rb', line 11

def manifest
  @manifest
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'decidim-core/lib/decidim/importers/import_manifest.rb', line 11

def name
  @name
end

Instance Method Details

#creator(creator = nil) ⇒ Object

Public: Sets the creator when an argument is provided, returns the stored creator otherwise.



32
33
34
# File 'decidim-core/lib/decidim/importers/import_manifest.rb', line 32

def creator(creator = nil)
  @creator ||= creator || Decidim::Admin::Import::Creator
end

#example(context = nil, component = nil, &block) ⇒ Object

Either define example import data when providing a block or fetch the example data for the given context and component.

When defining example data:

manifest.example do |component|
  organization = component.organization
  [
    %w(id name") + organization.available_locales.map { |l| "title/#{l}" },
    [1, "John Doe"] + organization.available_locales.map { "Manager" },
    [2, "Joanna Doe"] + organization.available_locales.map { "Manager" },
  ]
end

When fetching example data:

data = manifest.example(self, current_component)

Returns either the example data or nothing when defining the example.



102
103
104
105
106
107
108
# File 'decidim-core/lib/decidim/importers/import_manifest.rb', line 102

def example(context = nil, component = nil, &block)
  if block_given?
    @example = block
  elsif has_example?
    context.instance_exec(component, &@example)
  end
end

#form_classObject



36
37
38
# File 'decidim-core/lib/decidim/importers/import_manifest.rb', line 36

def form_class
  form_class_name.constantize
end

#has_example?Boolean

Returns a boolean indicating whether the example is available or not.

Returns:



111
112
113
# File 'decidim-core/lib/decidim/importers/import_manifest.rb', line 111

def has_example?
  @example.present?
end

#has_message?(key) ⇒ Boolean

Returns a boolean indicating whether the message exists with the given key.

Returns:



81
82
83
# File 'decidim-core/lib/decidim/importers/import_manifest.rb', line 81

def has_message?(key)
  messages.has?(key)
end

#message(key, context = nil, **extra) ⇒ Object

Define a message or render the message in the given context.

For defining a message:

manifest.message(:title) { I18n.t("decidim.foos.admin.imports.title.answers") }

Within the definition block, you can use ‘self` to refer to the context where the message is displayed but beware that it may also be `nil`.

For rendering the message (self = context within a view):

manifest.message(:title)
OR
manifest.message(:title, self)

Or alternatively render with extra arguments (self = context within a view):

manifest.message(:resource_name, count: 2)
OR
manifest.message(:resource_name, self, count: 2)

Returns either the set value (the block) when defining the message or the message String when rendering the message.



70
71
72
73
74
75
76
77
78
# File 'decidim-core/lib/decidim/importers/import_manifest.rb', line 70

def message(key, context = nil, **extra, &)
  extra = context if extra.empty? && context.is_a?(Hash)

  if block_given?
    messages.set(key, &)
  else
    messages.render(key, context, **extra)
  end
end

#messagesObject

Fetch the messages object or yield it for the block when a block is given.



42
43
44
45
46
47
48
# File 'decidim-core/lib/decidim/importers/import_manifest.rb', line 42

def messages
  if block_given?
    yield @messages
  else
    @messages
  end
end