Module: PrismicRails::QueryService

Defined in:
lib/prismic_rails/services/query_service.rb

Overview

The PrismicRails::QueryService is a small helper to query the Prismic API with support for query types. It also helps to create the right query for your i18n support.

Class Method Summary collapse

Class Method Details

.by_type(type, options = {}) ⇒ Object

Query the Prismic API by type

Examples

PrismicRails::QueryService.type('blog-post')

This returns a Prismic::Response with all the (published) documents of the type ‘blog-post’

PrismicRails::QueryService.type('blog-post', {lang: 'en'}

This gets all the (published) documents of the type ‘blog-post’ in english as a Prismic::Response and wraps it around with a PrismicRails::Result



21
22
23
24
25
26
27
28
29
# File 'lib/prismic_rails/services/query_service.rb', line 21

def by_type(type, options = {})
  type_query = {'document.type': type}
  if options[:q]
    options[:q].merge(type_query)
  else
    options[:q] = type_query
  end
  query(options)
end

.query(options = {}) ⇒ Object

Query the Prismic API



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/prismic_rails/services/query_service.rb', line 32

def query(options = {})
  match_language(options) if options[:lang]
  predicates = []
  if q = options.delete(:q)
    q.each do |key, value|
      predicates << Prismic::Predicates.at(key, value)
    end
  end
  response = api.query(predicates, options)
  PrismicRails::Result.new(response)
end