Navigation Menu

Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Apr 7, 2012
0 parents commit 0a66686
Show file tree
Hide file tree
Showing 47 changed files with 7,082 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions Procfile
@@ -0,0 +1 @@
web: node server.js
3 changes: 3 additions & 0 deletions app/controllers/application_controller.js
@@ -0,0 +1,3 @@
before('protect from forgery', function () {
protectFromForgery('5d016d2ae47dfb552e63b831eb9cecd809d60ed8');
});
78 changes: 78 additions & 0 deletions app/controllers/books_controller.js
@@ -0,0 +1,78 @@
load('application');

before(loadBook, {only: ['show', 'edit', 'update', 'destroy']});

action('new', function () {
this.title = 'New book';
this.book = new Book;
render();
});

action(function create() {
Book.create(req.body.Book, function (err, book) {
if (err) {
flash('error', 'Book can not be created');
render('new', {
book: book,
title: 'New book'
});
} else {
flash('info', 'Book created');
redirect(path_to.books);
}
});
});

action(function index() {
this.title = 'Books index';
Book.all(function (err, books) {
render({
books: books
});
});
});

action(function show() {
this.title = 'Book show';
render();
});

action(function edit() {
this.title = 'Book edit';
render();
});

action(function update() {
this.book.updateAttributes(body.Book, function (err) {
if (!err) {
flash('info', 'Book updated');
redirect(path_to.book(this.book));
} else {
flash('error', 'Book can not be updated');
this.title = 'Edit book details';
render('edit');
}
}.bind(this));
});

action(function destroy() {
this.book.destroy(function (error) {
if (error) {
flash('error', 'Can not destroy book');
} else {
flash('info', 'Book successfully removed');
}
send("'" + path_to.books + "'");
});
});

function loadBook() {
Book.find(params.id, function (err, book) {
if (err) {
redirect(path_to.books);
} else {
this.book = book;
next();
}
}.bind(this));
}
91 changes: 91 additions & 0 deletions app/controllers/chapters_controller.js
@@ -0,0 +1,91 @@
load('application');

before(loadBook);
before(loadChapter, {only: ['show', 'edit', 'update', 'destroy']});

action('new', function () {
this.title = 'New chapter';
this.chapter = new Chapter;
render();
});

action(function create() {
this.book.chapters.create(req.body.Chapter, function (err, chapter) {
if (err) {
flash('error', 'Chapter can not be created');
render('new', {
chapter: chapter,
title: 'New chapter'
});
} else {
flash('info', 'Chapter created');
redirect(path_to.book_chapters(this.book));
}
}.bind(this));
});

action(function index() {
this.title = 'Chapters index';
this.book.chapters(function (err, chapters) {
render({
chapters: chapters
});
});
});

action(function show() {
this.title = 'Chapter show';
render();
});

action(function edit() {
this.title = 'Chapter edit';
render();
});

action(function update() {
this.chapter.updateAttributes(body.Chapter, function (err) {
if (!err) {
flash('info', 'Chapter updated');
redirect(path_to.chapter(this.chapter));
} else {
flash('error', 'Chapter can not be updated');
this.title = 'Edit chapter details';
render('edit');
}
}.bind(this));
});

action(function destroy() {
this.chapter.destroy(function (error) {
if (error) {
flash('error', 'Can not destroy chapter');
} else {
flash('info', 'Chapter successfully removed');
}
send("'" + path_to.chapters + "'");
});
});

function loadChapter() {
this.book.chapters.find(params.id, function (err, chapter) {
if (err) {
console.log(err);
redirect(path_to.book_chapters(this.book));
} else {
this.chapter = chapter;
next();
}
}.bind(this));
}

function loadBook() {
var self = this;
Book.find(params.book_id, function (err, book) {
if (err) return next(err);
if (!book) return next(new Error(404));
self.book = book;
next();
});
}

