Class: Decidim::Importers::ParticipatorySpaceComponentsImporter

Inherits:
Importer
  • Object
show all
Defined in:
app/serializers/decidim/importers/participatory_space_components_importer.rb

Overview

This class parses and imports all components in a ParticipatorySpace. It currently supports JSON format.

Instance Method Summary collapse

Constructor Details

#initialize(participatory_space) ⇒ ParticipatorySpaceComponentsImporter

participatory_space: The ParticipatorySpace to which all components will belong to.



10
11
12
# File 'app/serializers/decidim/importers/participatory_space_components_importer.rb', line 10

def initialize(participatory_space)
  @participatory_space = participatory_space
end

Instance Method Details

#from_json(json_text, user) ⇒ Object

Parses an exported list of components and imports them into the platform.

participatory_space: The ParticipatorySpace to which all components will belong to. json_text: A json document as a String. user: The Decidim::User that is importing.



21
22
23
24
# File 'app/serializers/decidim/importers/participatory_space_components_importer.rb', line 21

def from_json(json_text, user)
  json = JSON.parse(json_text)
  import(json, user)
end

#import(json_ary, user) ⇒ Object

For each component configuration in the json, creates a new Decidim::Component with that configuration.

Returns: An Array with all components created.

json: An array of json compatible Hashes with the configuration of Decidim::Components. user: The Decidim::User that is importing.



33
34
35
36
37
38
39
40
41
42
43
# File 'app/serializers/decidim/importers/participatory_space_components_importer.rb', line 33

def import(json_ary, user)
  ActiveRecord::Base.transaction do
    json_ary.collect do |serialized|
      attributes = serialized.with_indifferent_access.except(:id, :participatory_space_id, :participatory_space_type)
      step_settings = attributes["settings"]["steps"]
      # we override the parent participatory space steps id
      override_step_settings_ids(attributes, step_settings)
      import_component_from_attributes(attributes, user)
    end
  end
end