#977 ✓not-applicable
mrship

dm-timestamps are not set for objects created with #new rather than #create

Reported by mrship | July 16th, 2009 @ 10:08 AM

IMO, the created_at (and updated_at) properties that you get from using dm-timestamps should be set when creating an object with #new as well as with #create. At the moment, I have to override the initialize method to call set_timestamps manually.

Here's a spec to prove the failing behaviour (if you consider to be failing!)

Thoughts?


rrequire 'rubygems'
 
USE_DM_0_9 = false
 
if USE_DM_0_9
  DM_GEMS_VERSION   = "0.9.11"
  DO_GEMS_VERSION   = "0.9.12"
else
  DM_GEMS_VERSION   = "0.10.0"
  DO_GEMS_VERSION   = "0.10.0"
end
 
gem "data_objects",    DO_GEMS_VERSION
gem "dm-core",         DM_GEMS_VERSION         
gem "dm-aggregates",   DM_GEMS_VERSION   
gem "dm-migrations",   DM_GEMS_VERSION   
gem "dm-timestamps",   DM_GEMS_VERSION   
gem "dm-types",        DM_GEMS_VERSION        
gem "dm-validations",  DM_GEMS_VERSION  
gem "dm-serializer",   DM_GEMS_VERSION  
 
require "data_objects"
require "do_postgres"
require "dm-core"
require "dm-aggregates"
require "dm-migrations"
require "dm-timestamps"
require "dm-types"
require "dm-validations"
require "dm-serializer"

require 'spec'
 
SQLITE_FILE = File.join(`pwd`.chomp, "test.db")
 
DataMapper.setup(:default, "sqlite3:#{SQLITE_FILE}")
DataMapper.setup(:reloaded, "sqlite3:#{SQLITE_FILE}")
 
class Parent
  include DataMapper::Resource
  property :id, Serial
  property :name, String, :nullable => false
  
  property :created_at, Time
  property :updated_at, Time
end
 
module IdentityMapHelper
  def reload(object)
    object.class.get(object.id)
  end
  
  def with_db_reconnect(&blk)
    original_repository = DataMapper::Repository.context.pop
    repository(:reloaded, &blk)
    DataMapper::Repository.context << original_repository
  end
end
 
Spec::Runner.configure do |config|
  include IdentityMapHelper
  
  config.before(:each) do
    Parent.auto_migrate!
  end
  
  config.before(:each) do    
    DataMapper::Repository.context << repository(:default)
  end
  
  config.after(:each) do
    DataMapper::Repository.context.pop
  end
end
 
describe Parent, "with timestamps" do
  it "should set the timestamps for a #new object" do
    @parent = Parent.new
    @parent.created_at.should_not be_nil
  end
  
  it "behaves correctly for #create" do
    @parent = Parent.create(:name => "Homer")
    @parent.created_at.should_not be_nil
  end
end

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) July 22nd, 2009 @ 03:52 PM

    • State changed from “new” to “unconfirmed”
    • Milestone cleared.
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) July 22nd, 2009 @ 04:04 PM

    • Assigned user set to “Lindsay Holmwood”
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) September 10th, 2009 @ 03:59 PM

    • State changed from “unconfirmed” to “not-applicable”
    • Assigned user cleared.

    The intention for dm-timestamps was that the created_at and updated_at get set on save, and not before.

    If you wanted to have the default value set on the first access, you can define your properties with a :default option, eg:

    property :created_at, DateTime, :nullable => false, :auto_validation => false, :default => TIMESTAMP_PROPERTIES[:created_at]
    

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Pages