Created
December 11, 2008 20:21
-
-
Save ashgti/34859 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'dm-core' | |
require 'dm-more' | |
require 'dm-timestamps' | |
require 'dm-is-versioned' | |
DataMapper.setup(:sqlite3, 'sqlite3::memory:') | |
DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[:sqlite3] | |
DataMapper::Logger.new(STDOUT, :debug) | |
DataObjects::Sqlite3.logger = DataObjects::Logger.new(STDOUT, :debug) | |
class Story | |
include DataMapper::Resource | |
property :id, Integer, :serial => true | |
property :title, String | |
property :updated_at, DateTime | |
before :save do | |
# For the sake of testing, make sure the updated_at is always unique | |
time = self.updated_at ? self.updated_at + 1 : Time.now | |
self.updated_at = time if self.dirty? | |
end | |
is_versioned :on => :updated_at | |
end | |
Story.auto_migrate! | |
story = Story.new | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment