Permalink
Browse files

Added default option to model property arguments as @TechWraith sugge…

…sted

ex: geddy resource user name:string:default
ex: geddy resource user name:string:def
ex: geddy resource user name:default
ex: geddy resoruce user name:def

All set the `name` property as the "default" value that will be used as the
value that will be shown in views. It still uses the ID property in the background
as it's more reliable then user input
  • Loading branch information...
1 parent ef58c7b commit 9925793b52a7437df989087aef6a09bc4f056c4d @larzconwell larzconwell committed Jul 19, 2012
View
@@ -67,6 +67,9 @@ usage = [
, ' geddy resource user name admin:boolean'
, ' Generate a users resource with the model properties'
, ' name as a string and admin as a boolean'
+ , ' geddy scaffold user name:string:default'
+ , ' Generate a users scaffolding user name as the default'
+ , ' value to display data with'
, ''
].join('\n');
@@ -176,6 +176,10 @@ var Mongo = function (config) {
// Remove a specific instance by id
this.remove = function(id, opts, callback){
+ if(typeof id !== 'object') {
+ // Assume the id is specifically an ID field
+ id = {id: id};
+ }
if (typeof opts == 'function') {
callback = opts;
opts = {};
@@ -184,7 +188,7 @@ var Mongo = function (config) {
callback = function(){};
}
- this.collection.remove({id: id}, function(err, res){
+ this.collection.remove(id, function(err, res){
callback(err);
});
};
View
@@ -33,33 +33,52 @@ namespace('gen', function () {
};
var _formatModelProperties = function(properties) {
- if(!properties) return { 'id': 'string' };
+ var obj = {'default': {name: 'id', type: 'string'}};
+ if(!properties) return obj;
var itemsArr = properties.split(' ')
- , properties = {}
, name
, type
- , customId
+ , args
, i
, value;
- // Go over each item and parse it's name and type and add to `arr`
for(i in itemsArr) {
value = itemsArr[i];
name = value.replace(/:.*/g, '');
type = value.replace(/[a-zA-Z]*:?/, '');
+ args = type.replace(/[a-zA-Z]*:?/, '');
- // Set defaults and alias's
+ // Take off any args on the type
+ type = type.replace(/:.*/g, '');
+
+ // Defaults and alias's
if(!type) type = 'string';
if(type === 'integer') type = 'int';
+ if(type === 'bool') type = 'boolean';
+ if(args === 'def') args = 'default';
+ if(type === 'default' || type === 'def') {
+ // If the type is `default` assume they want `string`
+ // type and want this property to be default
+ type = 'string';
+ args = 'default';
+ }
- properties[name] = type;
+ // Set it as default if given the `default` argument
+ if(args === 'default') {
+ obj['default'] = {name: name, type: type};
+ // Add the original ID property
+ obj['id'] = obj['id'] || {name: 'id', type: 'string'};
+ } else {
+ // If ID property is given and it matches the default
+ // then rewrite the default with the new ID property
+ if(name === 'id' && obj['default'].name === 'id') {
+ obj['default'] = {name: name, type: type};
+ } else obj[name] = {name: name, type: type};
+ }
}
- // Check to see if a `id` property exists if not, add it
- // Note: reverseMerge moves the default(or changed default) objects to the beginning
- // of the object. Which is useful for our forEach hack in the model template
- return utils.object.reverseMerge(properties, { 'id': 'string' });
+ return obj;
};
// Creates a new Geddy app scaffold
@@ -2,12 +2,14 @@ var <%= names.constructor.singular %> = function () {
this.defineProperties({
<% for(var i in properties) { -%>
-<%# Hack: ID property will always be first so we will manually take care of not inserting -%>
-<%# A leading comma -%>
+<% if(i === 'default') { -%>
+ <%= properties[i].name %>: {type: '<%= properties[i].type %>', required: true}
+<% } else { -%>
<% if(i === 'id') { -%>
- <%= i %>: {type: '<%= properties[i] %>', required: true}
+ , <%= properties[i].name %>: {type: '<%= properties[i].type %>', required: true}
<% } else { -%>
- , <%= i %>: {type: '<%= properties[i] %>'}
+ , <%= properties[i].name %>: {type: '<%= properties[i].type %>'}
+<% } -%>
<% } -%>
<% } -%>
});
@@ -15,6 +15,7 @@ var <%= names.constructor.plural %> = function () {
this.create = function (req, resp, params) {
params.id = params.id || geddy.string.uuid(10);
+
var self = this
, <%= names.property.singular %> = geddy.model.<%= names.constructor.singular %>.create(params);
@@ -46,11 +47,10 @@ var <%= names.constructor.plural %> = function () {
var self = this;
geddy.model.adapter.<%= names.constructor.singular %>.load(params.id, function(err, <%= names.property.singular %>) {
-<% for(var prop in properties) { -%>
-<% if(prop !== 'id') { -%>
- <%= names.property.singular %>.<%= prop %> = params.<%= prop %>;
-<% } -%>
+<% for(var i in properties) { -%>
+ <%= names.property.singular %>.<%= properties[i].name %> = params.<%= properties[i].name %>;
<% } -%>
+
<%= names.property.singular %>.save(function(err, data) {
if(err) {
params.errors = err;
@@ -2,12 +2,14 @@ var <%= names.constructor.singular %> = function () {
this.defineProperties({
<% for(var i in properties) { -%>
-<%# Hack: ID property will always be first so we will manually take care of not inserting -%>
-<%# A leading comma -%>
+<% if(i === 'default') { -%>
+ <%= properties[i].name %>: {type: '<%= properties[i].type %>', required: true}
+<% } else { -%>
<% if(i === 'id') { -%>
- <%= i %>: {type: '<%= properties[i] %>', required: true}
+ , <%= properties[i].name %>: {type: '<%= properties[i].type %>', required: true}
<% } else { -%>
- , <%= i %>: {type: '<%= properties[i] %>'}
+ , <%= properties[i].name %>: {type: '<%= properties[i].type %>'}
+<% } -%>
<% } -%>
<% } -%>
});
@@ -12,29 +12,29 @@
</div>
<%% } %>
<% for(var i in properties) { -%>
-<% if(i !== 'id') { -%>
+<% if(properties[i].name !== 'id') { -%>
<div class="control-group">
- <label for="<%= i %>"><%= i %></label>
+ <label for="<%= properties[i].name %>"><%= properties[i].name %></label>
<div class="controls">
-<% if(properties[i] === 'string') { -%>
-<% if(i === 'password') { -%>
- <@- contentTag('input', '', {type:'password', class:'span6', placeholder:'enter <%= i %>', name:'<%= i %>'}) @>
+<% if(properties[i].type === 'string') { -%>
+<% if(properties[i].name === 'password') { -%>
+ <@- contentTag('input', '', {type:'password', class:'span6', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'}) @>
<% } else { -%>
- <@- contentTag('input', '', {type:'text', class:'span6', placeholder:'enter <%= i %>', name:'<%= i %>'}) @>
+ <@- contentTag('input', '', {type:'text', class:'span6', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'}) @>
<% } -%>
-<% } else if(properties[i] === 'number' || properties[i] === 'int') { -%>
- <@- contentTag('input', '', {type:'number', class:'span2', placeholder:'enter <%= i %>', name:'<%= i %>'}) @>
-<% } else if(properties[i] === 'boolean') { -%>
- <select name="<%= i %>", class="span1">
+<% } else if(properties[i].type === 'number' || properties[i].type === 'int') { -%>
+ <@- contentTag('input', '', {type:'number', class:'span2', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'}) @>
+<% } else if(properties[i].type === 'boolean') { -%>
+ <select name="<%= properties[i].name %>", class="span1">
<option selected>false</option>
<option>true</option>
</select>
-<% } else if(properties[i] === 'datetime') { -%>
- <@- contentTag('input', '', {type:'datetime', class:'span3', name:'<%= i %>'}) @>
-<% } else if(properties[i] === 'date') { -%>
- <@- contentTag('input', '', {type:'date', class:'span2', name:'<%= i %>'}) @>
+<% } else if(properties[i].type === 'datetime') { -%>
+ <@- contentTag('input', '', {type:'datetime', class:'span3', name:'<%= properties[i].name %>'}) @>
+<% } else if(properties[i].type === 'date') { -%>
+ <@- contentTag('input', '', {type:'date', class:'span2', name:'<%= properties[i].name %>'}) @>
<% } else { -%>
- <%= properties[i] %>
+ <%= properties[i].type %>
<% } -%>
</div>
</div>
@@ -12,34 +12,34 @@
</div>
<%% } %>
<% for(var i in properties) { -%>
-<% if(i !== 'id') { -%>
+<% if(properties[i].name !== 'id') { -%>
<div class="control-group">
- <label for="<%= i %>"><%= i %></label>
+ <label for="<%= properties[i].name %>"><%= properties[i].name %></label>
<div class="controls">
-<% if(properties[i] === 'string') { -%>
-<% if(i === 'password') { -%>
- <@- contentTag('input', <%= names.property.singular %>.<%= i %>, {type:'password', class:'span6', placeholder:'enter <%= i %>', name:'<%= i %>'}) @>
+<% if(properties[i].type === 'string') { -%>
+<% if(properties[i].name === 'password') { -%>
+ <@- contentTag('input', '', {type:'password', class:'span6', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'}) @>
<% } else { -%>
- <@- contentTag('input', <%= names.property.singular %>.<%= i %>, {type:'text', class:'span6', placeholder:'enter <%= i %>', name:'<%= i %>'}) @>
+ <@- contentTag('input', <%= names.property.singular %>.<%= properties[i].name %>, {type:'text', class:'span6', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'}) @>
<% } -%>
-<% } else if(properties[i] === 'number' || properties[i] === 'int') { -%>
- <@- contentTag('input', <%= names.property.singular %>.<%= i %>, {type:'number', class:'span2', placeholder:'enter <%= i %>', name:'<%= i %>'}) @>
-<% } else if(properties[i] === 'boolean') { -%>
- <select name="<%= i %>", class="span1">
- <@ if(<%= names.property.singular %>.<%= i %>) { @>
+<% } else if(properties[i].type === 'number' || properties[i].type === 'int') { -%>
+ <@- contentTag('input', <%= names.property.singular %>.<%= properties[i].name %>, {type:'number', class:'span2', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'}) @>
+<% } else if(properties[i].type === 'boolean') { -%>
+ <select name="<%= properties[i].name %>", class="span1">
+ <@ if(<%= names.property.singular %>.<%= properties[i].name %>) { -@>
<option>false</option>
<option selected>true</option>
<@ } else { -@>
<option selected>false</option>
<option>true</option>
<@ } -@>
</select>
-<% } else if(properties[i] === 'datetime') { -%>
- <@- contentTag('input', '', {type:'datetime', class:'span3', name:'<%= i %>'}) @>
-<% } else if(properties[i] === 'date') { -%>
- <@- contentTag('input', '', {type:'date', class:'span2', name:'<%= i %>'}) @>
+<% } else if(properties[i].type === 'datetime') { -%>
+ <@- contentTag('input', <%= names.property.singular %>.<%= properties[i].name %>, {type:'datetime', class:'span3', name:'<%= properties[i].name %>'}) @>
+<% } else if(properties[i].type === 'date') { -%>
+ <@- contentTag('input', <%= names.property.singular %>.<%= properties[i].name %>, {type:'date', class:'span2', name:'<%= properties[i].name %>'}) @>
<% } else { -%>
- <%= properties[i] %>
+ <%= properties[i].type %>
<% } -%>
</div>
</div>
@@ -7,7 +7,7 @@
<@ for(var i in <%= names.property.plural %>) { @>
<div class="row list-item">
<div class="span8">
- <h3><@- linkTo(<%= names.property.plural %>[i].id, <%= names.property.singular %>Path(<%= names.property.plural %>[i].id)); @></h3>
+ <h3><@- linkTo(<%= names.property.plural %>[i].<%= properties['default'].name %>, <%= names.property.singular %>Path(<%= names.property.plural %>[i].id)); @></h3>
</div>
<div class="span4">
<h3><i class="icon-list-alt"></i><@= <%= names.property.plural %>[i].id; @></h3>
@@ -1,5 +1,5 @@
<div class="hero-unit">
- <h2><@= <%= names.property.singular %>.id @></h2>
+ <h2><@= <%= names.property.singular %>.<%= properties['default'].name %> @></h2>
<@- linkTo('Edit this <%= names.property.singular %>', edit<%= names.constructor.singular %>Path(params.id), {class: 'btn pull-right'}); @>
</div>
@@ -9,28 +9,28 @@
li= err
<% for(var i in properties) { -%>
-<% if(i !== 'id') { -%>
+<% if(properties[i].name !== 'id') { -%>
.control-group
- label(for="<%= i %>") <%= i %>
+ label(for="<%= properties[i].name %>") <%= properties[i].name %>
.controls
-<% if(properties[i] === 'string') { -%>
-<% if(i === 'password') { -%>
- != contentTag('input', '', {type:'password', class:'span6', placeholder:'enter <%= i %>', name:'<%= i %>'})
+<% if(properties[i].type === 'string') { -%>
+<% if(properties[i].name === 'password') { -%>
+ != contentTag('input', '', {type:'password', class:'span6', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'})
<% } else { -%>
- != contentTag('input', '', {type:'text', class:'span6', placeholder:'enter <%= i %>', name:'<%= i %>'})
+ != contentTag('input', '', {type:'text', class:'span6', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'})
<% } -%>
-<% } else if(properties[i] === 'number' || properties[i] === 'int') { -%>
- != contentTag('input', '', {type:'number', class:'span2', placeholder:'enter <%= i %>', name:'<%= i %>'})
-<% } else if(properties[i] === 'boolean') { -%>
- select.span1(name="<%= i %>")
+<% } else if(properties[i].type === 'number' || properties[i].type === 'int') { -%>
+ != contentTag('input', '', {type:'number', class:'span2', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'})
+<% } else if(properties[i].type === 'boolean') { -%>
+ select.span1(name="<%= properties[i].name %>")
option(selected) false
option true
-<% } else if(properties[i] === 'datetime') { -%>
- != contentTag('input', '', {type:'datetime', class:'span3', name:'<%= i %>'})
-<% } else if(properties[i] === 'date') { -%>
- != contentTag('input', '', {type:'date', class:'span2', name:'<%= i %>'})
+<% } else if(properties[i].type === 'datetime') { -%>
+ != contentTag('input', '', {type:'datetime', class:'span3', name:'<%= properties[i].name %>'})
+<% } else if(properties[i].type === 'date') { -%>
+ != contentTag('input', '', {type:'date', class:'span2', name:'<%= properties[i].name %>'})
<% } else { -%>
- = properties[i]
+ | <%= properties[i].type %>
<% } -%>
<% } -%>
<% } -%>
@@ -9,32 +9,32 @@
li= err
<% for(var i in properties) { -%>
-<% if(i !== 'id') { -%>
+<% if(properties[i].name !== 'id') { -%>
.control-group
- label(for="<%= i %>") <%= i %>
+ label(for="<%= properties[i].name %>") <%= properties[i].name %>
.controls
-<% if(properties[i] === 'string') { -%>
-<% if(i === 'password') { -%>
- != contentTag('input', <%= names.property.singular %>.<%= i %>, {type:'password', class:'span6', placeholder:'enter <%= i %>', name:'<%= i %>'})
+<% if(properties[i].type === 'string') { -%>
+<% if(properties[i].name === 'password') { -%>
+ != contentTag('input', '', {type:'password', class:'span6', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'})
<% } else { -%>
- != contentTag('input', <%= names.property.singular %>.<%= i %>, {type:'text', class:'span6', placeholder:'enter <%= i %>', name:'<%= i %>'})
+ != contentTag('input', <%= names.property.singular %>.<%= properties[i].name %>, {type:'text', class:'span6', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'})
<% } -%>
-<% } else if(properties[i] === 'number' || properties[i] === 'int') { -%>
- != contentTag('input', <%= names.property.singular %>.<%= i %>, {type:'number', class:'span2', placeholder:'enter <%= i %>', name:'<%= i %>'})
-<% } else if(properties[i] === 'boolean') { -%>
- select.span1(name="<%= i %>")
- if <%= names.property.singular %>.<%= i %>
+<% } else if(properties[i].type === 'number' || properties[i].type === 'int') { -%>
+ != contentTag('input', <%= names.property.singular %>.<%= properties[i].name %>, {type:'number', class:'span2', placeholder:'enter <%= properties[i].name %>', name:'<%= properties[i].name %>'})
+<% } else if(properties[i].type === 'boolean') { -%>
+ select.span1(name="<%= properties[i].name %>")
+ if <%= names.property.singular %>.<%= properties[i].name %>
option false
option(selected) true
else
option(selected) false
option true
-<% } else if(properties[i] === 'datetime') { -%>
- != contentTag('input', '', {type:'datetime', class:'span3', name:'<%= i %>'})
-<% } else if(properties[i] === 'date') { -%>
- != contentTag('input', '', {type:'date', class:'span2', name:'<%= i %>'})
+<% } else if(properties[i].type === 'datetime') { -%>
+ != contentTag('input', <%= names.property.singular %>.<%= properties[i].name %>, {type:'datetime', class:'span3', name:'<%= properties[i].name %>'})
+<% } else if(properties[i].type === 'date') { -%>
+ != contentTag('input', <%= names.property.singular %>.<%= properties[i].name %>, {type:'date', class:'span2', name:'<%= properties[i].name %>'})
<% } else { -%>
- = properties[i]
+ | <%= properties[i].type %>
<% } -%>
<% } -%>
<% } -%>
@@ -6,7 +6,7 @@ if <%= names.property.plural %>
each <%= names.property.singular %> in <%= names.property.plural %>
.row.list-item
.span8
- h3!= linkTo(<%= names.property.singular %>.id, <%= names.property.singular %>Path(<%= names.property.singular %>.id))
+ h3!= linkTo(<%= names.property.singular %>.<%= properties['default'].name %>, <%= names.property.singular %>Path(<%= names.property.singular %>.id))
.span4
h3
i.icon-list-alt
@@ -1,5 +1,5 @@
.hero-unit
- h2= <%= names.property.singular %>.id
+ h2= <%= names.property.singular %>.<%= properties['default'].name %>
!= linkTo('Edit this <%= names.property.singular %>', edit<%= names.constructor.singular %>Path(params.id), {class: 'btn pull-right'})
h2 <%= names.constructor.singular %> Properties

0 comments on commit 9925793

Please sign in to comment.