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?.

1
2
3
4
5
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:

1
2
3
4
5
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?

Comments