Class: Decidim::Cdtb::Fixes::YouTubeEmbedsFixer

Inherits:
Task
  • Object
show all
Defined in:
lib/decidim/cdtb/fixes/you_tube_embeds_fixer.rb

Overview

Fixes YouTube embeds to Decidim v0.28 format in PROCESSED_MODELS. Only YouTube is supported right now.

Constant Summary collapse

PROCESSED_MODELS =
{
  "Decidim::Meetings::Meeting" => [:description],
  "Decidim::Debates::Debate" => i[description instructions],
  "Decidim::StaticPage" => [:content],
  "Decidim::Pages::Page" => [:body],
  "Decidim::Assembly" => i[short_description description],
  "Decidim::ParticipatoryProcess" => i[short_description description],
  "Decidim::Proposals::Proposal" => i[body]
}.freeze

Instance Attribute Summary collapse

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

#initializeYouTubeEmbedsFixer

Returns a new instance of YouTubeEmbedsFixer.



19
20
21
22
# File 'lib/decidim/cdtb/fixes/you_tube_embeds_fixer.rb', line 19

def initialize
  progress_bar= { title: self.class.name }
  super("FIX YOUTUBE EMBEDS", progress_bar:)
end

Instance Attribute Details

#num_fixedObject (readonly)

Returns the value of attribute num_fixed.



24
25
26
# File 'lib/decidim/cdtb/fixes/you_tube_embeds_fixer.rb', line 24

def num_fixed
  @num_fixed
end

Instance Method Details

#do_execution(context) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/decidim/cdtb/fixes/you_tube_embeds_fixer.rb', line 39

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

  PROCESSED_MODELS.each_pair do |model_class_name, attribs|
    log_task_step("Processing #{model_class_name.pluralize}")

    model_class= model_class_name.constantize
    model_class.find_each do |model|
      Rails.logger.debug("Processing #{model_class_name}[#{model.id}]")

      attribs.each do |attribute|
        fix_embed(model, attribute)
      end

      @num_fixed+= 1 if model.changed?
      model.save!(validate: false)
      progress_bar.increment
    end
  end
end

#end_execution(_ctx) ⇒ Object



60
61
62
# File 'lib/decidim/cdtb/fixes/you_tube_embeds_fixer.rb', line 60

def end_execution(_ctx)
  log_task_step("#{@num_fixed} embeds fixed")
end

#prepare_execution(_ctx = nil) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/decidim/cdtb/fixes/you_tube_embeds_fixer.rb', line 26

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

  PROCESSED_MODELS.each_key do |model_class|
    @num_items+= model_class.constantize.count
  end
  log_task_info("Checking #{@num_items} models...")
end

#total_itemsObject



35
36
37
# File 'lib/decidim/cdtb/fixes/you_tube_embeds_fixer.rb', line 35

def total_items
  @num_items
end