Class: Decidim::ComponentManifest

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

Overview

This class handles all the logic associated to configuring a component associated to a participatory process.

It is normally not used directly but through the API exposed through ‘Decidim.register_component`.

Constant Summary

Constants included from AttributeObject::TypeMap

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

Instance Method Summary collapse

Methods included from AttributeObject::Model

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

Instance Method Details

#component_form_classObject

Public: Finds the form class from its component, using the ‘component_form_class_name` attribute. If the class does not exist, it raises an exception. If the class name is not set, it returns nil.

Returns a Class.



257
258
259
# File 'decidim-core/lib/decidim/component_manifest.rb', line 257

def component_form_class
  component_form_class_name&.constantize
end

#export_manifestsObject

Pubic: Returns a collection of previously registered export manifests for this component.

Returns an Array<Decidim::Exporters::ExportManifest>.



197
198
199
200
201
202
203
# File 'decidim-core/lib/decidim/component_manifest.rb', line 197

def export_manifests
  @export_manifests ||= Array(@exports).map do |(name, block)|
    Decidim::Exporters::ExportManifest.new(name, self).tap do |manifest|
      block.call(manifest)
    end
  end
end

#exports(name, &block) ⇒ Object

Public: Registers an export artifact with a name and its properties defined in ‘Decidim::Exporters::ExportManifest`.

Export artifacts provide an unified way for components to register exportable collections serialized via a ‘Serializer` that eventually are transformed to their formats.

name - The name of the artifact. Should be unique in the context of

the component.

block - A block that receives the manifest as its only argument.

Returns nothing.



185
186
187
188
189
190
191
# File 'decidim-core/lib/decidim/component_manifest.rb', line 185

def exports(name, &block)
  return unless name

  @exports ||= []
  @exports << [name, block]
  @export_manifests = nil
end

#import_manifestsObject



213
214
215
216
217
218
219
# File 'decidim-core/lib/decidim/component_manifest.rb', line 213

def import_manifests
  @import_manifests ||= Array(@imports).map do |(name, block)|
    Decidim::Importers::ImportManifest.new(name, self).tap do |manifest|
      block.call(manifest)
    end
  end
end

#imports(name, &block) ⇒ Object



205
206
207
208
209
210
211
# File 'decidim-core/lib/decidim/component_manifest.rb', line 205

def imports(name, &block)
  return unless name

  @imports ||= []
  @imports << [name, block]
  @import_manifests = nil
end

#on(event_name, &block) ⇒ Object

Public: Registers a hook to this manifest. Hooks get fired when some lifecycle events happen, like the creation of a component or its destruction.

event_name - A String or Symbol with the event name. &block - The block to run when the hook gets triggered.

Returns nothing.



99
100
101
102
# File 'decidim-core/lib/decidim/component_manifest.rb', line 99

def on(event_name, &block)
  hooks[event_name.to_sym] ||= []
  hooks[event_name.to_sym] << block
end

#permissions_classObject

Public: Finds the permission class from its name, using the ‘permissions_class_name` attribute. If the class does not exist, it raises an exception. If the class name is not set, it returns nil.

Returns a Class.



248
249
250
# File 'decidim-core/lib/decidim/component_manifest.rb', line 248

def permissions_class
  permissions_class_name&.constantize
end

#register_resource(name) ⇒ Object

Public: Registers a resource. Exposes a DSL defined by ‘Decidim::ResourceManifest`. Automatically sets the component manifest for that resource to the current one.

Resource manifests are a way to expose a resource from one engine to the whole system. This way resources can be linked between them.

name - A name for that resource. Should be singular (ie not plural). block - A Block that will be called to set the Resource attributes.

Returns nothing.



290
291
292
293
294
295
296
297
298
299
# File 'decidim-core/lib/decidim/component_manifest.rb', line 290

def register_resource(name)
  my_component_manifest = self

  my_block = proc do |resource|
    resource.component_manifest = my_component_manifest
    yield(resource)
  end

  Decidim.register_resource(name, &my_block)
end

#register_stat(name, options = {}) ⇒ Object

Public: Registers a stat inside a component manifest.

name - The name of the stat options - A hash of options

