Class: Decidim::Exporters::ExportManifest

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, AttributeObject::Model
Defined in:
decidim-core/lib/decidim/exporters/export_manifest.rb

Overview

This class serves as a DSL to declaratively specify which artifacts are exportable in a parent manifest. A parent manifest is the manifest that will be used by the Serializer to take the settings of the element to serialize. For example a parent manifest may be a ParticipatorySpaceManifest or a ComponentManifest.

Constant Summary collapse

DEFAULT_FORMATS =
%w(CSV JSON Excel).freeze

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) ⇒ ExportManifest

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 export manifest belongs to.



28
29
30
31
32
# File 'decidim-core/lib/decidim/exporters/export_manifest.rb', line 28

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

Instance Attribute Details

#manifestObject (readonly)

Returns the value of attribute manifest.



19
20
21
# File 'decidim-core/lib/decidim/exporters/export_manifest.rb', line 19

def manifest
  @manifest
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'decidim-core/lib/decidim/exporters/export_manifest.rb', line 19

def name
  @name
end

Instance Method Details

#collection(&block) ⇒ Object

Public: Sets the collection block when a block is given, or returns the previously setted collection block if no block is provided.

The collection block knows how to obtain the collection of elements to be serialized by the Serializer.

The collection block should be invoked like ‘export.collection.call(artifact_type)` and, when evaluated, will get passed an instance of the parent artifact type, `Decidim::ParticipatorySpace` or `Decidim::Component` for example, so you can easily find the elements to export. It also receives, as a second parameter, the user triggering the action, in case you need to filter the collection based on the user.

The collection block in the end should return the collection of elements to be serialized.

&block - An optional block that returns the collection once evaluated.

Returns the stored collection.



54
55
56
57
58
59
60
# File 'decidim-core/lib/decidim/exporters/export_manifest.rb', line 54

def collection(&block)
  if block_given?
    @collection = block
  else
    @collection
  end
end

#formats(formats = nil) ⇒ Object

Public: Sets the available formats if an argument is provided and loads the required exporters, returns the array with the default available formats otherwise.

The formats array is used to define which exporters are available in the component. Each member of the array is a string with the name of the exporter class that will instantiated when needed.

formats - The array containing the available formats.

Returns the stored formats if previously stored, or the default formats array.



90
91
92
93
# File 'decidim-core/lib/decidim/exporters/export_manifest.rb', line 90

def formats(formats = nil)
  load_exporters(formats)
  @formats ||= formats || DEFAULT_FORMATS
end

#serializer(serializer = nil) ⇒ Object

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

A ‘Serializer` will be run against each and every element of the collection in order to extract and process the relevant fields.

serializer - A subclass of ‘Decidim::Exporters::Serializer`.

Returns the stored serializer if previously stored, or ‘Decidim::Exporters::Serializer` as a default implementation.



72
73
74
# File 'decidim-core/lib/decidim/exporters/export_manifest.rb', line 72

def serializer(serializer = nil)
  @serializer ||= serializer || Decidim::Exporters::Serializer
end