#544 open
Iain

Support for optimistic locking in update controller action

Reported by Iain | November 18th, 2009 @ 10:10 PM | in Beyond Hobo 1.0

Currently hobo doesn't seem to support activerecord's optimistic locking system (ie. the lock_version column on models). However, it should be very easy to support by changing two small things:

  1. The form tag should include in the hidden-fields element the lock_version attribute if it is present and the model being edited is not a new record
  2. The hobo_update method should be changed so that if a ActiveRecord::StaleObjectError is raised then it reloads the current model from the database and redirects back to the edit page (updating the flash appropriately)

This way all that the user would have to do to get optimistic locking is to add the lock_version column to their models.

I haven't submitted this as a patch to the repository, but here's what I've done in my own code (note that I've worked around the existing system rather than trying to patch it up, but obviously it would be better to implement this as I've described above)

<extend tag="form">
  <old-form merge >
    <before-field-list:>
      <input type="hidden" name="#{param_name_for_this}[lock_version]" value="&this.lock_version" unless="&this.new_record?"/>
    </before-field-list:>
  </old-form>
</extend>
  def update
    hobo_update
  rescue ActiveRecord::StaleObjectError
    flash_notice "This #{model.view_hints.model_name} was changed by someone else while you were editing it. Please try again."
    respond_to do |wants|
      wants.html do
        self.this = find_instance
        re_render_form(:edit)
      end
      wants.js do
        render(:status => 500, :text => ("There was a problem with that change.\n" + @this.errors.full_messages.join("\n")))
      end
    end
  end

I'm not sure if this would be worth doing before 1.0 but I thought I'd raise it (as I think it'd be a very powerful feature)

Comments and changes to this ticket

  • Bryan Larsen

    Bryan Larsen November 18th, 2009 @ 11:01 PM

    #2 isn't always the right thing to do, but it is often enough that I'd be comfortable with something like this.

  • Matt Jones

    Matt Jones November 27th, 2009 @ 07:27 PM

    Saw the tutorial on the cookbook - pretty neat. Would it be helpful to have a helper to declare the field / set never_show, etc? On the DRYML side, it should be straightforward to grab the column name from AR's locking_column method and put it in hidden-fields automatically.

  • Bryan Larsen

    Bryan Larsen December 1st, 2009 @ 11:42 PM

    • State changed from “new” to “open”
    • Milestone set to Beyond Hobo 1.0

    I think if we had one more day, this would have made it into RC1. Sorry. Especially since you did all the grunt work.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

People watching this ticket

Pages