Class: Pageflow::EditLock

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/pageflow/edit_lock.rb

Defined Under Namespace

Classes: Error, HeldByOtherSessionError, HeldByOtherUserError, NotHeldError, Null

Constant Summary collapse

TIME_TO_LIVE =
10.seconds

Instance Method Summary collapse

Instance Method Details

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


39
40
41
42
43
44
45
46
47
# File 'app/models/pageflow/edit_lock.rb', line 39

def aquire(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)

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

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

#matches?(id) ⇒ Boolean

Returns:

  • (Boolean)

31
32
33
# File 'app/models/pageflow/edit_lock.rb', line 31

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

#release(current_user) ⇒ Object


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

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

#timed_out?Boolean

Returns:

  • (Boolean)

35
36
37
# File 'app/models/pageflow/edit_lock.rb', line 35

def timed_out?
  Time.now > self.updated_at + TIME_TO_LIVE
end

#verify!(current_user, options) ⇒ Object


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

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