Class: Decidim::FeatureManifest

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/decidim/feature_manifest.rb

Overview

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

It’s normally not used directly but through the API exposed through ‘Decidim.register_feature`.

Instance Method Summary collapse

Instance Method Details

#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.



39
40
41
42
# File 'lib/decidim/feature_manifest.rb', line 39

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

#register_resource {|manifest| ... } ⇒ Object

Public: Registers a resource inside a feature manifest. Exposes a DSL defined by ‘Decidim::ResourceManifest`.

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

block - A Block that will be called to set the Resource attributes.

Returns nothing.

Yields:

  • (manifest)


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

def register_resource
  manifest = ResourceManifest.new
  manifest.feature_manifest = self
  yield(manifest)
  manifest.validate!
  resource_manifests << manifest
end

#reset_hooks!Object

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

Returns nothing.



64
65
66
# File 'lib/decidim/feature_manifest.rb', line 64

def reset_hooks!
  self.hooks = {}
end

#resource_manifestsObject

Public: Finds all the registered resource manifest’s via the ‘register_resource` method.

Returns an Array.



124
125
126
# File 'lib/decidim/feature_manifest.rb', line 124

def resource_manifests
  @resource_manifests ||= []
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, mostly the
Component.

Returns nothing.



53
54
55
56
57
58
# File 'lib/decidim/feature_manifest.rb', line 53

def run_hooks(event_name, context = nil)
  return unless hooks[event_name]
  hooks[event_name.to_sym].each do |hook|
    hook.call(context)
  end
end

#seed!Object

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

Returns nothing.



78
79
80
# File 'lib/decidim/feature_manifest.rb', line 78

def seed!
  @seeds&.call
end

#seeds(&block) ⇒ Object

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

Returns nothing.



71
72
73
# File 'lib/decidim/feature_manifest.rb', line 71

def seeds(&block)
  @seeds = block
end

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

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

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

Examples:

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

Returns nothing.

Yields:



95
96
97
98
99
100
101
# File 'lib/decidim/feature_manifest.rb', line 95

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