Class: Decidim::Cdtb::ParticipatorySpaces::AddContentBlocks

Inherits:
Task
  • Object
show all
Includes:
ManagesContentBlocks
Defined in:
lib/decidim/cdtb/participatory_spaces/add_content_blocks.rb

Overview

Add content blocks to participatory spaces

Instance Attribute Summary collapse

Attributes inherited from Task

#num_applied, #title

Instance Method Summary collapse

Methods included from ManagesContentBlocks

#current_space_content_blocks, #find_or_create_content_block, #manifest_for, #scope_name, #update_content_block_image

Methods inherited from Task

#execute!, #finish, #init

Methods included from TasksUtils

#do_log_error, #do_log_info, #log_task_end, #log_task_failure, #log_task_info, #log_task_step, #log_task_title, #logger

Constructor Details

#initialize(processed_models, content_block_names) ⇒ AddContentBlocks

Returns a new instance of AddContentBlocks.



12
13
14
15
16
17
# File 'lib/decidim/cdtb/participatory_spaces/add_content_blocks.rb', line 12

def initialize(processed_models, content_block_names)
  progress_bar= { title: self.class.name }
  @processed_models = processed_models
  @content_block_names = content_block_names
  super("ADD CONTENT BLOCKS", progress_bar:)
end

Instance Attribute Details

#num_addedObject (readonly)

Returns the value of attribute num_added.



19
20
21
# File 'lib/decidim/cdtb/participatory_spaces/add_content_blocks.rb', line 19

def num_added
  @num_added
end

Instance Method Details

#do_execution(context) ⇒ Object

rubocop:disable Metrics/AbcSize



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/decidim/cdtb/participatory_spaces/add_content_blocks.rb', line 35

def do_execution(context)
  progress_bar= context[:progress_bar]

  @processed_models.each do |processed_model|
    log_task_step("Processing #{processed_model}")

    spaces = processed_model

    @content_block_names.each do |content_block_name|
      log_task_step("Adding #{content_block_name} content block")

      spaces.find_each do |space|
        current_content_blocks = current_space_content_blocks(scope_name(space), space.organization, space.id)

        new_content_block = find_or_create_content_block(space, content_block_name)
        if content_block_name == "extra_data" && space.instance_of?(Decidim::ParticipatoryProcess)
          next if new_content_block.weight == 20

          force_extra_data_content_block_weight!(content_block_name, current_content_blocks)
        end

        @num_added += 1
        progress_bar.increment
      end
    end
  end
end

#end_execution(_ctx) ⇒ Object

rubocop:enable Metrics/AbcSize



64
65
66
# File 'lib/decidim/cdtb/participatory_spaces/add_content_blocks.rb', line 64

def end_execution(_ctx)
  log_task_step("#{@num_added} content blocks added")
end

#force_extra_data_content_block_weight!(content_block_name, current_content_blocks) ⇒ Object

extra_data content block usually be down of hero image, therefore, it’s weight is 20 and all others content blocks go one position down added 10



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/decidim/cdtb/participatory_spaces/add_content_blocks.rb', line 70

def force_extra_data_content_block_weight!(content_block_name, current_content_blocks)
  extra_data_content_block = current_content_blocks.find_by(manifest_name: content_block_name)
  extra_data_content_block.update(weight: 20)

  current_content_blocks.each do |content_block|
    # hero is usually the first content block
    next if content_block.nil?
    next if content_block == extra_data_content_block || content_block.manifest_name == "hero"

    content_block.update(weight: content_block.weight + 10)
  end
end

#prepare_execution(_ctx = nil) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/decidim/cdtb/participatory_spaces/add_content_blocks.rb', line 21

def prepare_execution(_ctx = nil)
  @num_added= @num_items= 0

  @processed_models.each do |model_name|
    @num_items+= model_name.count
  end
  log_task_info("Adding content blocks in #{@num_items} spaces...")
end

#total_itemsObject



30
31
32
# File 'lib/decidim/cdtb/participatory_spaces/add_content_blocks.rb', line 30

def total_items
  @num_items
end