Class: Decidim::ContentBlocksCreator

Inherits:
Object
  • Object
show all
Defined in:
app/services/decidim/content_blocks_creator.rb

Overview

Service to generate content blocks on spaces o resources which have content blocks registered for them on their engines.

This class is initialized passing an space which can be an organization, a participatory space with content blocks like processes or assemblies or a participatory process group.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(space) ⇒ ContentBlocksCreator

Returns a new instance of ContentBlocksCreator.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/decidim/content_blocks_creator.rb', line 13

def initialize(space)
  @space = space
  @is_organization = space.is_a? Decidim::Organization
  @organization = is_organization ? space : space.organization
  @scoped_resource_id = is_organization ? nil : space.id
  manifest = manifest_for(space)
  @scope_name = is_organization ? :homepage : manifest.content_blocks_scope_name
  raise ArgumentError, "[ERROR] The #{manifest.name} spaces do not define a content blocks scope" if scope_name.blank?

  @default_content_blocks = Decidim.content_blocks.for(scope_name).select(&:default)
  @component_content_blocks = Decidim.content_blocks.for(scope_name).select(&:component_manifest_name)
end

Instance Attribute Details

#component_content_blocksObject (readonly)

Returns the value of attribute component_content_blocks.



11
12
13
# File 'app/services/decidim/content_blocks_creator.rb', line 11

def component_content_blocks
  @component_content_blocks
end

#default_content_blocksObject (readonly)

Returns the value of attribute default_content_blocks.



11
12
13
# File 'app/services/decidim/content_blocks_creator.rb', line 11

def default_content_blocks
  @default_content_blocks
end

#is_organizationObject (readonly)

Returns the value of attribute is_organization.



11
12
13
# File 'app/services/decidim/content_blocks_creator.rb', line 11

def is_organization
  @is_organization
end

#organizationObject (readonly)

Returns the value of attribute organization.



11
12
13
# File 'app/services/decidim/content_blocks_creator.rb', line 11

def organization
  @organization
end

#scope_nameObject (readonly)

Returns the value of attribute scope_name.



11
12
13
# File 'app/services/decidim/content_blocks_creator.rb', line 11

def scope_name
  @scope_name
end

#scoped_resource_idObject (readonly)

Returns the value of attribute scoped_resource_id.



11
12
13
# File 'app/services/decidim/content_blocks_creator.rb', line 11

def scoped_resource_id
  @scoped_resource_id
end

#spaceObject (readonly)

Returns the value of attribute space.



11
12
13
# File 'app/services/decidim/content_blocks_creator.rb', line 11

def space
  @space
end

Instance Method Details

#create_components_blocks!Object

This method only works in participatory spaces. For all the components published in the space creates an associated content block if registered for the sope and component and is not created configured for the component or globally to cover all components of the same type.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/decidim/content_blocks_creator.rb', line 50

def create_components_blocks!
  return unless space.is_a? Decidim::Participable

  space.components.published.each_with_index do |component, index|
    block_manifest = component_content_blocks.find { |manifest| manifest.component_manifest_name == component.manifest_name }

    next unless block_manifest

    content_blocks = Decidim::ContentBlock.where(decidim_organization_id: organization.id, scope_name:, manifest_name: block_manifest.name, scoped_resource_id:)

    # Ignore creation if there is already a content block for the same
    # component or a general content block for all the components of the
    # same type in the space
    next if content_blocks.any? do |block|
      component_id = block.settings.component_id
      component_id.blank? || component_id.to_i == component.id
    end

    weight = ((index + 1) * 10) + 1000

    Decidim::ContentBlock.create(
      decidim_organization_id: organization.id,
      weight:,
      scope_name:,
      scoped_resource_id:,
      manifest_name: block_manifest.name,
      settings: { component_id: component.id.to_s },
      published_at: Time.current
    )
  end
end

#create_default!Object

Creates all content blocks registered as default for the space or resource unless one created with the same manifest already exists in the same space.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/decidim/content_blocks_creator.rb', line 29

def create_default!
  default_content_blocks.each_with_index do |manifest, index|
    next if Decidim::ContentBlock.exists?(decidim_organization_id: organization.id, scope_name:, manifest_name: manifest.name, scoped_resource_id:)

    weight = (index + 1) * 10

    Decidim::ContentBlock.create(
      decidim_organization_id: organization.id,
      weight:,
      scope_name:,
      scoped_resource_id:,
      manifest_name: manifest.name,
      published_at: Time.current
    )
  end
end