lib/wcc/contentful/entry_locale_transformer.rb in wcc-contentful-1.4.0.rc3 vs lib/wcc/contentful/entry_locale_transformer.rb in wcc-contentful-1.4.0

- old
+ new

@@ -20,24 +20,25 @@ unless entry_locale = entry.dig('sys', 'locale') # nothing to do return entry end - sys = entry['sys'].except('locale').merge({ + new_entry = entry.dup + + new_entry['sys'] = entry['sys'].except('locale').merge({ 'WCC::Contentful::EntryLocaleTransformer:locales_included' => [entry_locale] }) - fields = - entry['fields']&.transform_values do |value| - h = {} - h[entry_locale] = value - h - end + if entry.key?('fields') + new_entry['fields'] = + entry['fields'].transform_values do |value| + h = {} + h[entry_locale] = value + h + end + end - { - 'sys' => sys, - 'fields' => fields - } + new_entry end ## # Takes an entry in the 'locale=*' format and transforms it to a specific locale def transform_to_locale(entry, locale) @@ -52,32 +53,32 @@ end # Transform the store's "locale=*" entry into a localized one locale ||= default_locale - sys = entry['sys'].deep_dup - sys['locale'] = locale - fields = - entry['fields']&.transform_values do |value| - next if value.nil? + new_entry = entry.dup + new_entry['sys'] = entry['sys'].deep_dup + new_entry['sys']['locale'] = locale + if entry.key?('fields') + new_entry['fields'] = + entry['fields']&.transform_values do |value| + next if value.nil? - # replace the all-locales value with the localized value - l = locale - v = nil - while l - v = value[l] - break if v + # replace the all-locales value with the localized value + l = locale + v = nil + while l + v = value[l] + break if v - l = configuration.locale_fallbacks[l] + l = configuration.locale_fallbacks[l] + end + + v end + end - v - end - - { - 'sys' => sys, - 'fields' => fields - } + new_entry end ## # Takes an entry in a specific 'sys.locale' and merges it into an entry that is # in the 'locale=*' format