module Padlock
Constants
- VERSION
Public Class Methods
config()
click to toggle source
# File lib/padlock.rb, line 6 def config @config ||= OpenStruct.new( table_name: "padlocks", user_foreign_key: "user_id", user_class_name: "User", timeout: 1.day ) end
lock(user, *objects)
click to toggle source
# File lib/padlock.rb, line 15 def lock(user, *objects) objects.each do |object| unlock!(object) user.padlocks.create(lockable: object) object.reload end end
locked?(object)
click to toggle source
# File lib/padlock.rb, line 23 def locked? object object.locked? end
touch(*objects)
click to toggle source
# File lib/padlock.rb, line 39 def touch *objects objects.select(&:locked?).each do |object| object.update_attribute(:updated_at, Time.zone.now) end end
unlock!(*objects)
click to toggle source
# File lib/padlock.rb, line 27 def unlock! *objects objects.each(&:unlock!) end
unlock_stale()
click to toggle source
# File lib/padlock.rb, line 35 def unlock_stale stale_locks.destroy_all end
unlocked?(object)
click to toggle source
# File lib/padlock.rb, line 31 def unlocked? object object.unlocked? end
Private Class Methods
stale_locks()
click to toggle source
# File lib/padlock.rb, line 51 def stale_locks Padlock::Instance.where("updated_at >= ?", timeout) end
timeout()
click to toggle source
# File lib/padlock.rb, line 47 def timeout Time.zone.now - config.timeout end