lib/wcc/contentful.rb in wcc-contentful-1.3.2 vs lib/wcc/contentful.rb in wcc-contentful-1.4.0.rc1

- old
+ new

@@ -8,10 +8,11 @@ require 'wcc/contentful/active_record_shim' require 'wcc/contentful/configuration' require 'wcc/contentful/downloads_schema' require 'wcc/contentful/exceptions' require 'wcc/contentful/helpers' +require 'wcc/contentful/entry_locale_transformer' require 'wcc/contentful/link_visitor' require 'wcc/contentful/services' require 'wcc/contentful/simple_client' require 'wcc/contentful/store' require 'wcc/contentful/content_type_indexer' @@ -38,13 +39,12 @@ ActiveSupport::Deprecation.warn('Use WCC::Contentful::Model.schema instead') WCC::Contentful::Model.schema end # Gets all queryable locales. - # Reserved for future use. def locales - @locales ||= { 'en-US' => {} }.freeze + configuration&.locale_fallbacks end def logger ActiveSupport::Deprecation.warn('Use WCC::Contentful::Services.instance.logger instead') WCC::Contentful::Services.instance.logger @@ -91,19 +91,22 @@ Services.instance.logger.warn("Unable to download schema from management API - #{e.message}") end end - content_types = + schema = begin - JSON.parse(File.read(configuration.schema_file))['contentTypes'] if File.exist?(configuration.schema_file) + JSON.parse(File.read(configuration.schema_file)) if File.exist?(configuration.schema_file) rescue JSON::ParserError Services.instance.warn("Schema file invalid, ignoring it: #{configuration.schema_file}") nil end - if !content_types && %i[if_possible never].include?(configuration.update_schema_file) + content_types = schema['contentTypes'] if schema + locales = schema['locales'] if schema + + if !schema && %i[if_possible never].include?(configuration.update_schema_file) # Final fallback - try to grab content types from CDN. We can't update the file # because the CDN doesn't have all the field validation info, but we can at least # build the WCC::Contentful::Model instances. client = Services.instance.management_client || Services.instance.client @@ -125,10 +128,18 @@ configuration, schema: WCC::Contentful::ContentTypeIndexer.from_json_schema(content_types).types, services: WCC::Contentful::Services.instance ) - # Drop an initial sync + # Update the locale fallbacks from the schema file, unless they have already + # been configured. + locales&.each do |locale_hash| + next if @configuration.locale_fallbacks[locale_hash['code']] + + @configuration.locale_fallbacks[locale_hash['code']] = locale_hash['fallbackCode'] + end + + # Enqueue an initial sync WCC::Contentful::SyncEngine::Job.perform_later if defined?(WCC::Contentful::SyncEngine::Job) @configuration = @configuration.freeze @initialized = true end