Class: Collab::Models::Document
- Defined in:
- lib/collab/models/document.rb
Instance Method Summary collapse
- #apply_commit(data) ⇒ Object
- #as_json ⇒ Object
- #content_will_change! ⇒ Object
- #from_html(html) ⇒ Object
- #oldest_saved_commit_version ⇒ Object
- #possibly_saved_version?(version) ⇒ Boolean
- #resolve_positions(*positions, **kwargs, &block) ⇒ Object (also: #resolve_position)
- #resolve_selection(anchor_pos, head_pos, version:, &block) ⇒ Object
-
#to_html ⇒ Object
Serialize the document to html, uses cached if possible.
Instance Method Details
#apply_commit(data) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/collab/models/document.rb', line 31 def apply_commit(data) base_version = data["v"]&.to_i steps = data["steps"] return false unless base_version return false unless steps.is_a?(Array) && !steps.empty? self.with_lock do return false unless self.possibly_saved_version? base_version self.document_version += 1 original_positions = self.tracked_positions.current.distinct.pluck(:pos, :assoc).map { |(pos, assoc)| {pos: pos, assoc: assoc} } result = ::Collab::JS.apply_commit content, {steps: steps}, map_steps_through: self.commits.where("document_version > ?", base_version).steps, pos: original_positions, schema_name: schema_name self.content = result["doc"] commits.create!({ steps: result["steps"], ref: data["ref"], document_version: self.document_version }) self.save! original_positions.lazy.zip(result["pos"]) do |original, res| if res["deleted"] tracked_positions.current.where(original).update_all deleted_at_version: self.document_version elsif original[:pos] != res["pos"] tracked_positions.current.where(original).update_all pos: res["pos"] end end end end |
#as_json ⇒ Object
76 77 78 |
# File 'lib/collab/models/document.rb', line 76 def as_json {id: id, content: content, version: document_version} end |
#content_will_change! ⇒ Object
80 81 82 83 |
# File 'lib/collab/models/document.rb', line 80 def content_will_change! super self.serialized_html = nil end |
#from_html(html) ⇒ Object
72 73 74 |
# File 'lib/collab/models/document.rb', line 72 def from_html(html) self.content = ::Collab::JS.html_to_document(html, schema_name: schema_name) end |
#oldest_saved_commit_version ⇒ Object
89 90 91 92 |
# File 'lib/collab/models/document.rb', line 89 def oldest_saved_commit_version v = document_version - ::Collab.config.max_commit_history_length v > 0 ? v : 0 end |
#possibly_saved_version?(version) ⇒ Boolean
85 86 87 |
# File 'lib/collab/models/document.rb', line 85 def possibly_saved_version?(version) self.document_version >= version && self.oldest_saved_commit_version <= version end |
#resolve_positions(*positions, **kwargs, &block) ⇒ Object Also known as: resolve_position
94 95 96 |
# File 'lib/collab/models/document.rb', line 94 def resolve_positions(*positions, **kwargs, &block) ::Collab.config.tracked_position_model.constantize.resolve(self, *positions, **kwargs, &block) end |
#resolve_selection(anchor_pos, head_pos, version:, &block) ⇒ Object
99 100 101 |
# File 'lib/collab/models/document.rb', line 99 def resolve_selection(anchor_pos, head_pos, version:, &block) ::Collab::DocumentSelection.resolve(self, anchor_pos, head_pos, version: version, &block) end |
#to_html ⇒ Object
Serialize the document to html, uses cached if possible. Note that this may lock the document
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/collab/models/document.rb', line 18 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 |