Permalink
Browse files

#8 presun vytvoreni URL do pre validation hook

  • Loading branch information...
JakubMrozek committed Nov 15, 2012
1 parent ebc1fe1 commit bcceaa4aceef3fff5a54294d6912eab37b18afc6
Showing with 62 additions and 2 deletions.
  1. +0 −2 controllers/PageController.js
  2. +38 −0 models/Page.js
  3. +1 −0 test/api/pages.test.js
  4. +23 −0 test/unit/models/Page.test.js
@@ -3,7 +3,6 @@
* Zavislosti modulu.
*/
var Page = require(process.cwd() + '/models/Page');
-var url = require(process.cwd() + '/lib/filters/url');
var error = require(process.cwd() + '/lib/error');
var util = require(process.cwd() + '/lib/util');
@@ -66,7 +65,6 @@ exports.show = function(req, res, next){
exports.create = function(req, res, next){
var page = new Page();
page.title = req.body.title;
- page.url = url(req.body.title);
page.content = req.body.content;
page.save(function(err, doc) {
if (err) return next(err);
View
@@ -4,6 +4,7 @@
*/
var mongoose = require('mongoose');
+var url = require(process.cwd() + '/lib/filters/url');
var Schema = mongoose.Schema;
/**
@@ -27,6 +28,43 @@ var fields = {
var PageSchema = new Schema(fields);
+/**
+ * Vygeneruje URL pro stranku z titulku.
+ *
+ * Pouze pokud URL jiz neexistuje.
+ * Priklad:
+ *
+ * var page = new Page();
+ * page.name = 'Platebni podminky';
+ * page.content = 'Lorem ipsum set dolorem';
+ * page.save();
+ *
+ * //vznikne polozka page.url s hodnotou 'platebni-podminky'
+ *
+ * @param {Page} page
+ */
+
+function addUrl(page) {
+ if (typeof page.url === 'undefined') {
+ page.url = url(page.title);
+ }
+}
+
+/**
+ * Udalosti pred validaci.
+ *
+ * Akce:
+ * - pokud neexistuje URL, vygeneruje se z titulku
+ *
+ * @param {Function} next
+ */
+
+PageSchema.pre('validate', function(next){
+ addUrl(this);
+ next();
+});
+
+
/**
* Vybere dokument podle URL.
*
View
@@ -95,6 +95,7 @@ describe('API pages', function () {
Page.findOne({title: 'titulek ABC'}, function(err, doc) {
if (err) return done(err);
doc.title.should.equal('titulek ABC');
+ doc.url.should.equal('titulek-abc');
doc.content.should.equal('lorem ipsum set dolorem');
done();
});
@@ -3,6 +3,29 @@ var PageHoraa = require('horaa')(process.cwd() + '/models/Page');
describe('model Page', function(){
+ describe('pred validaci a vlozenim/editaci', function() {
+ it('se vytvori URL ze z titulku', function(done) {
+ var page = new Page();
+ page.title = 'Obchodni podminky';
+ page.content = 'Lorem ipsum set dolorem';
+ page.validate(function(){
+ page.url.should.eql('obchodni-podminky');
+ done();
+ });
+ });
+
+ it('se vytvori nezmeni URL, pokud jiz bylo nejake vytvoreno', function(done) {
+ var page = new Page();
+ page.title = 'Obchodni podminky';
+ page.url = 'jina-url';
+ page.content = 'Lorem ipsum set dolorem';
+ page.validate(function(){
+ page.url.should.eql('jina-url');
+ done();
+ });
+ });
+ });
+
describe('metoda inSchema', function() {
it('vrati true, pokud pole ve schema je, jinak vrátí false', function() {
Page.inSchema({url:1}).should.equal(true);

0 comments on commit bcceaa4

Please sign in to comment.