CheckOut
Let users check out
Active Record instances and then release them when they're done editing.
Using include CheckOut
in a model's class definition provides the following features:
- Scopes:
checked_out_to(user)
checked_out
-
released
(inverse ofchecked_out
) available_to(user)
- Collection / scope methods:
check_out_all(user)
-
release_all_checkouts(user)
(user
is optional)
- Instance methods:
checked_out?
checked_out_to?(user)
check_out(user)
-
release(user)
(user
is optional) update_attributes_and_release(attributes)
The model must have the following columns:
-
checked_out_by_user_id
(integer) -
checked_out_by_user_type
(string) -
checked_out_at
(datetime)
Requires Rails ~> 3 and Ruby 1.9.2.
Usage
create_table "jobs" do |t| t.integer :checked_out_by_user_id t.string :checked_out_by_user_type t.datetime :checked_out_at end
class Job < ActiveRecord::Base include CheckOut end
in 'edit' action
job.checkout(user) # => job.checked_out_at = Time.now, etc.
in 'update' action
job = Job.available_to(user).find(params[:id]) job.update_attributes_and_release(params[:job])
The challenging thing is knowing when to release the record if the user decides not to make any changes (i.e. cancels) or just closes their browser.
I've found that by including Job.release_all_checkouts(current_user)
in the 'index' action, I can avoid almost all conflicts. I also add an "unlock" action for manually overriding an existing checkout. A link to 'unlock' appears in a flash message if a user tries to edit a record that is already checked out.