Make acts_as_versioned create new version on demand

I use acts_as_versioned to manage versions of Rails models. As I don’t want to create a new version of my document everytime I save it (to fix a typo for instance), I added a virtual attribute called save_with_revision to my model and put it into the definition of version_condition_met?.

attr_accessor :save_with_revision

def version_condition_met?
  @save_with_revision.to_s[/true|1/] != nil
end

Cool… except that it does not save intermediate updates into the **_versions table. Let say you create document version 1.0, then update it without creating a new version (from v1.1 to v1.4) and finally save a new version (v2.0); v1 will still be v1.0 and not v1.4.

To save intermediate updates into the **_versions table, just add to your model:

def after_update
  if !version_condition_met? && changed?
    versions.find(:last).update_attributes(self.attributes)
  end
end

Hope it helps. :)
Anyone with a better solution?

. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted October 22, 2009 at 09:00 | Permalink

    Hello from Russia!
    Can I quote a post in your blog with the link to you?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Hi, my name is Philippe Creux.

    I am a software engineer specializing in Agile methods, Behavior Driven Development and Ruby on Rails. I live in Vancouver, Canada where I work for Versapay as a Ruby on Rails programmer & BDD guy. This blog is about Agile project management, Ruby programming and other cool things.