Class: Oembed::LiquidTagMigrator

Inherits:
Object
  • Object
show all
Defined in:
app/services/oembed/liquid_tag_migrator.rb

Overview

Migrates legacy Liquid video tags in article content to oEmbed HTML.

Tags handled:
cloudflare_video 'slug' % → WyVideoProvider HTML5 player + EmbeddedVideoAsset
video_tag 'slug' % → WyVideoProvider HTML5 player + EmbeddedVideoAsset

The articles.solution column is the single source of truth — update_columns
is used to bypass callbacks (url sanitizer, revision hooks, embedding triggers)
that must not fire during a bulk data migration.

Scope: all Article subclasses (blog posts, FAQs, procedures, etc.) — not just Posts.

Usage:

Dry run — reports what would change without writing anything

result = Oembed::LiquidTagMigrator.new(dry_run: true).migrate_all
puts result.inspect

Live run

result = Oembed::LiquidTagMigrator.new.migrate_all

Constant Summary collapse

CLOUDFLARE_VIDEO_REGEX =

cloudflare_video 'slug' % or cloudflare_video "slug" %

/\{%\s*cloudflare_video\s+['"]?([^'"}\s]+)['"]?\s*%\}/i
VIDEO_TAG_REGEX =

video_tag 'slug' % or video_tag "slug" %

/\{%\s*video_tag\s+['"]?([^'"}\s]+)['"]?\s*%\}/i
LEGACY_TAG_LIKE_CONDITIONS =
[
  "solution LIKE '%{%% cloudflare_video%'",
  "solution LIKE '%{%% video_tag%'"
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(dry_run: false) ⇒ LiquidTagMigrator

Returns a new instance of LiquidTagMigrator.



36
37
38
39
40
41
42
43
44
45
# File 'app/services/oembed/liquid_tag_migrator.rb', line 36

def initialize(dry_run: false)
  @dry_run = dry_run
  @stats = {
    articles_scanned: 0,
    articles_updated: 0,
    videos_migrated: 0,
    errors: [],
    updated_articles: []
  }
end

Instance Method Details

#migrate_allHash

Migrate all articles that still contain legacy Liquid video tags.

Returns:

  • (Hash)

    Migration statistics



49
50
51
52
53
54
# File 'app/services/oembed/liquid_tag_migrator.rb', line 49

def migrate_all
  articles_with_legacy_tags.find_each do |article|
    migrate_article(article)
  end
  @stats
end

#migrate_article(article) ⇒ Boolean Also known as: migrate_post

Migrate a single article.
Updates both articles.solution and the active revision's solution.

Parameters:

Returns:

  • (Boolean)

    true if the article was (or would be) updated



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/services/oembed/liquid_tag_migrator.rb', line 60

def migrate_article(article)
  @stats[:articles_scanned] += 1

  content = article.solution
  return false if content.blank?
  return false unless legacy_tags_present?(content)

  updated_content = migrate_video_tags(article, content)

  return false if updated_content == content

  @stats[:articles_updated] += 1
  @stats[:updated_articles] << { id: article.id, subject: article.subject, type: article.type }

  unless @dry_run
    now = Time.current

    article.update_columns(solution: updated_content, updated_at: now)

    Rails.logger.info "[LiquidTagMigrator] Migrated article ##{article.id}: #{article.subject}"
  end

  true
rescue StandardError => e
  @stats[:errors] << "Article ##{article.id}: #{e.message}"
  Rails.logger.error "[LiquidTagMigrator] Error on article ##{article.id}: #{e.message}\n#{e.backtrace.first(5).join("\n")}"
  false
end