Permalink
Browse files

Default ID attribute is now 'int', if scaffold model template the def…

…ault adapter is sqlite(which will hopefully be added to model soon), and if the ID property is an 'int' it'll set this.autoIncrementId to true.

The scaffold controller template doesn't loop and add all properties on the update action, it now just calls updateAttributes, also in the create action if the ID property is a string it'll add params.id = params.id || geddy.string.uuid(10);
  • Loading branch information...
1 parent a7bc092 commit b262b1c3e36822d6cc9f1b08bbe7f700e23b8d0f @larzconwell larzconwell committed Aug 28, 2012
Showing with 77 additions and 14 deletions.
  1. +12 −9 templates/Jakefile
  2. +5 −5 templates/scaffold/controller.ejs
  3. +60 −0 templates/scaffold/model.ejs
View
@@ -100,10 +100,9 @@ namespace('db', function () {
namespace('gen', function () {
- var _writeTemplate = function (name, filename, dirname, options) {
- options = options || {};
-
- var names = utils.string.getInflections(name)
+ var _writeTemplate = function (name, filename, dirname, opts) {
+ var options = opts || {}
+ , names = utils.string.getInflections(name)
, text = fs.readFileSync(path.join(__dirname, filename + '.ejs'), 'utf8').toString()
, bare = options.bare || false // Default to full controller
, templ
@@ -121,7 +120,7 @@ namespace('gen', function () {
};
var _formatModelProperties = function (properties) {
- var obj = {'default': {name: 'id', type: 'string'}};
+ var obj = {'default': {name: 'id', type: 'int'}};
if (!properties) {
return obj;
}
@@ -159,7 +158,7 @@ namespace('gen', function () {
if (args === 'default') {
obj['default'] = {name: name, type: type};
// Add the original ID property
- obj['id'] = obj['id'] || {name: 'id', type: 'string'};
+ obj['id'] = obj['id'] || {name: 'id', type: 'int'};
}
else {
// If ID property is given and it matches the default
@@ -263,17 +262,21 @@ namespace('gen', function () {
{engine: engine, properties: modelProperties});
complete();
});
- modelTask.invoke(name, modelProperties);
+ modelTask.invoke(name, modelProperties, 'scaffold');
}, {async: true});
- task('model', function (name, properties) {
+ task('model', function (name, properties, modelPath) {
var createTableTask;
if (!name) {
throw new Error('No model name specified.');
}
+ if (!modelPath) {
+ modelPath = 'resource';
+ }
+ modelPath += '/model';
- _writeTemplate(name, 'resource/model', 'models', {
+ _writeTemplate(name, modelPath, 'models', {
inflection: 'singular'
, properties: _formatModelProperties(properties)
});
@@ -14,8 +14,12 @@ var <%= names.constructor.plural %> = function () {
};
this.create = function (req, resp, params) {
+<% for(var i in properties) { -%>
+<% if(properties[i].name === 'id' && properties[i].type === 'string') { -%>
params.id = params.id || geddy.string.uuid(10);
+<% } -%>
+<% } -%>
var self = this
, <%= names.property.singular %> = geddy.model.<%= names.constructor.singular %>.create(params);
@@ -47,11 +51,7 @@ var <%= names.constructor.plural %> = function () {
var self = this;
geddy.model.<%= names.constructor.singular %>.load(params.id, function(err, <%= names.property.singular %>) {
-<% for(var i in properties) { -%>
-<% if(properties[i].name !== 'id') { -%>
- <%= names.property.singular %>.<%= properties[i].name %> = params.<%= properties[i].name %>;
-<% } -%>
-<% } -%>
+ <%= names.property.singular %>.updateAttributes(params);
<%= names.property.singular %>.save(function(err, data) {
if(err) {
@@ -0,0 +1,60 @@
+var <%= names.constructor.singular %> = function () {
+
+ this.defineProperties({
+<% for(var i in properties) { -%>
+<% if(i === 'default') { -%>
+<% if(properties[i].name !== 'id') { -%>
+ <%= properties[i].name %>: {type: '<%= properties[i].type %>', required: true},
+<% } -%>
+<% } else { -%>
+<% if(i !== 'id') -%>
+ <%= properties[i].name %>: {type: '<%= properties[i].type %>'},
+<% } -%>
+<% } -%>
+ });
+
+<% for(var i in properties) { -%>
+<% if(properties[i].name === 'id' && properties[i].type === 'int') { -%>
+ this.autoIncrementId = true;
+<% } -%>
+<% } -%>
+ this.adapter = 'sqlite';
+
+ /*
+ this.property('login', 'string', {required: true});
+ this.property('password', 'string', {required: true});
+ this.property('lastName', 'string');
+ this.property('firstName', 'string');
+
+ this.validatesPresent('login');
+ this.validatesFormat('login', /[a-z]+/, {message: 'Subdivisions!'});
+ this.validatesLength('login', {min: 3});
+ // Use with the name of the other parameter to compare with
+ this.validatesConfirmed('password', 'confirmPassword');
+ // Use with any function that returns a Boolean
+ this.validatesWithFunction('password', function (s) {
+ return s.length > 0;
+ });
+
+ // Can define methods for instances like this
+ this.someMethod = function () {
+ // Do some stuff
+ };
+ */
+
+};
+
+/*
+// Can also define them on the prototype
+<%= names.constructor.singular %>.prototype.someOtherMethod = function () {
+ // Do some other stuff
+};
+// Can also define static methods and properties
+<%= names.constructor.singular %>.someStaticMethod = function () {
+ // Do some other stuff
+};
+<%= names.constructor.singular %>.someStaticProperty = 'YYZ';
+*/
+
+<%= names.constructor.singular %> = geddy.model.register('<%= names.constructor.singular %>', <%= names.constructor.singular %>);
+

0 comments on commit b262b1c

Please sign in to comment.