Sha256: eb96a71c53e89e61081d267705c737020f7b69ba8f87a46f5b22dc1519ef8915

Contents?: true

Size: 1.63 KB

Versions: 9

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require 'wcc/contentful'

namespace :wcc_contentful do
  desc 'Rewrites JSON fixtures from locale=* to locale=en-US'
  task :rewrite_fixtures, %i[glob locale] => :environment do |_t, args|
    glob = args[:glob] || 'spec/fixtures/**/*.json'
    locale = args[:locale] || 'en-US'

    Dir.glob(glob) do |filename|
      next unless File.file?(filename)

      contents = JSON.parse(File.read(filename))
      next unless contents.is_a?(Hash) && contents['sys']

      rewritten_contents =
        case contents['sys']['type']
        when 'Array'
          rewrite_array(contents, locale: locale)
        when 'Entry', 'Asset'
          rewrite_entry(contents, locale: locale)
        end
      next unless rewritten_contents

      File.write(filename, JSON.pretty_generate(rewritten_contents))
    end
  end

  def rewrite_array(contents, locale: 'en-US')
    contents['items'] =
      contents['items'].map do |item|
        rewrite_entry(item, locale: locale)
      end
    if contents['includes']
      if contents['includes']['Entry']
        contents['includes']['Entry'] =
          contents['includes']['Entry'].map do |item|
            rewrite_entry(item, locale: locale)
          end
      end
      if contents['includes']['Asset']
        contents['includes']['Asset'] =
          contents['includes']['Asset'].map do |item|
            rewrite_entry(item, locale: locale)
          end
      end
    end

    contents
  end

  def rewrite_entry(contents, locale: 'en-US')
    return contents unless contents['sys']

    WCC::Contentful::EntryLocaleTransformer.transform_to_locale(
      contents,
      locale
    )
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
wcc-contentful-1.7.2 lib/tasks/rewrite_fixtures.rake
wcc-contentful-1.7.1 lib/tasks/rewrite_fixtures.rake
wcc-contentful-1.7.0 lib/tasks/rewrite_fixtures.rake
wcc-contentful-1.6.2 lib/tasks/rewrite_fixtures.rake
wcc-contentful-1.6.1 lib/tasks/rewrite_fixtures.rake
wcc-contentful-1.6.0 lib/tasks/rewrite_fixtures.rake
wcc-contentful-1.5.1 lib/tasks/rewrite_fixtures.rake
wcc-contentful-1.5.0 lib/tasks/rewrite_fixtures.rake
wcc-contentful-1.5.0.rc1 lib/tasks/rewrite_fixtures.rake