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.

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.



25
26
27
28
29
30
# File 'lib/wcc/contentful/model_singleton_methods.rb', line 25

def find(id, options: nil)
  options ||= {}
  raw = store(options[:preview])
    .find(id, { hint: type }.merge!(options.except(:preview)))
  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:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wcc/contentful/model_singleton_methods.rb', line 39

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

  if filter.present?
    filter.transform_keys! { |k| k.to_s.camelize(:lower) }
    bad_fields = filter.keys.reject { |k| self::FIELDS.include?(k) }
    raise ArgumentError, "These fields do not exist: #{bad_fields}" unless bad_fields.empty?
  end

  query = store(options[:preview])
    .find_all(content_type: content_type, options: options.except(:preview))
  query = query.apply(filter) if filter.present?
  query.map { |r| new(r, options) }
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:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wcc/contentful/model_singleton_methods.rb', line 61

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

  if filter.present?
    filter.transform_keys! { |k| k.to_s.camelize(:lower) }
    bad_fields = filter.keys.reject { |k| self::FIELDS.include?(k) }
    raise ArgumentError, "These fields do not exist: #{bad_fields}" unless bad_fields.empty?
  end

  result = store(options[:preview])
    .find_by(content_type: content_type, filter: filter, options: options.except(:preview))

  new(result, options) if result
end

#inherited(subclass) ⇒ Object



77
78
79
80
81
82
# File 'lib/wcc/contentful/model_singleton_methods.rb', line 77

def inherited(subclass)
  # only register if it's not already registered
  return if WCC::Contentful::Model.registered?(content_type)

  WCC::Contentful::Model.register_for_content_type(content_type, klass: subclass)
end

#store(preview = false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/wcc/contentful/model_singleton_methods.rb', line 7

def store(preview = false)
  if preview
    if WCC::Contentful::Model.preview_store.nil?
      raise ArgumentError,
        'You must include a contentful preview token in your WCC::Contentful.configure block'
    end
    WCC::Contentful::Model.preview_store
  else
    WCC::Contentful::Model.store
  end
end