Class: Collab::Models::Commit

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_json(data) ⇒ Object



11
12
13
# File 'lib/collab/models/commit.rb', line 11

def self.from_json(data)
  new(document_version: data["v"]&.to_i, steps: data["steps"], ref: data["ref"])
end

Instance Method Details

#apply!Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/collab/models/commit.rb', line 31

def apply!
  raise "cannot apply persisted commit" if self.persisted?
  raise "commit not valid" unless self.valid?
  return false if self.document.document_version != self.document_version # optimization, prevents need to apply lock if outdated

  self.document.with_lock do
    return false if self.document.document_version != self.document_version
    
    return false unless result = ::Collab::JS.apply_commit(self.document, self.to_json, schema_name: self.document.schema_name)
    
    self.document.document = result["doc"]
    self.document.document_version = self.document_version
    
    self.document.save!
    self.save!
  end
end

#apply_laterObject



23
24
25
26
27
28
29
# File 'lib/collab/models/commit.rb', line 23

def apply_later
  raise "cannot apply persisted commit" if self.persisted?
  raise "commit not valid" unless self.valid?
  return false if self.document.document_version != self.document_version

  ::Collab.config.commit_job.constantize.perform_later(self.document, as_json)
end

#as_jsonObject



15
16
17
18
19
20
21
# File 'lib/collab/models/commit.rb', line 15

def as_json
  {
    v: document_version,
    steps: steps,
    ref: ref
  }
end

#broadcastObject



49
50
51
# File 'lib/collab/models/commit.rb', line 49

def broadcast
  ::Collab.config.channel_name.constantize.broadcast_to(document, as_json)
end