Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Feature 211 articles #212

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ module.exports = Generator.extend({
}
}

// Copy the MVC specific views.
if (this.options.mvc) {
this.sourceRoot(path.join(__dirname, 'templates', 'mvc-views', views));
if (this.options.viewEngine == 'ejs') {
this.fs.copy(this.templatePath('.'), this.destinationPath('app/views'));
} else {
this.fs.copyTpl(this.templatePath('.'), this.destinationPath('app/views'), this);
}
}

// css
var stylesheets = this.options.cssPreprocessor;
if(stylesheets === 'none') stylesheets = 'css';
Expand Down
48 changes: 48 additions & 0 deletions app/templates/mvc-coffee/app/controllers/articles.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
express = require 'express'
router = express.Router()<% if(options.viewEngine == 'marko'){ %>
marko = require 'marko'<% } %><% if(options.database == 'mongodb'){ %>
mongoose = require 'mongoose'
Article = mongoose.model 'Article'<% } %><% if(options.database == 'mysql' || options.database == 'postgresql' || options.database == 'sqlite'){ %>
db = require '../models'<% } %><% if(options.database == 'rethinkdb'){ %>
models = require('../models')
Article = models.Article<% } %>

// Register the 'articles' path, All router items defined below will start with /articles.
module.exports = (app) ->
app.use '/articles', router

<% if(options.viewEngine == 'marko'){ %>
indexTemplate = marko.load require.resolve '../views/list.marko'<% } %>
router.get '/', (req, res, next) -><% if(options.database == 'mongodb'){ %>
Article.find (err, articles) ->
return next(err) if err<% } if(options.database == 'mysql' || options.database == 'postgresql' || options.database == 'sqlite'){ %>
router.get '/', (req, res) ->
db.Article.findAll().then (articles) -><% } %><% if(options.database == 'rethinkdb'){ %>
Article.run().then (articles) -><% } %><% if(options.viewEngine == 'marko'){ %>
indexTemplate.render
$global: locals: req.app.locals
title: 'Articles'
articles: articles
, res<% } else { %>
res.render 'index',
title: 'Articles'
articles: articles<% } %>

