Module: WCC::Contentful::ModelSingletonMethods

Defined in:
lib/wcc/contentful/model_singleton_methods.rb

Overview

This module is extended by all models and defines singleton methods that are not dynamically generated.

Defined Under Namespace

Classes: ModelQuery

Instance Method Summary collapse

Instance Method Details

#find(id, options: nil) ⇒ nil, WCC::Contentful::Model

Finds an instance of this content type.

Examples:

WCC::Contentful::Model::Page.find(id)

Returns:

  • (nil, WCC::Contentful::Model)

    An instance of the appropriate model class for this content type, or nil if the ID does not exist in the space.



13
14
15
16
17
18
19
20
21
22
# File 'lib/wcc/contentful/model_singleton_methods.rb', line 13

def find(id, options: nil)
  options ||= {}
  store = options[:preview] ? services.preview_store : services.store
  raw =
    _instrumentation.instrument 'find.model.contentful.wcc',
      content_type: content_type, id: id, options: options do
      store.find(id, **{ hint: type }.merge!(options.except(:preview)))
    end
  new(raw, options) if raw.present?
end

#find_all(filter = nil) ⇒ Enumerator::Lazy<WCC::Contentful::Model>, <WCC::Contentful::Model>

Finds all instances of this content type, optionally limiting to those matching a given filter query.

Examples:

WCC::Contentful::Model::Page.find_all('sys.created_at' => { lte: Date.today })

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wcc/contentful/model_singleton_methods.rb', line 31

def find_all(filter = nil)
  filter = filter&.dup
  options = filter&.delete(:options) || {}

  filter.transform_keys! { |k| k.to_s.camelize(:lower) } if filter.present?

  store = options[:preview] ? services.preview_store : services.store
  query =
    _instrumentation.instrument 'find_all.model.contentful.wcc',
      content_type: content_type, filter: filter, options: options do
      store.find_all(content_type: content_type, options: options.except(:preview))
    end
  query = query.apply(filter) if filter.present?
  ModelQuery.new(query, options, self)
end

#find_by(filter = nil) ⇒ nil, WCC::Contentful::Model

Finds the first instance of this content type matching the given query.

Examples:

WCC::Contentful::Model::Page.find_by(slug: '/some-slug')

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wcc/contentful/model_singleton_methods.rb', line 53

def find_by(filter = nil)
  filter = filter&.dup
  options = filter&.delete(:options) || {}

  filter.transform_keys! { |k| k.to_s.camelize(:lower) } if filter.present?

  store = options[:preview] ? services.preview_store : services.store
  result =
    _instrumentation.instrument 'find_by.model.contentful.wcc',
      content_type: content_type, filter: filter, options: options do
      store.find_by(content_type: content_type, filter: filter, options: options.except(:preview))
    end

  new(result, options) if result
end

#inherited(subclass) ⇒ Object

rubocop:disable Lint/MissingSuper



69
70
71
72
73
74
75
# File 'lib/wcc/contentful/model_singleton_methods.rb', line 69

def inherited(subclass) # rubocop:disable Lint/MissingSuper
  # If another different class is already registered for this content type,
  # don't auto-register this one.
  return if model_namespace.registered?(content_type)

  model_namespace.register_for_content_type(content_type, klass: subclass)
end