lib/wcc/contentful/model_methods.rb in wcc-contentful-1.0.8 vs lib/wcc/contentful/model_methods.rb in wcc-contentful-1.1.0

- old
+ new

@@ -3,12 +3,10 @@ # This module is included by all {WCC::Contentful::Model models} and defines instance # methods that are not dynamically generated. # # @api Model module WCC::Contentful::ModelMethods - include WCC::Contentful::Instrumentation - # The set of options keys that are specific to the Model layer and shouldn't # be passed down to the Store layer. MODEL_LAYER_CONTEXT_KEYS = %i[ preview backlinks @@ -38,10 +36,11 @@ fields = fields.map { |f| f.to_s.camelize(:lower) } if fields.present? fields ||= self.class::FIELDS typedef = self.class.content_type_definition links = fields.select { |f| %i[Asset Link].include?(typedef.fields[f].type) } + store = context[:preview] ? self.class.services.preview_store : self.class.services.store raw_link_ids = links.map { |field_name| raw.dig('fields', field_name, sys.locale) } .flat_map do |raw_value| _try_map(raw_value) { |v| v.dig('sys', 'id') if v.dig('sys', 'type') == 'Link' } @@ -52,16 +51,15 @@ has_unresolved_raw_links = (raw_link_ids - backlinked_ids).any? if has_unresolved_raw_links raw = _instrument 'resolve', id: id, depth: depth, backlinks: backlinked_ids do # use include param to do resolution - self.class.store(context[:preview]) - .find_by(content_type: self.class.content_type, - filter: { 'sys.id' => id }, - options: context.except(*MODEL_LAYER_CONTEXT_KEYS).merge!({ - include: [depth, 10].min - })) + store.find_by(content_type: self.class.content_type, + filter: { 'sys.id' => id }, + options: context.except(*MODEL_LAYER_CONTEXT_KEYS).merge!({ + include: [depth, 10].min + })) end unless raw raise WCC::Contentful::ResolveError, "Cannot find #{self.class.content_type} with ID #{id}" end @@ -168,13 +166,13 @@ # instantiate from already resolved raw entry data. m = already_resolved || if raw.dig('sys', 'type') == 'Link' _instrument 'resolve', id: self.id, depth: depth, backlinks: context[:backlinks]&.map(&:id) do - WCC::Contentful::Model.find(id, options: new_context) + self.class.model_namespace.find(id, options: new_context) end else - WCC::Contentful::Model.new_from_raw(raw, new_context) + self.class.model_namespace.new_from_raw(raw, new_context) end m.resolve(depth: depth - 1, context: new_context, **options) if m && depth > 1 m }