versioning

An article by Gaspard Bucher
  1. action
  2. ajax
  3. API
    1. zafu
    2. query builder
    3. rubyless
    4. gold
    5. property
    6. versions
    7. remote
    8. zazen
    9. console
  4. classes
  5. common attributes
  6. conditions
  7. context
  8. dates
  9. display
  10. forms
  11. i18n
  12. meta
  13. SQLiss
  14. urls

It’s nice to have properties, but it would be even nicer if we could easily enable versioning. This is very easy: you just need to specify the name of a method to reach the model that will hold the properties storage. Here is an example that uses the brand new Versions gem:

class Contact < ActiveRecord::Base
  include Versions::Multi
  has_multiple :versions

  include Property
  store_properties_in :version

  property do |t|
    t.string   'name', :indexed => true
    t.string   'first_name'
    t.integer  'age'
    t.datetime 'seen_at', :default => Proc.new { Time.now }
  end
end

Your Version model would look like this:

class Version < ActiveRecord::Base
  include Versions::Auto

  def should_clone?
    true # always clone on update
  end
end

And that’s it. You can use the Contact class as if it had ‘name’ and ‘first_name’ attributes and the content will be transparently versioned.