2 changes: 2 additions & 0 deletions app/helpers/books_helper.js
@@ -0,0 +1,2 @@
module.exports = {
};
2 changes: 2 additions & 0 deletions app/helpers/chapters_helper.js
@@ -0,0 +1,2 @@
module.exports = {
};
1 change: 1 addition & 0 deletions app/models/book.js
@@ -0,0 +1 @@
Book.hasMany(Chapter, {as: 'chapters', foreignKey: 'bookId'});
1 change: 1 addition & 0 deletions app/models/chapter.js
@@ -0,0 +1 @@
Chapter.belongsTo(Book, {as: 'book', foreignKey: 'bookId'});
6 changes: 6 additions & 0 deletions app/views/books/_form.ejs
@@ -0,0 +1,6 @@
<div class="control-group">
<%- form.label("name", false, {class: "control-label"}) %>
<div class="controls">
<%- form.input("name") %>
</div>
</div>
9 changes: 9 additions & 0 deletions app/views/books/edit.ejs
@@ -0,0 +1,9 @@
<div class="page-header"><h1>Edit book</h1></div>

<% form_for(book, {action: path_to.book(book), method: 'PUT', id: "book_form", class: 'form-horizontal'}, function (form) { %>
<%- partial('books/form.ejs', {locals: {form: form, book: book}}) %>
<div class="form-actions">
<%- form.submit('<i class="icon-ok icon-white"></i> Update book', {class: 'btn btn-primary'}) %> or
<%- link_to('Cancel', path_to.book(book), {class: 'btn'}) %>
</div>
<% });%>
39 changes: 39 additions & 0 deletions app/views/books/index.ejs
@@ -0,0 +1,39 @@
<div class="page-header">
<h1>My Books</h1>
</div>

<div class="row">
<div class="span12">
<p><%- link_to('<i class="icon-plus icon-white"></i> Add Book', path_to.new_book, {class: 'btn btn-primary'}) %></>
</div>
</div>

<div class="row">
<div class="span12">
<% if (books.length > 0) { %>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% books.forEach(function (book) { %>
<tr>
<td><strong><%- linkTo(book.name, path_to.book(book)) %></strong></td>
<td>
<%- link_to('<i class="icon-edit"></i> Edit', path_to.edit_book(book), {class: 'btn btn-mini'}) %>
<%- link_to('<i class="icon-remove icon-white"></i> Delete', path_to.book(book), {class: 'btn btn-mini btn-danger', method: 'delete', remote: true, jsonp: '(function (u) {location.href = u;})'}) %>
</td>
</tr>
<% }); %>
</tbody>
</table>
<% } else { %>
<p class="alert alert-block alert-info">
<strong>No books were found.</strong>
</p>
<% } %>
</div>
</div>
11 changes: 11 additions & 0 deletions app/views/books/new.ejs
@@ -0,0 +1,11 @@
<div class="page-header">
<h1>New book</h1>
</div>

<% form_for(book, {action: path_to.books, method: 'POST', id: "book_form", class: 'form-horizontal'}, function (form) { %>
<%- partial('books/form.ejs', {locals: {form: form, book: book}}) %>
<div class="form-actions">
<%- form.submit('<i class="icon-ok icon-white"></i> Create book', {class: 'btn btn-primary'}) %> or
<%- link_to('Cancel', path_to.books, {class: 'btn'}) %>
</div>
<% });%>
17 changes: 17 additions & 0 deletions app/views/books/show.ejs
@@ -0,0 +1,17 @@
<div class="page-header">
<h1>Details of book</h1>
</div>


<table class="table table-bordered">
<tbody>
<tr><th>name</th><td><%= book.name %></td></tr>
</tbody>
</table>

