Methods

Files

DataMapper::Is::Versioned

Is Versioned

The Versioned module will configure a model to be versioned.

The is-versioned plugin functions differently from other versioning solutions (such as acts_as_versioned), but can be configured to function like it if you so desire.

The biggest difference is that there is not an incrementing 'version' field, but rather, any field of your choosing which will be unique on update.

Setup

For simplicity, I will assume that you have loaded dm-timestamps to automatically update your :updated_at field. See versioned_spec for and example of updating the versioned field yourself.

class Story
  include DataMapper::Resource
  property :id, Serial
  property :title, String
  property :updated_at, DateTime

  is_versioned :on => [:updated_at]
end

Auto Upgrading and Auto Migrating

Story.auto_migrate! # => will run auto_migrate! on Story::Version, too
Story.auto_upgrade! # => will run auto_upgrade! on Story::Version, too

Usage

story = Story.get(1)
story.title = "New Title"
story.save # => Saves this story and creates a new version with the
           #    original values.
story.versions.size # => 1

story.title = "A Different New Title"
story.save
story.versions.size # => 2

TODO: enable replacing a current version with an old version.

Public Instance Methods

is_versioned(options = {}) click to toggle source
# File lib/dm-is-versioned/is/versioned.rb, line 48
def is_versioned(options = {})
  @on = on = options[:on]

  extend(Migration) if respond_to?(:auto_migrate!)

  properties.each do |property|
    name = property.name
    before "#{name}=".to_sym do
      unless (value = property.get(self)).nil? || pending_version_attributes.key?(name)
        pending_version_attributes[name] = value
      end
    end
  end

  after :update do
    if clean? && pending_version_attributes.key?(on)
      model::Version.create(attributes.merge(pending_version_attributes))
      pending_version_attributes.clear
    end
  end

  extend ClassMethods
  include InstanceMethods
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.