Class: Decidim::Cdtb::Comments::OrphanedRemover

Inherits:
Task
  • Object
show all
Defined in:
lib/decidim/cdtb/comments/orphaned_remover.rb

Overview

Removes Decidim::Comments::Comment's that are orphaned: their participatory space can no longer be resolved, be it because their root commentable (e.g. a proposal or a meeting) was removed without cascading its comments, or because the participatory space itself was removed.

This is the same condition Decidim::Cdtb::Users::Remover skips when reporting/hiding comments, since Decidim::CreateReport requires a participatory_space to create the Moderation.

Rows are deleted with delete_all (no callbacks, no validations) on purpose: Decidim::Searchable#remove_from_index blows up for these comments, since it ends up calling comment.organization.id and organization is nil for an orphaned comment.

Instance Attribute Summary

Attributes inherited from Task

#num_applied, #title

Instance Method Summary collapse

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

#initializeOrphanedRemover

Returns a new instance of OrphanedRemover.



21
22
23
24
25
# File 'lib/decidim/cdtb/comments/orphaned_remover.rb', line 21

def initialize
  @root_commentable_orphaned = {}
  progress_bar = { title: "Decidim::Comments::Comment" }
  super("REMOVE ORPHANED COMMENTS", progress_bar:)
end

Instance Method Details

#do_execution(context) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/decidim/cdtb/comments/orphaned_remover.rb', line 36

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

  query.find_each do |comment|
    remove_comment(comment) if orphaned?(comment)
    progress_bar.increment
  end
end

#end_execution(_ctx) ⇒ Object



45
46
47
# File 'lib/decidim/cdtb/comments/orphaned_remover.rb', line 45

def end_execution(_ctx)
  log_task_step("#{@num_applied} orphaned comments removed")
end

#prepare_execution(_ctx) ⇒ Object



27
28
29
30
# File 'lib/decidim/cdtb/comments/orphaned_remover.rb', line 27

def prepare_execution(_ctx)
  @num_comments = query.count
  log_task_info("Checking #{@num_comments} comments...")
end

#total_itemsObject



32
33
34
# File 'lib/decidim/cdtb/comments/orphaned_remover.rb', line 32

def total_items
  @num_comments
end