Module: Decidim::Cdtb::ParticipatorySpaces::ManagesContentBlocks

Included in:
AddContentBlocks, MoveImagesToContentBlock
Defined in:
lib/decidim/cdtb/participatory_spaces/manages_content_blocks.rb

Overview

Methods for use in participatory spaces tasks

Instance Method Summary collapse

Instance Method Details

#current_space_content_blocks(scope_name, organization, scoped_resource_id) ⇒ Object



41
42
43
# File 'lib/decidim/cdtb/participatory_spaces/manages_content_blocks.rb', line 41

def current_space_content_blocks(scope_name, organization, scoped_resource_id)
  Decidim::ContentBlock.for_scope(scope_name, organization:).where(scoped_resource_id:)
end

#find_or_create_content_block(space, content_block_name) ⇒ Object

rubocop:disable Metrics/AbcSize



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/decidim/cdtb/participatory_spaces/manages_content_blocks.rb', line 9

def find_or_create_content_block(space, content_block_name)
  current_content_blocks = current_space_content_blocks(scope_name(space), space.organization, space.id)
  exists_content_block = Decidim::ContentBlock.find_by(decidim_organization_id: space.organization.id,
                                                       scope_name: scope_name(space), manifest_name: content_block_name,
                                                       scoped_resource_id: space.id)

  return exists_content_block if exists_content_block.present?

  weight = (current_content_blocks.last.weight + 1) * 10
  log_task_step("Adding #{content_block_name} to #{space.slug}[#{space.id}]")
  Decidim::ContentBlock.create(
    decidim_organization_id: space.organization.id,
    weight:,
    scope_name: scope_name(space),
    scoped_resource_id: space.id,
    manifest_name: content_block_name,
    published_at: Time.current
  )
end

#manifest_for(resource) ⇒ Object



45
46
47
48
# File 'lib/decidim/cdtb/participatory_spaces/manages_content_blocks.rb', line 45

def manifest_for(resource)
  return resource.manifest if resource.is_a? Decidim::Participable
  return resource.resource_manifest if resource.is_a? Decidim::Resourceable
end

#scope_name(space) ⇒ Object



50
51
52
# File 'lib/decidim/cdtb/participatory_spaces/manages_content_blocks.rb', line 50

def scope_name(space)
  manifest_for(space).content_blocks_scope_name
end

#update_content_block_image(content_block, image) ⇒ Object

rubocop:enable Metrics/AbcSize



30
31
32
33
34
35
36
37
38
39
# File 'lib/decidim/cdtb/participatory_spaces/manages_content_blocks.rb', line 30

def update_content_block_image(content_block, image)
  content_block.manifest.images.each do |image_config|
    image_name = image_config[:name]

    next if content_block.images_container.send(image_name).present?

    content_block.images_container.send("#{image_name}=", image.blob)
    content_block.save
  end
end