Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 113 lines (97 sloc) 3.199 kb
65125d23 »
2011-05-12 Initial release
1 loadPage = ->
2 Page.findById req.params.id, (err, page) =>
3 if err or !page
4 redirect path_to.pages
5 else
6 @page = page
7 next()
8
9 before loadPage,
10 only: ['show', 'edit', 'update', 'destroy']
11
12 action 'new', ->
13 @page = new Page
14 @page.path = req.param('path') if req.param('path')?
15
16 render title: 'New page'
17
18 action 'create', ->
19 if Page.index[req.body.path]
20 flash 'error', 'Page for this path already exists'
21 render 'new', title: 'New page'
22 else
23 @page = new Page
24 @page["title"] = req.body["title"]
25 @page["content"] = req.body["content"]
26 @page["path"] = req.body["path"]
27 @page.save (errors) =>
28 if errors
29 flash 'error', 'Page can not be created'
30 render 'new', title: 'New page'
31 else
32 flash 'info', 'Page created'
33 Page.buildIndex =>
34 redirect @page.path
35
36 action 'index', ->
37 @pages = Page.getSorted()
38
39 if req.params.format == 'json'
40 send @pages
41 else
42 render
43 title: 'Pages index'
44
45 action 'show', ->
46 render title: 'Page show'
47
48 action 'edit', ->
49 render title: 'Page edit'
50
51 action 'update', ->
52 if Page.index[req.body.path] && Page.index[req.body.path].id != @page.id
53 flash 'error', 'Page can not be updated'
54 render 'edit', title: 'Edit page details'
55 else
56 @page["title"] = req.body["title"]
57 @page["content"] = req.body["content"]
58 @page["path"] = req.body["path"]
59 @page.save (err) =>
60 if !err
61 flash 'info', 'Page updated'
62 Page.buildIndex =>
63 redirect @page.path
64 else
65 flash 'error', 'Page can not be updated'
66 render 'edit', title: 'Edit page details'
67
68 action 'destroy', ->
69 @page.remove (error) ->
70 if error
71 flash 'error', 'Can not destroy page'
72 else
73 flash 'info', 'Page successfully removed'
74
75 Page.buildIndex () ->
76 send "'" + path_to.pages + "'"
77
78 action 'reorder', ->
79 wait = 0
80 req.body.order.forEach (path, index) ->
81 wait += 1
82 Page.index[path].pos = index
83 Page.index[path].save ->
84 if --wait == 0
85 Page.buildIndex ->
86 Page.getSorted().forEach (p) ->
87 console.log p.pos
88 send 'ok'
66a87a53 »
2011-05-12 Added dump
89
90 action 'backup', ->
91 require('fs').writeFileSync(app.root + '/db/dump.json', JSON.stringify(Page.getSorted().map (page) -> page.doc))
92 send 'ok'
93
94 action 'restore', ->
95 wait = 0
96 waitSaving = 0
97 saved = ->
98 if --waitSaving == 0
99 send '0k'
100 done = ->
101 if --wait == 0
102 pages = JSON.parse(require('fs').readFileSync(app.root + '/db/dump.json').toString('utf8'))
103 pages.forEach (page) ->
104 delete page._id
105 p = new Page(page)
106 waitSaving += 1
107 p.save(saved)
108
109 Page.find (err, pages) ->
110 pages.forEach (page) ->
111 wait += 1
112 page.remove(done)
Something went wrong with that request. Please try again.