<div class="well">
<%- link_to('<i class="icon-edit icon-white"></i> Edit', path_to.edit_book(book), {class: 'btn btn-primary'}) %>
<%- link_to('<i class="icon-remove icon-white"></i> Delete', path_to.book(book), {class: 'btn btn-danger', method: 'delete', remote: true, jsonp: '(function (u) { location.href = u; })'}) %>
or
<%- link_to('Back to index', path_to.books) %>
</div>
6 changes: 6 additions & 0 deletions app/views/chapters/_form.ejs
@@ -0,0 +1,6 @@
<div class="control-group">
<%- form.label("title", false, {class: "control-label"}) %>
<div class="controls">
<%- form.input("title") %>
</div>
</div>
9 changes: 9 additions & 0 deletions app/views/chapters/edit.ejs
@@ -0,0 +1,9 @@
<div class="page-header"><h1>Edit chapter</h1></div>

<% form_for(chapter, {action: path_to.book_chapter(book, chapter), method: 'PUT', id: "chapter_form", class: 'form-horizontal'}, function (form) { %>
<%- partial('chapters/form.ejs', {locals: {form: form, chapter: chapter}}) %>
<div class="form-actions">
<%- form.submit('<i class="icon-ok icon-white"></i> Update chapter', {class: 'btn btn-primary'}) %> or
<%- link_to('Cancel', path_to.book_chapter(book, chapter), {class: 'btn'}) %>
</div>
<% });%>
39 changes: 39 additions & 0 deletions app/views/chapters/index.ejs
@@ -0,0 +1,39 @@
<div class="page-header">
<h1>Chapters of Book <%- book.title || book.name %></h1>
</div>

<div class="row">
<div class="span12">
<p><%- link_to('<i class="icon-plus icon-white"></i> New chapter', path_to.new_book_chapter(book), {class: 'btn btn-primary'}) %></>
</div>
</div>

<div class="row">
<div class="span12">
<% if (chapters.length > 0) { %>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% chapters.forEach(function (chapter, i) { %>
<tr>
<td><strong><%- link_to('chapter #' + (i + 1) + '. ' + chapter.title, path_to.book_chapter(book, chapter)) %></strong></td>
<td>
<%- link_to('<i class="icon-edit"></i> Edit', path_to.edit_book_chapter(book, chapter), {class: 'btn btn-mini'}) %>
<%- link_to('<i class="icon-remove icon-white"></i> Delete', path_to.book_chapter(book, chapter), {class: 'btn btn-mini btn-danger', method: 'delete', remote: true, jsonp: '(function (u) {location.href = u;})'}) %>
</td>
</tr>
<% }); %>
</tbody>
</table>
<% } else { %>
<p class="alert alert-block alert-info">
<strong>No chapters were found.</strong>
</p>
<% } %>
</div>
</div>
11 changes: 11 additions & 0 deletions app/views/chapters/new.ejs
@@ -0,0 +1,11 @@
<div class="page-header">
<h1>New chapter</h1>
</div>

<% form_for(chapter, {action: path_to.book_chapters(book), method: 'POST', id: "chapter_form", class: 'form-horizontal'}, function (form) { %>
<%- partial('chapters/form.ejs', {locals: {form: form, chapter: chapter}}) %>
<div class="form-actions">
<%- form.submit('<i class="icon-ok icon-white"></i> Create chapter', {class: 'btn btn-primary'}) %> or
<%- link_to('Cancel', path_to.book_chapters(book), {class: 'btn'}) %>
</div>
<% });%>
17 changes: 17 additions & 0 deletions app/views/chapters/show.ejs
@@ -0,0 +1,17 @@
<div class="page-header">
<h1>Details of chapter</h1>
</div>


<table class="table table-bordered">
<tbody>
<tr><th>title</th><td><%= chapter.title %></td></tr>
</tbody>
</table>

<div class="well">
<%- link_to('<i class="icon-edit icon-white"></i> Edit', path_to.edit_book_chapter(book, chapter), {class: 'btn btn-primary'}) %>
<%- link_to('<i class="icon-remove icon-white"></i> Delete', path_to.book_chapter(book, chapter), {class: 'btn btn-danger', method: 'delete', remote: true, jsonp: '(function (u) { location.href = u; })'}) %>
or
<%- link_to('Back to index', path_to.book_chapters(book)) %>
</div>

0 comments on commit 0a66686

Please sign in to comment.