I have an application in which I need to prevent users from editing data while it is being edited by another user. I am trying to think about how to do this and would like to ask for ideas. So far, I have created a settings model that stores the general configuration of the application on db in key / value pairs. So, for locking, I have an instance of settings that called LOCKED_TABLE_UID, and it saved the user_id of the user editing the table, or null (nil) if the table is free.
>> lock = Setting.find_by_key('LOCKED_TABLE_UID')
Then I implemented 2 methods in my application controller to get and release the lock:
# current_user returns the user currently logged in def acquire_lock lock = Setting.find_by_key("LOCKED_TABLE_UID") if lock.value # if lock taken, see if it the current_user or someone else if lock.value.to_i == current_user.id.to_i return true else return false end else # lock is free, assign it to this user lock.value = current_user.id return true if lock.save end end def release_lock lock = Setting.find_by_key("LOCKED_TABLE_UID") if lock.value # the lock belongs to current_user, so he can release it if lock.value.to_i == current_user.id.to_i lock.value = nil return true if lock.save else # not your lock, go away return false end else # lock is free, quit bugging return true end end
I want to create some kind of block code containing a locking mechanism, something like this:
def some_crud_action requires_locking do |lock| if lock # do some CRUD stuff here else # decline CRUD and give some error end end end
I would appreciate help in this, but I am also open to other suggestions on how to do this, or about some things that I may have forgotten. This lock does not have to be atomic, but rather simple and most important - that it works :) thanks.
sa125 source share