Class: Decidim::Admin::Import::Creator

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/admin/import/creator.rb

Overview

This is an abstract class with a very naive default implementation for the importers to use. It can also serve as a superclass of your own implementation.

It is used to be run against each element of an importable collection in order to parse relevant fields. Every import should specify their own creator or this default will be used.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, context = nil) ⇒ Creator

Initializes the creator with a resource.

data - The data hash to parse. context - The context needed by the producer



20
21
22
23
# File 'lib/decidim/admin/import/creator.rb', line 20

def initialize(data, context = nil)
  @data = data.except(:id, "id")
  @context = context
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



14
15
16
# File 'lib/decidim/admin/import/creator.rb', line 14

def data
  @data
end

Class Method Details

.resource_klassObject

Retuns the resource class to be created with the provided data.

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/decidim/admin/import/creator.rb', line 26

def self.resource_klass
  raise NotImplementedError, "#{self.class.name} does not define resource class"
end

Instance Method Details

#finish!Object



49
50
51
# File 'lib/decidim/admin/import/creator.rb', line 49

def finish!
  resource.save!
end

#produceObject

Public: Returns a created object with the parsed data.

Returns a target object.



45
46
47
# File 'lib/decidim/admin/import/creator.rb', line 45

def produce
  self.class.resource_klass.new(resource_attributes)
end

#resource_attributesObject

Can be used to convert the data hash to the resource attributes in case the data hash to be imported has different column names than the resource object to be created of it.

By default returns the data hash but can be implemented by each creator implementation.

Returns the resource attributes to be passed for the constructor.



38
39
40
# File 'lib/decidim/admin/import/creator.rb', line 38

def resource_attributes
  @data
end