Class: Pageflow::EditLock

Inherits:
ApplicationRecord show all
Defined in:
app/models/pageflow/edit_lock.rb

Defined Under Namespace

Classes: Error, HeldByOtherSessionError, HeldByOtherUserError, NotHeldError, Null

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.time_to_liveObject



28
29
30
31
# File 'app/models/pageflow/edit_lock.rb', line 28

def self.time_to_live
  timer_tolerance = 2.3
  Pageflow.config.edit_lock_polling_interval * timer_tolerance
end

Instance Method Details

#acquire(current_user, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'app/models/pageflow/edit_lock.rb', line 45

def acquire(current_user, options = {})
  verify!(current_user, options)
rescue Error
  if options[:force] || timed_out?
    entry.create_edit_lock(:user => current_user)
  else
    raise
  end
end

#held_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/pageflow/edit_lock.rb', line 33

def held_by?(user)
  self.user == user
end

#matches?(id) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/pageflow/edit_lock.rb', line 37

def matches?(id)
  self.id == id.to_i
end

#release(current_user) ⇒ Object



55
56
57
58
59
# File 'app/models/pageflow/edit_lock.rb', line 55

def release(current_user)
  if user == current_user
    destroy
  end
end

#timed_out?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/pageflow/edit_lock.rb', line 41

def timed_out?
  Time.now > updated_at + EditLock.time_to_live
end

#verify!(current_user, options) ⇒ Object



61
62
63
64
65
# File 'app/models/pageflow/edit_lock.rb', line 61

def verify!(current_user, options)
  raise HeldByOtherUserError.new(user) unless held_by?(current_user)
  raise HeldByOtherSessionError unless matches?(options[:id])
  touch
end