Class: Collab::Models::Document

Inherits:
Base
  • Object
show all
Defined in:
lib/collab/models/document.rb

Instance Method Summary collapse

Instance Method Details

#as_jsonObject



35
36
37
# File 'lib/collab/models/document.rb', line 35

def as_json
  {id: id, content: content, version: document_version}
end

#commit_later(data) ⇒ Object



31
32
33
# File 'lib/collab/models/document.rb', line 31

def commit_later(data)
  commits.from_json(data).apply_later
end

#content_will_change!Object



39
40
41
42
# File 'lib/collab/models/document.rb', line 39

def content_will_change!
  super
  self.serialized_html = nil
end

#from_html(html) ⇒ Object



27
28
29
# File 'lib/collab/models/document.rb', line 27

def from_html(html)
  self.content = ::Collab::JS.html_to_document(html, schema_name: schema_name)
end

#to_htmlObject

Serialize the document to html, uses cached if possible. Note that this may lock the document



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/collab/models/document.rb', line 14

def to_html
  return serialized_html if serialized_html

  serialized_version = self.document_version
  ::Collab::JS.document_to_html(self.content, schema_name: schema_name).tap do |serialized_html|
    Thread.new do # use a thread to prevent deadlocks and avoid incuring the cost of an inline-write
      self.with_lock do
        self.update_attribute(:serialized_html, serialized_html) if serialized_version == self.version and self.serialized_html.nil?
      end
    end
  end
end