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
-
#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
- #store(preview = false) ⇒ Object
Instance Method Details
#find(id, options: nil) ⇒ nil, WCC::Contentful::Model
Finds an instance of this content type.
25 26 27 28 29 30 |
# File 'lib/wcc/contentful/model_singleton_methods.rb', line 25 def find(id, options: nil) ||= {} raw = store([:preview]) .find(id, { hint: type }.merge!(.except(:preview))) 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.
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 = 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([:preview]) .find_all(content_type: content_type, options: .except(:preview)) query = query.apply(filter) if filter.present? query.map { |r| new(r, ) } end |
#find_by(filter = nil) ⇒ nil, WCC::Contentful::Model
Finds the first instance of this content type matching the given query.
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 = 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([:preview]) .find_by(content_type: content_type, filter: filter, options: .except(:preview)) new(result, ) 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 |