Class: PrismicRails::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/prismic_rails/content/document.rb

Overview

The PrismicRails::Document is a wrapper class around Prismic::Document to support custom features like to_html, to_text and to handle nil documents.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Document

Creates a new PrismicRails::Document

+document+ A Prismic::Document

11
12
13
# File 'lib/prismic_rails/content/document.rb', line 11

def initialize(document)
  @document = document || PrismicRails::NilDocument.new
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.


6
7
8
# File 'lib/prismic_rails/content/document.rb', line 6

def document
  @document
end

Instance Method Details

#as_html(serializer = nil) ⇒ Object

Returns the document as safe html


16
17
18
# File 'lib/prismic_rails/content/document.rb', line 16

def as_html(serializer = nil)
  @document.as_html(serializer)
end

#as_textObject

Returns only the text of a document


21
22
23
# File 'lib/prismic_rails/content/document.rb', line 21

def as_text
  @document.as_text
end

#find_fragment(type) ⇒ Object

Finds a fragment of a specific type in a document

+type+ 'text', 'image' etc

28
29
30
31
32
33
34
35
# File 'lib/prismic_rails/content/document.rb', line 28

def find_fragment(type)
  fragment = @document.fragments[type]
  if fragment
    PrismicRails::Fragment.new(fragment)
  else
    NilDocument.new
  end
end

#is_type?(type) ⇒ Boolean

Tests if the document has the type type.

+type+ 'text', 'image' etc

Returns:

  • (Boolean)

40
41
42
# File 'lib/prismic_rails/content/document.rb', line 40

def is_type? type
  type == @document.type
end