Class: Decidim::Importers::ImportManifest
- Inherits:
-
Object
- Object
- Decidim::Importers::ImportManifest
- Includes:
- AttributeObject::Model
- Defined in:
- 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
-
#manifest ⇒ Object
readonly
Returns the value of attribute manifest.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#creator(creator = nil) ⇒ Object
Public: Sets the creator when an argument is provided, returns the stored creator otherwise.
-
#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.
- #form_class ⇒ Object
-
#has_example? ⇒ Boolean
Returns a boolean indicating whether the example is available or not.
-
#has_message?(key) ⇒ Boolean
Returns a boolean indicating whether the message exists with the given key.
-
#initialize(name, manifest) ⇒ ImportManifest
constructor
Initializes the manifest.
-
#message(key, context = nil, **extra) ⇒ Object
Define a message or render the message in the given context.
-
#messages ⇒ Object
Fetch the messages object or yield it for the block when a block is given.
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 '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
#manifest ⇒ Object (readonly)
Returns the value of attribute manifest.
11 12 13 |
# File 'lib/decidim/importers/import_manifest.rb', line 11 def manifest @manifest end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File '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 '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 '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_class ⇒ Object
36 37 38 |
# File '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.
111 112 113 |
# File '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.
81 82 83 |
# File 'lib/decidim/importers/import_manifest.rb', line 81 def (key) .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.(: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.(:title)
OR
manifest.(:title, self)
Or alternatively render with extra arguments (self = context within a view):
manifest.(:resource_name, count: 2)
OR
manifest.(: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 'lib/decidim/importers/import_manifest.rb', line 70 def (key, context = nil, **extra, &) extra = context if extra.empty? && context.is_a?(Hash) if block_given? .set(key, &) else .render(key, context, **extra) end end |
#messages ⇒ Object
Fetch the messages object or yield it for the block when a block is given.
42 43 44 45 46 47 48 |
# File 'lib/decidim/importers/import_manifest.rb', line 42 def if block_given? yield @messages else @messages end end |