<% if(options.viewEngine == 'marko'){ %>
indexTemplate = marko.load require.resolve '../views/view.marko'<% } %>
router.get '/:articleId', (req, res, next) -><% if(options.database == 'mongodb'){ %>
Article.findById req.params.articleId, (err, article) ->
return next(err) if err<% } if(options.database == 'mysql' || options.database == 'postgresql' || options.database == 'sqlite'){ %>
db.Article.findById(req.params.articleId).then((article) -> <% } %><% if(options.database == 'rethinkdb'){ %>
Article.get(req.params.articleId).run().then((article) -> <% } %><% if(options.database == 'none'){ %>
var article = new Article();<% } %><% if(options.viewEngine == 'marko'){ %>
indexTemplate.render
$global: {locals: req.app.locals},
title: article.title,
article: article
, res<% } else { %>
res.render 'articles/view',
title: article.title
article: article
<% } %><% if(options.database !== 'none'){ %>
<% } %>
10 changes: 10 additions & 0 deletions app/templates/mvc-views/ejs/articles/list.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% include header %>
<h1><%-title %></h1>
<% for(var i=0; i < aritcles.length; i++) { %>
<li>
<a href='supplies/<%= aritcles[i].id %>'>
<%= aritcles[i].title %>
</a>
</li>
<% } %>
<% include footer %>
6 changes: 6 additions & 0 deletions app/templates/mvc-views/ejs/articles/view.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% include header %>

<h1><%-title %></h1>
<p><%- article.content %></p>

<% include footer %>
9 changes: 9 additions & 0 deletions app/templates/mvc-views/handlebars/articles/list.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{#if galleries }}
<ul>
{{#each articles as |article key|}}
<li><a href="articles/{{article.id}}">{{article.title}}</li>
{{/each}}
</ul>
{{else}}
No articles yet.
{{/if}}
2 changes: 2 additions & 0 deletions app/templates/mvc-views/handlebars/articles/view.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>{{title}}</h1>
<p>{{text}}</p>
6 changes: 6 additions & 0 deletions app/templates/mvc-views/jade/articles/list.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ul
for article in articles
li
a(href="/articles/{article.id}) {article.title}
else
li No articles yet.
3 changes: 3 additions & 0 deletions app/templates/mvc-views/jade/articles/view.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
block content
h1= title
p=article.text
11 changes: 11 additions & 0 deletions app/templates/mvc-views/marko/articles/list.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<layout-use("./../layout.marko")>
<layout-put into="body">
<h1>${title}</h1>
<ul>
<li for(article in articles)>
<a href="/articles/${article.id}">
${article.title}
</a>
</ul>
</layout-put>
</layout-use>
6 changes: 6 additions & 0 deletions app/templates/mvc-views/marko/articles/view.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<layout-use("./../layout.marko")>
<layout-put into="body">
<h1>${title}</h1>
<p>${article.text}</p>
</layout-put>
</layout-use>
12 changes: 12 additions & 0 deletions app/templates/mvc-views/nunjucks/articles/list.nunjucks
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'layout.nunjucks' %}

{% block content %}
<h1>{{ title }}</h1>
<ul>
{% for article in articles %}
<li><a href="/articles/{{ article._id }}">{{ article.title }}</li>
{% else %}
<li>No articles yet.</li>
{% endfor %}
</ul>
{% endblock %}
6 changes: 6 additions & 0 deletions app/templates/mvc-views/nunjucks/articles/view.nunjucks
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'layout.nunjucks' %}

{% block content %}
<h1>{{ title }}</h1>
<p>{{ article.text }}</p>
{% endblock %}
14 changes: 14 additions & 0 deletions app/templates/mvc-views/swig/articles/list.swig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'layout.swig' %}

{% block content %}
<h1>{{ title }}</h1>
{% if articles %}
<ul>
{% for article in articles %}
<a href="/articles/{{ article.id }}" >{{ article.title }}</a>
{% endfor %}
</ul>
{% else %}
<p>No articles yet.</p>
{% endif %}
{% endblock %}
6 changes: 6 additions & 0 deletions app/templates/mvc-views/swig/articles/view.swig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'layout.swig' %}

{% block content %}
<h1>{{ title }}</h1>
<p>{{ article.text }}</p>
{% endblock %}
54 changes: 54 additions & 0 deletions app/templates/mvc/app/controllers/articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var express = require('express'),
router = express.Router(),<% if(options.viewEngine == 'marko'){ %>
marko = require('marko'),<% } %><% if(options.database == 'mongodb'){ %>
mongoose = require('mongoose'),
Article = mongoose.model('Article');<% } %><% if(options.database == 'mysql' || options.database == 'postgresql' || options.database == 'sqlite'){ %>
db = require('../models');<% } %><% if(options.database == 'none'){ %>
Article = require('../models/article');<% } %><% if(options.database == 'rethinkdb'){ %>
models = require('../models'),
Article = models.Article;<% } %>

// Register the 'articles' path, All router items defined below will start with /articles.
module.exports = function (app) {
app.use('/articles', router);
};
<% if(options.viewEngine == 'marko'){ %>
var indexTemplate = marko.load(require.resolve('../views/articles/list.marko'));<% } %>
router.get('/', function (req, res, next) {<% if(options.database == 'mongodb'){ %>
Article.find(function (err, articles) {
if (err) return next(err);<% } %><% if(options.database == 'mysql' || options.database == 'postgresql' || options.database == 'sqlite'){ %>
db.Article.findAll().then(function (articles) {<% } %><% if(options.database == 'rethinkdb'){ %>
Article.run().then(function (articles) {<% } %><% if(options.database == 'none'){ %>
var articles = [new Article(), new Article()];<% } %><% if(options.viewEngine == 'marko'){ %>
indexTemplate.render({
$global: {locals: req.app.locals},
title: 'Articles',
articles: articles
}, res);<% } else { %>
res.render('articles/list', {
title: 'Articles',
articles: articles
});<% } %><% if(options.database !== 'none'){ %>
});<% } %>
});

<% if(options.viewEngine == 'marko'){ %>
var indexTemplate = marko.load(require.resolve('../views/articles/view.marko'));<% } %>
router.get('/:articleId', function (req, res, next) {<% if(options.database == 'mongodb'){ %>
Article.findById(req.params.articleId, function (err, article) {
if (err) return next(err);<% } %>
<% if(options.database == 'mysql' || options.database == 'postgresql' || options.database == 'sqlite'){ %>
db.Article.findById(req.params.articleId).then(function (article) {<% } %><% if(options.database == 'rethinkdb'){ %>
Article.get(req.params.articleId).run().then(function (article) {<% } %><% if(options.database == 'none'){ %>
var article = new Article();<% } %><% if(options.viewEngine == 'marko'){ %>
indexTemplate.render({
$global: {locals: req.app.locals},
title: article.title,
article: article
}, res);<% } else { %>
res.render('articles/view', {
title: article.title,
article: article
});<% } %><% if(options.database !== 'none'){ %>
});<% } %>
});
18 changes: 15 additions & 3 deletions test/test-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ describe('Express generator', function () {
var expected = [
'app/views/layout.jade',
'app/views/error.jade',
'app/views/index.jade'
'app/views/index.jade',
'app/views/articles/list.jade',
'app/views/articles/view.jade',
];
it('creates expected files', function (done) {
runGenerationTest.call(this, expected, 'mvc', 'jade', 'none', false, 'none', 'grunt', done);
Expand Down Expand Up @@ -379,7 +381,9 @@ describe('Express generator', function () {
'app/views/header.ejs',
'app/views/footer.ejs',
'app/views/error.ejs',
'app/views/index.ejs'
'app/views/index.ejs',
'app/views/articles/list.ejs',
'app/views/articles/view.ejs',
];

it('creates expected files', function (done) {
Expand Down Expand Up @@ -420,6 +424,8 @@ describe('Express generator', function () {
'app/views/index.swig',
'app/views/layout.swig',
'app/views/error.swig',
'app/views/articles/view.swig',
'app/views/articles/list.swig',
];

it('creates expected files', function (done) {
Expand All @@ -432,6 +438,8 @@ describe('Express generator', function () {
'app/views/index.nunjucks',
'app/views/layout.nunjucks',
'app/views/error.nunjucks',
'app/views/articles/view.nunjucks',
'app/views/articles/list.nunjucks',
];

it('creates expected files', function (done) {
Expand All @@ -445,6 +453,8 @@ describe('Express generator', function () {
'app/views/partials/welcome.handlebars',
'app/views/index.handlebars',
'app/views/error.handlebars',
'app/views/articles/view.handlebars',
'app/views/articles/list.handlebars',
];

it('creates expected files', function (done) {
Expand All @@ -456,7 +466,9 @@ describe('Express generator', function () {
var expected = [
'app/views/index.marko',
'app/views/error.marko',
'app/views/layout.marko'
'app/views/layout.marko',
'app/views/articles/view.marko',
'app/views/articles/list.marko',
];

it('creates expected files', function (done) {
Expand Down