Permalink
Browse files

Comments

  • Loading branch information...
1 parent b1aac85 commit f35b74f5dbdce1c3739518195eb93ecd327d64af Anatoliy Chakkaev committed May 14, 2011
@@ -1,15 +1,22 @@
load('application')
action("page", function () {
+ console.log(req.session.twitter);
var path = req.params[0];
var page = Page.index[path];
if (page) {
+ if (page.comments) {
+ done();
+ } else {
+ page.loadComments(done);
+ }
+ } else {
+ redirect('/pages/new?path=' + path);
+ }
+ function done () {
render({
title: page.title,
page: page
});
- } else {
- redirect('/pages/new?path=' + path);
- // send("No routes matched with " + path);
}
});
@@ -0,0 +1,26 @@
+action('create', function () {
+ var path = req.body.path
+ if (!Page.pathCommentable(path)) {
+ return error('/');
+ }
+ req.comment = new Comment({
+ text: req.body.text,
+ path: path,
+ date: new Date()
+ });
+ req.comment.save(function (errors) {
+ if (errors) {
+ return error();
+ } else {
+ flash('info', 'Comment created');
+ Page.index[path].loadComments(function () {
+ redirect(path);
+ });
+ }
+ });
+
+ function error (path) {
+ flash('error', 'Comment can not be created');
+ redirect(path);
+ }
+});
@@ -0,0 +1,2 @@
+module.exports = {
+};
View
No changes.
View
@@ -34,3 +34,10 @@ Page.prototype.siblings = ->
Page.prototype.htmlContent = ->
require('markdown-js').makeHtml @content
+Page.pathCommentable = (path) ->
+ !!Page.index[path]
+
+Page.prototype.loadComments = (done) ->
+ Comment.find {path: @path}, (err, comments) =>
+ @comments = comments || []
+ done()
View
@@ -1 +1,23 @@
<%- page.htmlContent() %>
+
+<% if (page.path !== '/') { %>
+ <h2 id="discussion">Discussion</h2>
+
+ <% if (request.session.twitter) { %>
+ <% form_for('', {action: path_to.comments}, function (form) { %>
+ Leave comment as <%- request.session.twitter.screen_name %><br />
+ <%- form.input('path', {type: 'hidden', value: page.path}) %>
+ <%- form.textarea('text') %><br />
+ <%- form.submit('Post comment') %>
+ <% }); %>
+ <% } else { %>
+ <%- link_to('Connect with twitter', '/twitter_connect') %> to leave comment.
+ <% } %>
+
+ <% page.comments.forEach(function (comment) { %>
+ <div class="comment">
+ <%- comment.author %><br />
+ <%- sanitize(comment.text) %>
+ </div>
+ <% }); %>
+<% } %>
@@ -0,0 +1,12 @@
+<p>
+ <%- form.label("path") %><br />
+ <%- form.input("path") %>
+</p>
+<p>
+ <%- form.label("author") %><br />
+ <%- form.input("author") %>
+</p>
+<p>
+ <%- form.label("text") %><br />
+ <%- form.input("text") %>
+</p>
@@ -0,0 +1,7 @@
+<h1>Edit comment</h1>
+
+<% form_for(comment, {action: path_to.comment(comment), method: 'PUT', id: "comment_form"}, function (form) { %>
+ <%- partial('comments/form.ejs', {locals: {form: form, comment: comment}}) %>
+ <%- form.submit('Update comment') %> or
+ <%- link_to('Cancel', path_to.comment(comment)) %>
+<% });%>
@@ -0,0 +1,9 @@
+<h1>Index of comment</h1>
+
+<% comments.forEach(function (comment) { %>
+ comment #<%= comment.id %>
+ <%- link_to('Edit', path_to.edit_comment(comment)) %>
+ <%- link_to('Delete', path_to.comment(comment), {method: 'delete', remote: true, jsonp: '(function (u) {location.href = u;})'}) %><br/>
+<% }); %>
+
+<%- link_to('New comment', path_to.new_comment) %>
@@ -0,0 +1,7 @@
+<h1>New comment</h1>
+
+<% form_for(comment, {action: path_to.comments, method: 'POST', id: "comment_form"}, function (form) { %>
+ <%- partial('comments/form.ejs', {locals: {form: form, comment: comment}}) %>
+ <%- form.submit('Create comment') %> or
+ <%- link_to('Cancel', path_to.comments) %>
+<% });%>
@@ -0,0 +1,4 @@
+<h1>Details of comment</h1>
+<%- link_to('Edit', path_to.edit_comment(comment)) %>
+<%- link_to('Delete', path_to.comment(comment), {method: 'delete', remote: true, jsonp: '(function (u) { location.href = u; })'}) %><br/>
+<%- link_to('Back to index', path_to.comments) %>
@@ -22,6 +22,13 @@
</script>
</head>
<body>
+ <% var flash = request.flash('info').pop(); if (flash) { %>
+ <div class="flash info"><%- flash %></div>
+ <% } %>
+
+ <% flash = request.flash('error').pop(); if (flash) { %>
+ <div class="flash error"><%- flash %></div>
+ <% }; %>
<div class="wrap">
<div class="header">
<h1 class="title">
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Controller comment: <%= title %></title>
+ <%- stylesheet_link_tag('reset', 'scaffold') %>
+ <%- javascript_include_tag('https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', 'rails', 'application') %>
+ </head>
+ <body>
+ <% var flash = request.flash('info').pop(); if (flash) { %>
+ <div class="flash info"><%- flash %></div>
+ <% } %>
+
+ <% flash = request.flash('error').pop(); if (flash) { %>
+ <div class="flash error"><%- flash %></div>
+ <% }; %>
+
+ <div class="wrap">
+ <%- body %>
+ </div>
+ </body>
+</html>
View
@@ -0,0 +1,27 @@
+var mongoStore = require('connect-mongodb');
+var express = require('express');
+
+app.settings.db = JSON.parse(require('fs').readFileSync(__dirname + '/database.json', 'utf-8'))[app.settings.env];
+
+var mongoSessionStore = new mongoStore({
+ maxAge: 31536000000, // session lives forever (expire manually)
+ dbname: app.settings.db.database,
+ host: app.settings.db.host,
+ username: app.settings.db.user,
+ password: app.settings.db.password
+}, function () {});
+// Configuration
+
+app.configure(function(){
+ app.use(express.static(app.root + '/public'));
+ app.set('views', app.root + '/app/views');
+ app.set('view engine', 'ejs');
+ app.set('defaultLocale', 'en');
+ app.use(express.bodyParser());
+ app.use(express.cookieParser());
+ console.log(mongoSessionStore);
+ app.use(express.session({secret: 'secret', store: mongoSessionStore}));
+ app.use(express.methodOverride());
+ app.use(app.router);
+});
+
@@ -0,0 +1,11 @@
+var express = require('express');
+
+app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+ app.set('translationMissing', 'display');
+ app.disable('view cache');
+ app.disable('model cache');
+ app.disable('eval cache');
+ // app.set('translationMissing', 'default');
+});
+
@@ -0,0 +1,11 @@
+var express = require('express');
+
+app.configure('production', function () {
+ app.enable('view cache');
+ app.enable('model cache');
+ app.enable('eval cache');
+ app.use(express.errorHandler());
+ app.set('translationMissing', 'default');
+ app.settings.quiet = true;
+});
+
@@ -0,0 +1,6 @@
+var express = require('express');
+
+app.configure('staging', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+});
+
@@ -0,0 +1,6 @@
+app.configure('test', function(){
+ app.use(require('express').errorHandler({ dumpExceptions: true, showStack: true }));
+ app.settings.quiet = true;
+ app.set('translationMissing', 'display');
+});
+
View
@@ -5,4 +5,5 @@ exports.routes = function (map) {
map.post('/pages/reorder', 'pages#reorder');
map.get(/^(.*)$/, 'cms#page');
+ map.resources('comments', {only: 'create'});
};
View
@@ -46,3 +46,17 @@ PageSchema.add({
});
mongoose.model("Page", PageSchema);
module.exports["Page"] = mongoose.model("Page");
+
+
+/**
+ * Comment
+ */
+var CommentSchema = new Schema;
+CommentSchema.add({
+ path: { type: String, index: true },
+ author: { type: String },
+ text: { type: String },
+ date: { type: Date }
+});
+mongoose.model("Comment", CommentSchema);
+module.exports["Comment"] = mongoose.model("Comment");
View
@@ -1,2 +1,3 @@
require('ejs-ext');
require('timezone');
+require('twitter');
View
@@ -6,10 +6,10 @@
, "main": "server.js"
, "dependencies":
{ "ejs": ">= 0.2.1"
- , "express": ">= 2.2.2"
+ , "express": ">= 2.3.4"
, "nodeunit": ">= 0.5.0"
- , "connect-mongo": ">= 0"
- , "connect-mongodb": ">= 0"
+ , "connect-mongodb": ">= 0.3.0"
+ , "connect": ">=1.4.1"
}
, "repository":
{ "type": "git"
View
@@ -1,64 +1,7 @@
-/**
- * Module dependencies.
- */
-
-var express = require('express');
-
-var app = module.exports = express.createServer(),
- mongoStore = require('connect-mongodb');
-
-app.settings.db = JSON.parse(require('fs').readFileSync(__dirname + '/config/database.json', 'utf-8'))[app.settings.env];
-app.settings.sessionKey = 'railway.sid';
-
-var mongoSessionStore = mongoStore({
- // maxAge: 60000,
- dbname: app.settings.db.database,
- host: app.settings.db.host,
- username: app.settings.db.user,
- password: app.settings.db.password
-}, function () {});
-
-app.configure(function(){
- app.use(express.static(__dirname + '/public'));
- app.set('views', __dirname + '/app/views');
- app.set('view engine', 'ejs');
- // app.use(express.session({store: mongoSessionStore, key: app.settings.sessionKey}));
- app.use(express.bodyParser());
- app.use(express.cookieParser());
- app.use(express.session({secret: 'secret', store: mongoSessionStore}));
- app.use(express.methodOverride());
- app.use(app.router);
-});
-
-app.configure('development', function(){
- app.disable('view cache');
- app.disable('model cache');
- app.disable('eval cache');
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
-});
-
-app.configure('staging', function(){
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
-});
-
-app.configure('test', function(){
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
- app.settings.quiet = true;
-});
-
-app.configure('production', function(){
- app.enable('view cache');
- app.enable('model cache');
- app.enable('eval cache');
- app.use(express.errorHandler());
- app.settings.quiet = true;
-});
-
-require("./.railway/lib/onrailway").init(__dirname, app);
-
-// Only listen on $ node app.js
+var app = module.exports = require('express').createServer();
+require("./.railway/lib/onrailway").init(app);
if (!module.parent) {
- app.listen(8808);
+ app.listen(1602);
console.log("Express server listening on port %d", app.address().port)
}

0 comments on commit f35b74f

Please sign in to comment.