Class: Decidim::ContentBlockRegistry

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/lib/decidim/content_block_registry.rb

Overview

This class acts as a registry for content blocks. Check the docs on the `ContentBlockManifest` class to learn how they work.

In order to register a content block, you can follow this example:

Decidim.content_blocks.register(:homepage, :global_stats) do |content_block|
  content_block.cell = "decidim/content_blocks/stats_block"
  content_block.public_name_key = "decidim.content_blocks.stats_block.name"
  content_block.settings_form_cell = "decidim/content_blocks/stats_block_settings_form"

  content_block.settings do |settings|
    settings.attribute :minimum_priority_level,
                       type: :integer
                       default: lambda { StatsRegistry::HIGH_PRIORITY }
  end
end

Content blocks can also register attached images. Here's an example of a content block with 4 attached images:

Decidim.content_blocks.register(:homepage, :carousel) do |content_block|
  content_block.cell = "decidim/content_blocks/carousel_block"
  content_block.public_name_key = "decidim.content_blocks.carousel_block.name"

  content_block.images = [
    {
      name: :image_1,
      uploader: "Decidim::ImageUploader"
    },
    {
      name: :image_2,
      uploader: "Decidim::ImageUploader"
    }
  ]
end

You will probably want to register your content blocks in an initializer in the `engine.rb` file of your module.

Defined Under Namespace

Classes: ContentBlockAlreadyRegistered

Instance Method Summary collapse

Instance Method Details

#allObject


75
76
77
# File 'decidim-core/lib/decidim/content_block_registry.rb', line 75

def all
  content_blocks
end

#for(scope) ⇒ Object


71
72
73
# File 'decidim-core/lib/decidim/content_block_registry.rb', line 71

def for(scope)
  content_blocks[scope.to_s]
end

#register(scope, name) {|content_block| ... } ⇒ Object

Public: Registers a content block for the home page.

scope - a symbol or string representing the scope of the content block.

Will be persisted as a string.

name - a symbol representing the name of the content block &block - The content block definition.

Returns nothing. Raises an error if there's already a content block registered with that name.

Yields:

  • (content_block)

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'decidim-core/lib/decidim/content_block_registry.rb', line 52

def register(scope, name)
  scope = scope.to_s
  block_exists = content_blocks[scope].any? { |content_block| content_block.name == name }

  if block_exists
    raise(
      ContentBlockAlreadyRegistered,
      "There's a content block already registered with the name `:#{name}` for the scope `:#{scope}, must be unique"
    )
  end

  content_block = ContentBlockManifest.new(name: name)

  yield(content_block)

  content_block.validate!
  content_blocks[scope].push(content_block)
end