Skip to content

Commit

Permalink
Support custom scenario option in .first and .all calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Ng committed Jul 4, 2013
1 parent 09a3274 commit 42fcf4a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
6 changes: 6 additions & 0 deletions lib/adapters/memory/index.js
Expand Up @@ -55,6 +55,12 @@ utils.mixin(Adapter.prototype, new (function () {

if (id) {
item = _data[key][id];

// Re-validate params with the correct scenario
if(item) {
model.validateAndUpdateFromParams(item, item.toObj(), {scenario: query.opts.scenario});
}

callback(null, item);
}
else {
Expand Down
4 changes: 2 additions & 2 deletions lib/adapters/mongo/index.js
Expand Up @@ -206,7 +206,7 @@ utils.mixin(Adapter.prototype, new (function () {
}
else {
if (data) {
inst = query.model.create(data, {scenario: 'reify'});
inst = query.model.create(data, {scenario: query.opts.scenario});
inst.id = id;
inst._id = data._id;
inst._saved = true;
Expand Down Expand Up @@ -244,7 +244,7 @@ utils.mixin(Adapter.prototype, new (function () {
else {
rows = data;
rows.forEach(function (row) {
var inst = query.model.create(row, {scenario: 'reify'});
var inst = query.model.create(row, {scenario: query.opts.scenario});
inst.id = row.id;
inst._id = row._id;
inst._saved = true;
Expand Down
4 changes: 2 additions & 2 deletions lib/adapters/riak/index.js
Expand Up @@ -142,7 +142,7 @@ utils.mixin(Adapter.prototype, new (function () {
}
}
else {
inst = query.model.create(data, {scenario: 'reify'});
inst = query.model.create(data, {scenario: query.opts.scenario});
inst.id = id;
inst._saved = true;
res.push(inst);
Expand Down Expand Up @@ -173,7 +173,7 @@ utils.mixin(Adapter.prototype, new (function () {
else {
rows = data;
rows.forEach(function (row) {
var inst = query.model.create(row, {scenario: 'reify'});
var inst = query.model.create(row, {scenario: query.opts.scenario});
inst.id = row.id;
inst._saved = true;
res.push(inst);
Expand Down
12 changes: 6 additions & 6 deletions lib/adapters/sql/postgres.js
Expand Up @@ -34,7 +34,7 @@ Adapter.prototype.constructor = Adapter;

utils.mixin(Adapter.prototype, new (function () {

var _itemsWithSQL = function (sql, tables, mainTable, callback) {
var _itemsWithSQL = function (sql, tables, mainTable, query, callback) {
var self = this;
this.exec(sql, function (err, data) {
var res
Expand Down Expand Up @@ -91,7 +91,7 @@ utils.mixin(Adapter.prototype, new (function () {
if (p == mainTable) {
// This is a new id -- create an owner object
if (params.id != mainId) {
inst = modelItem.ctor.create(params, {scenario: 'reify'});
inst = modelItem.ctor.create(params, {scenario: query.opts.scenario});
inst._saved = true;
res.push(inst);
}
Expand All @@ -105,11 +105,11 @@ utils.mixin(Adapter.prototype, new (function () {
// Create an array to hold the items if necessary
if (modelItem.assnType == 'hasMany') {
inst[p] = inst[p] || [];
inst[p].push(modelItem.ctor.create(params, {scenario: 'reify'}));
inst[p].push(modelItem.ctor.create(params, {scenario: query.opts.scenario}));
}
else {
inst[utils.string.getInflection(p, 'property', 'singular')] =
modelItem.ctor.create(params, {scenario: 'reify'});
modelItem.ctor.create(params, {scenario: query.opts.scenario});
}
}
}
Expand Down Expand Up @@ -199,7 +199,7 @@ utils.mixin(Adapter.prototype, new (function () {
}
sql += ';'

_itemsWithSQL.apply(this, [sql, selects, tableName, function (err, res) {
_itemsWithSQL.apply(this, [sql, selects, tableName, query, function (err, res) {
// If explicitly limited to one, just return the single instance
// This is also used by the `first` method
if (res && query.opts.limit == 1) {
Expand All @@ -214,7 +214,7 @@ utils.mixin(Adapter.prototype, new (function () {
var name = this._tableizeModelName(model.modelName)
, sql = 'SELECT ' + name + '.* FROM ' + name;
sql += ' ' + query;
_itemsWithSQL.apply(this, [sql, [name], name, callback]);
_itemsWithSQL.apply(this, [sql, [name], name, query, callback]);
};

this.update = function (data, query, callback) {
Expand Down
4 changes: 3 additions & 1 deletion lib/index.js
Expand Up @@ -473,7 +473,9 @@ utils.mixin(model, new (function () {
, query = args.shift() || {}
, opts = args.shift() || {}
, adapt;


opts.scenario = opts.scenario || 'reify';

query = new Query(model[name], query, opts);

adapt = model.getAdapterForModel(name);
Expand Down
26 changes: 26 additions & 0 deletions test/integration/adapters/shared.js
Expand Up @@ -507,6 +507,32 @@ tests = {
});
}

, 'test validations on fetch with scenario': function (next) {
var u = User.create({
login: 'as' // Too short, will cause validation error on reify
// Invalid model as confirmPassword should fail
});
u.save({force: true}, function (err, data) {
if (err) {
throw err;
}
currentId = data.id;

// Fetch the invalid model
User.first(currentId, {scenario: 'update'}, function (err, data) {
// Ensure that reification worked
assert.ok(typeof data.toObj === 'function');

// Ensure that we get errors about the password, but not the login
assert.ok(!data.errors.login);
assert.ok(typeof data.errors.password !== 'undefined');

// Cleanup
User.remove(data.id, next);
});
});
}

, 'test hasOne association': function (next) {
var u = User.create({
login: 'asdf'
Expand Down

0 comments on commit 42fcf4a

Please sign in to comment.