Skip to content

Commit

Permalink
allowed records to be created with ids, InMemory adapter now allows f…
Browse files Browse the repository at this point in the history
…or out of sequence ids
  • Loading branch information
syntacticx committed Feb 13, 2009
1 parent 497b164 commit 47393c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/active_record/adapters/in_memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ ActiveSupport.extend(Adapters.InMemory.prototype,{
data.id = max;
}
this.lastInsertId = data.id;
this.storage[table][max] = data;
this.storage[table][data.id] = data;
this.notify('created',table,data.id,data);
return true;
},
Expand Down
14 changes: 10 additions & 4 deletions src/active_record/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ ActiveSupport.extend(ActiveRecord.InstanceMethods,{
/**
* Persists the object, creating or updating as nessecary.
* @alias ActiveRecord.Instance.save
* @param {Boolean} force_created_mode Defaults to false, will force the
* record to act as if it was created even if an id property was passed.
* @return {Boolean}
*/
save: function save()
save: function save(force_created_mode)
{
//callbacks/proxy not working
if (!this._valid())
Expand All @@ -170,7 +172,7 @@ ActiveSupport.extend(ActiveRecord.InstanceMethods,{
{
this.set('updated',ActiveSupport.dateFormat('yyyy-mm-dd HH:MM:ss'));
}
if (!this.get(this.constructor.primaryKeyName))
if (force_created_mode || !this.get(this.constructor.primaryKeyName))
{
if (this.notify('beforeCreate') === false)
{
Expand All @@ -180,8 +182,12 @@ ActiveSupport.extend(ActiveRecord.InstanceMethods,{
{
this.set('created',ActiveSupport.dateFormat('yyyy-mm-dd HH:MM:ss'));
}
var id = this.get(this.constructor.primaryKeyName);
ActiveRecord.connection.insertEntity(this.tableName, this.constructor.primaryKeyName, this.toObject());
this.set(this.constructor.primaryKeyName, ActiveRecord.connection.getLastInsertedRowId());
if(!id)
{
this.set(this.constructor.primaryKeyName, ActiveRecord.connection.getLastInsertedRowId());
}
Synchronization.triggerSynchronizationNotifications(this,'afterCreate');
this.notify('afterCreate');
}
Expand Down Expand Up @@ -422,7 +428,7 @@ ActiveSupport.extend(ActiveRecord.ClassMethods,{
create: function create(data)
{
var record = this.build(data);
record.save();
record.save(true);
return record;
},
/**
Expand Down
15 changes: 14 additions & 1 deletion test/active_record/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,20 @@ ActiveTest.Tests.ActiveRecord.basic = function(proceed)
var count = Comment.count();
c.destroy();
assert(!c.reload() && count - 1 == Comment.count(),'destroy()');


//create with an id preserves id and still acts as "created"
var called = false;
Comment.observeOnce('afterCreate',function(){
called = true;
});
var d = Comment.create({
id: 50,
title: 'd',
body: 'dd'
});
d.reload();
assert(d.id == 50 && called,'create with an id preserves id and still acts as "created"');

Comment.destroy('all');
assert(Comment.count() == 0,'destroy("all")');

Expand Down

0 comments on commit 47393c1

Please sign in to comment.