Class: RubyCms::ContentBlocksSync
- Inherits:
-
Object
- Object
- RubyCms::ContentBlocksSync
- 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
-
#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.
-
#import_from_yaml(locale: nil, create_missing: true, update_existing: true, published: false) ⇒ Hash
Import content blocks from YAML locale files to database.
-
#initialize(namespace: nil, locales_dir: nil) ⇒ ContentBlocksSync
constructor
A new instance of ContentBlocksSync.
-
#sync(import_after_export: false) ⇒ Hash
Sync: export database to YAML, then optionally import from YAML Useful for keeping both in sync.
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
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
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
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 |