Model Versioning Plugin?

Hi Everyone,

I am attempting to version my model, so I can go back through time, as
well as offer an audit trail.

I have attempted to use acts_as_versioned (http://wiki.rubyonrails.org/
rails/pages/ActsAsVersioned), but the issue with this is the
associated models, which it does not support.

Here is an example of what my model looks like:

company has_many phone_numbers
company has_many contacts
company has_one status
.... and so on

I need to be able to have the plugin version each associated model
automatically.

Does anyone know of a solution for this?

Thanks for your help!

Lundie wrote:

Hi Everyone,

company has_many phone_numbers
company has_many contacts
company has_one status
.... and so on

I need to be able to have the plugin version each associated model
automatically.

Does anyone know of a solution for this?

I haven't heard of anything like that.

But I can't imagine you can adapt ActsAsVersioned to your needs,
because it works with a different table, so associations are going to
break.

Perhaps a more bespoke solution;

add_column :companies, :versioned_at, :datetime, :default => nil
add_column :companies, :version_of, :integer, :default => nil

Now when we "version" it, we go.

class Company

  has_many :versions, :class_name => "Company", :foreign_key =>
"version_id", :order => "companies.versioned_at DESC"
  belongs_to :version_of, :class_name => "Company"

  def version!
    transaction do
      versioned = Company.new(self.attributes.merge(:versioned_at =>
Time.now, :version_of => self.id)
      # go through each association and clone the attribs, pointing at
versioned

      self.phone_numbers.each do |phone_number|
        versioned.phone_numbers.build(phone_number.attributes)
      end
      ...

      self.save!
    end
  end
end

something like that maybe?
(totally untested code)

Matthew Rudy Jacobs
http://funchforlunch.blogspot.com

Matthew Rudy Jacobs wrote:

add_column :companies, :versioned_at, :datetime, :default => nil
add_column :companies, :version_of, :integer, :default => nil

and, if you did this,
you'd have to remember only to look at "live versions"

class Company
  named_scope :live, :conditions => {:version_of => nil}
end

and then always refer to;

Company.live.find(params[:id])