module WCC::Contentful::ModelSingletonMethods
This module is extended by all models and defines singleton methods that are not dynamically generated. @api Model
Public Instance Methods
find(id, options: nil)
click to toggle source
Finds an instance of this content type.
@return [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.
@example
WCC::Contentful::Model::Page.find(id)
# File lib/wcc/contentful/model_singleton_methods.rb, line 13 def find(id, options: nil) options ||= {} store = store(options[:preview]) raw = WCC::Contentful::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)
click to toggle source
Finds all instances of this content type, optionally limiting to those matching a given filter query.
@return [Enumerator::Lazy<WCC::Contentful::Model>, <WCC::Contentful::Model>]
A set of instantiated model objects matching the given query.
@example
WCC::Contentful::Model::Page.find_all('sys.created_at' => { lte: Date.today })
# 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 = store(options[:preview]) query = WCC::Contentful::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)
click to toggle source
Finds the first instance of this content type matching the given query.
@return [nil, WCC::Contentful::Model] A set of instantiated model objects matching
the given query.
@example
WCC::Contentful::Model::Page.find_by(slug: '/some-slug')
# 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 = store(options[:preview]) result = WCC::Contentful::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)
click to toggle source
# File lib/wcc/contentful/model_singleton_methods.rb, line 69 def inherited(subclass) # If another different class is already registered for this content type, # don't auto-register this one. return if WCC::Contentful::Model.registered?(content_type) WCC::Contentful::Model.register_for_content_type(content_type, klass: subclass) end