Class: RubyCms::ContentBlocksSync

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_cms/content_blocks_sync.rb

Overview

Service class for syncing content blocks between database and YAML locale files

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(namespace: nil, locales_dir: nil) ⇒ ContentBlocksSync

Returns a new instance of ContentBlocksSync.



10
11
12
13
# File 'lib/ruby_cms/content_blocks_sync.rb', line 10

def initialize(namespace: nil, locales_dir: nil)
  @namespace = namespace || default_namespace_from_config
  @locales_dir = locales_dir || Rails.root.join("config/locales")
end

Instance Method Details

#export_to_yaml(only_published: true, flatten_keys: false) ⇒ Hash

Export all published content blocks to YAML files Creates/updates locale files for each locale configured in I18n.available_locales

Parameters:

  • (defaults to: true)

    If true, only export published blocks

  • (defaults to: false)

    If true, flatten dot-separated keys into nested structure

Returns:

  • Summary of exported blocks per locale



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_cms/content_blocks_sync.rb', line 20

def export_to_yaml(only_published: true, flatten_keys: false)
  scope = only_published ? RubyCms::ContentBlock.published : RubyCms::ContentBlock.all
  blocks_by_locale = scope.order(:key).group_by(&:locale)

  summary = {}
  I18n.available_locales.each do |locale|
    summary[locale] = export_locale_to_yaml(
      locale, blocks_by_locale, flatten_keys:
    )
  end

  summary
end

#import_from_yaml(locale: nil, create_missing: true, update_existing: true, published: false) ⇒ Hash

Import content blocks from YAML locale files to database

Parameters:

  • (defaults to: nil)

    Specific locale to import, or nil for all locales

  • (defaults to: true)

    Create content blocks that don’t exist in DB

  • (defaults to: true)

    Update existing content blocks

  • (defaults to: false)

    Set published status for imported blocks

Returns:

  • Summary of imported/updated blocks



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_cms/content_blocks_sync.rb', line 40

def import_from_yaml(locale: nil, create_missing: true, update_existing: true, published: false)
  locales_to_process = locale ? [locale.to_sym] : I18n.available_locales
  summary = { created: 0, updated: 0, skipped: 0, errors: [] }

  locales_to_process.each do |loc|
    import_locale_from_yaml(
      loc,
      summary,
      create_missing:,
      update_existing:,
      published:
    )
  end

  summary
end

#sync(import_after_export: false) ⇒ Hash

Sync: export database to YAML, then optionally import from YAML Useful for keeping both in sync

Parameters:

  • (defaults to: false)

    Import from YAML after exporting

Returns:

  • Summary of operations



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby_cms/content_blocks_sync.rb', line 61

def sync(import_after_export: false)
  result = { export: {}, import: {} }

  # Export database to YAML
  result[:export] = export_to_yaml

  # Optionally import from YAML (useful for seeding from YAML)
  result[:import] = import_from_yaml if import_after_export

  result
end