class PrismicRails::Result

The PrismicRails::Result object is a wrapper object around the Prismic::Response. The purpose of this wrapper object is to have an easy access on the included documents or to query a specific fragment.

Attributes

documents[RW]

Public Class Methods

new(response) click to toggle source

Creates the PrismicRails::Result object with the initial documents.

Attributes

+response+ The response of the Prismic API query
# File lib/prismic_rails/content/result.rb, line 15
def initialize(response)
  if response && response.results && !response.results.empty?
    @documents = response.results.map do |document|
      PrismicRails::Document.new(document)
    end
  else #Handles the case if the response is nil or empty
    nil_document = PrismicRails::NilDocument.new
    @documents = [nil_document]
  end
end

Public Instance Methods

find_fragment(type) click to toggle source

Find a specific fragment in the list of all document.

Examples

PrismicRails::Result.find_fragment('image')

This call walks through all the document and looks for a fragment with the type 'image'.

# File lib/prismic_rails/content/result.rb, line 33
def find_fragment(type)
  @documents.each do |document|
    return document.find_fragment(type)
  end
end
first() click to toggle source
# File lib/prismic_rails/content/result.rb, line 39
def first
  @documents.first
end
last() click to toggle source
# File lib/prismic_rails/content/result.rb, line 43
def last
  @documents.last
end