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
-
#find(id, options: nil) ⇒ nil, WCC::Contentful::Model
Finds an instance of this content type.
-
#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.
-
#find_by(filter = nil) ⇒ nil, WCC::Contentful::Model
Finds the first instance of this content type matching the given query.
-
#inherited(subclass) ⇒ Object
rubocop:disable Lint/MissingSuper.
Instance Method Details
#find(id, options: nil) ⇒ nil, WCC::Contentful::Model
Finds an instance of this content type.
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) ||= {} store = [:preview] ? services.preview_store : services.store raw = _instrumentation.instrument 'find.model.contentful.wcc', content_type: content_type, id: id, options: do store.find(id, **{ hint: type }.merge!(.except(:preview))) end new(raw, ) 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.
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 = filter&.delete(:options) || {} filter.transform_keys! { |k| k.to_s.camelize(:lower) } if filter.present? store = [:preview] ? services.preview_store : services.store query = _instrumentation.instrument 'find_all.model.contentful.wcc', content_type: content_type, filter: filter, options: do store.find_all(content_type: content_type, options: .except(:preview)) end query = query.apply(filter) if filter.present? ModelQuery.new(query, , self) end |
#find_by(filter = nil) ⇒ nil, WCC::Contentful::Model
Finds the first instance of this content type matching the given query.
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 = filter&.delete(:options) || {} filter.transform_keys! { |k| k.to_s.camelize(:lower) } if filter.present? store = [:preview] ? services.preview_store : services.store result = _instrumentation.instrument 'find_by.model.contentful.wcc', content_type: content_type, filter: filter, options: do store.find_by(content_type: content_type, filter: filter, options: .except(:preview)) end new(result, ) 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 |