Permalink
Browse files

Merge branch 'master' of github.com:mde/geddy

  • Loading branch information...
2 parents 3baaf50 + fe9dc60 commit 22f12ddfabe57990472df97c8f643b36d24609d2 Techwraith committed Mar 13, 2012
Showing with 70 additions and 41 deletions.
  1. +5 −0 lib/app.js
  2. +6 −1 lib/base_config.js
  3. +10 −1 lib/base_controller.js
  4. +1 −1 lib/i18n/index.js
  5. +37 −36 lib/model/datatypes.js
  6. +4 −2 lib/routers/regexp_router.js
  7. +7 −0 templates/locales/en-us.json
View
@@ -77,6 +77,10 @@ var App = function () {
// Load models
// ==================
, _registerModels = function (next) {
+
+ // Set any model properties set in the config
+ geddy.mixin(geddy.model, geddy.config.model);
+
var modelDir = 'app/models'
, dirList
, item;
@@ -402,6 +406,7 @@ var App = function () {
controller.accessTime = accessTime;
if (typeof controller[params.action] == 'function') {
+ controller.app = self;
controller.request = reqObj;
controller.response = resp;
controller.method = method;
View
@@ -46,6 +46,11 @@ config = {
}
// Default to no SSL setup
, ssl: null
- };
+// Model defaults
+, model: {
+ useTimestamps: false
+ , forceCamel: true
+ }
+};
module.exports = config;
View
@@ -595,7 +595,16 @@ controller.BaseController.prototype = new (function () {
if (typeof target == 'string') {
url = target;
}
- else {
+ else if (typeof this.app.router.url == 'function') {
+ if (this.name && !target.controller)
+ target.controller = this.name;
+ if (this.params.format && !target.format)
+ target.format = this.params.format;
+
+ url = this.app.router.url(target);
+ }
+
+ if (!url) {
var contr = target.controller || this.name;
var act = target.action;
var ext = target.format || this.params.format;
View
@@ -8,7 +8,7 @@ var i18n = new (function () {
var defaultLocale = geddy.config.i18n.defaultLocale
, currentLocale = locale || defaultLocale
, currentLocaleStrings = _strings[currentLocale] || {}
- , defaultLocaleStrings = _strings[defaultLocale]
+ , defaultLocaleStrings = _strings[defaultLocale] || {}
, str = currentLocaleStrings[key]
|| defaultLocaleStrings[key] || "[[" + key + "]]";
for (p in opts){
View
@@ -16,6 +16,8 @@
*
*/
+var i18n = require('../i18n');
+
/*
* Datatype verification -- may modify the value by casting
*/
@@ -31,35 +33,35 @@ var datatypes = new (function () {
this.string = function (name, val, locale) {
return {
- err: null,
- val: String(val)
+ err: null
+ , val: String(val)
};
};
this.number = function (name, val, locale) {
if (isNaN(val)) {
return {
- err: 'Field "' + name + '" must be a Number.',
- val: null
+ err: i18n.getText('model.validatesNumber', {name: name}, locale)
+ , val: null
};
};
return {
- err: null,
- val: Number(val)
+ err: null
+ , val: Number(val)
};
};
this.int = function (name, val, locale) {
// Allow decimal values like 10.0 and 3.0
if (Math.round(val) != val) {
return {
- err: 'Field "' + name + '" must be an integer.',
- val: null
+ err: i18n.getText('model.validatesInteger', {name: name}, locale)
+ , val: null
};
};
return {
- err: null,
- val: parseInt(val, 10)
+ err: null
+ , val: parseInt(val, 10)
};
};
@@ -91,30 +93,29 @@ var datatypes = new (function () {
if (typeof validated != 'boolean') {
return {
- err: 'Field "' + name + '" must be a Boolean.',
- val: null
+ err: i18n.getText('model.validatesBoolean', {name: name}, locale)
+ , val: null
};
};
return {
- err: null,
- val: validated
+ err: null
+ , val: validated
};
};
this.object = function (name, val, locale) {
// Sure, Arrays are technically Objects, but we're treating Array as a
// separate datatype. Remember, instanceof Array fails across window
- // boundaries, so let's also make sure the Object doesn't have a 'length'
- // property.
+ // boundaries, so let's also make sure the Object isn't Array-ish
if (typeof val != 'object' || _isArray(val)) {
return {
- err: 'Field "' + name + '" must be an Object.',
- val: null
+ err: i18n.getText('model.validatesObject', {name: name}, locale)
+ , val: null
};
};
return {
- err: null,
- val: val
+ err: null
+ , val: val
};
};
@@ -123,28 +124,28 @@ var datatypes = new (function () {
// to make sure there's a length property
if (!_isArray(val)) {
return {
- err: 'Field "' + name + '" must be an Array.',
- val: null
+ err: i18n.getText('model.validatesArray', {name: name}, locale)
+ , val: null
};
};
return {
- err: null,
- val: val
+ err: null
+ , val: val
};
};
this.date = function (name, val, locale) {
var dt = geddy.date.parse(val);
if (dt) {
return {
- err: null,
- val: dt
+ err: null
+ , val: dt
};
}
else {
return {
- err: 'Field "' + name + '" must be in a valid date format.',
- val: null
+ err: i18n.getText('model.validatesDate', {name: name}, locale)
+ , val: null
};
}
};
@@ -153,14 +154,14 @@ var datatypes = new (function () {
var dt = geddy.date.parse(val);
if (dt) {
return {
- err: null,
- val: dt
+ err: null
+ , val: dt
};
}
else {
return {
- err: 'Field "' + name + '" must be in a valid datetime format.',
- val: null
+ err: i18n.getText('model.validatesDatetime', {name: name}, locale)
+ , val: null
};
}
};
@@ -171,14 +172,14 @@ var datatypes = new (function () {
var dt = geddy.date.parse(val);
if (dt) {
return {
- err: null,
- val: dt
+ err: null
+ , val: dt
};
}
else {
return {
- err: 'Field "' + name + '" must be in a valid time format.',
- val: null
+ err: i18n.getText('model.validatesTime', {name: name}, locale)
+ , val: null
};
}
};
@@ -199,6 +199,9 @@ var Router = function () {
this.url = function (params) {
var url = false;
+ // attempt the stringification with defaults mixed in
+ params = geddy.mixin({controller:'Application', action:'index' }, params);
+
// iterate through the existing routes until a suitable match is found
for (var i in this.routes) {
// do the controller & acton match?
@@ -210,8 +213,7 @@ var Router = function () {
this.routes[i].params.action != params.action) {
continue;
}
- // attempt the stringification with defaults mixed in
- params = geddy.mixin({controller:'Application', action:'index' }, params);
+
url = this.routes[i].stringify(params);
if (url) {
break;
@@ -7,4 +7,11 @@
, "model.validatesMinLength": "\"{name}\" must be at least {min} characters long."
, "model.validatesMaxLength": "\"{name}\" may not be more than {max} characters long."
, "model.validatesWithFunction": "\"{name}\" is not valid."
+, "model.validatesNumber": "\"{name}\" must be a number."
+, "model.validatesInteger": "\"{name}\" must be an integer."
+, "model.validatesObject": "\"{name}\" must be an object."
+, "model.validatesArray": "\"{name}\" must be an array."
+, "model.validatesDate": "\"{name}\" must be in a valid date format."
+, "model.validatesDatetime": "\"{name}\" must be in a valid datetime format."
+, "model.validatesTime": "\"{name}\" must be in a valid time format."
}

0 comments on commit 22f12dd

Please sign in to comment.