* primary: Whether the stat is primary or not.
* priority: The priority of the stat used for render issues.

block - A block that receive the components to filter out the stat.

Returns nothing.



239
240
241
# File 'decidim-core/lib/decidim/component_manifest.rb', line 239

def register_stat(name, options = {}, &)
  stats.register(name, options, &)
end

#reset_hooks!Object

Semiprivate: Resets all the hooks of this manifest. Mostly useful when testing.

Returns nothing.



124
125
126
# File 'decidim-core/lib/decidim/component_manifest.rb', line 124

def reset_hooks!
  self.hooks = {}
end

#route_nameObject

The name of the named Rails route to create the url to the resource.

Returns a String.



148
149
150
# File 'decidim-core/lib/decidim/component_manifest.rb', line 148

def route_name
  "component"
end

#run_hooks(event_name, context = nil) ⇒ Object

Public: Runs all the hooks associated with this manifest and a particular event.

event_name - A String or Symbol with the event name. context - An optional context that will be provided to the block as a

parameter. Usually the subject of the hook.

Returns nothing.



112
113
114
115
116
117
118
# File 'decidim-core/lib/decidim/component_manifest.rb', line 112

def run_hooks(event_name, context = nil)
  return unless hooks[event_name]

  hooks[event_name.to_sym].map do |hook|
    hook.call(context)
  end
end

#seed!(participatory_space) ⇒ Object

Public: Creates the seeds for this components in order to populate the database.

Returns nothing.



138
139
140
141
142
143
# File 'decidim-core/lib/decidim/component_manifest.rb', line 138

def seed!(participatory_space)
  # rubocop:disable Rails/Output
  print "-- Creating #{name} component seeds for the participatory space with ID: #{participatory_space.id}...\n" unless Rails.env.test?
  # rubocop:enable Rails/Output
  @seeds&.call(participatory_space)
end

#seeds(&block) ⇒ Object

Public: A block that gets called when seeding for this component takes place.

Returns nothing.



131
132
133
# File 'decidim-core/lib/decidim/component_manifest.rb', line 131

def seeds(&block)
  @seeds = block
end

#serializes_specific_data?Boolean

Returns:



221
222
223
# File 'decidim-core/lib/decidim/component_manifest.rb', line 221

def serializes_specific_data?
  serializes_specific_data
end

#settings(name = :global) {|settings| ... } ⇒ Object

Public: Adds configurable attributes for this component, scoped to a name. It uses the DSL specified under ‘Decidim::SettingsManifest`.

name - Either ‘global` or `step` &block - The DSL present on `Decidim::SettingsManifest`

Examples:

component.settings(:global) do |settings|
  settings.attribute :voting_enabled, type: :boolean, default: true
end

Returns nothing.

Yields:



165
166
167
168
169
170
171
# File 'decidim-core/lib/decidim/component_manifest.rb', line 165

def settings(name = :global, &block)
  @settings ||= {}
  name = name.to_sym
  settings = (@settings[name] ||= SettingsManifest.new)
  yield(settings) if block
  settings
end

#specific_data_importer_classObject

Public: Finds the specific data importer class from its name, using the ‘specific_data_importer_class_name` attribute. If the class does not exist, it raises an exception. If the class name is not set, it returns nil.

Returns a Decidim::Importers::Importer subclass or nil.



275
276
277
# File 'decidim-core/lib/decidim/component_manifest.rb', line 275

def specific_data_importer_class
  specific_data_importer_class_name&.constantize
end

#specific_data_serializer_classObject

Public: Finds the specific data serializer class from its name, using the ‘specific_data_serializer_class_name` attribute. If the class does not exist, it raises an exception. If the class name is not set, it returns nil.

Returns a Decidim::Exporters::Serializer subclass or nil.



266
267
268
# File 'decidim-core/lib/decidim/component_manifest.rb', line 266

def specific_data_serializer_class
  specific_data_serializer_class_name&.constantize
end

#statsObject

Public: Stores an instance of StatsRegistry



226
227
228
# File 'decidim-core/lib/decidim/component_manifest.rb', line 226

def stats
  @stats ||= StatsRegistry.new
end