https://github.com/printercu/costa
In the search of smth Rails-like for node
https://github.com/printercu/costa
Last synced: about 1 year ago
JSON representation
In the search of smth Rails-like for node
- Host: GitHub
- URL: https://github.com/printercu/costa
- Owner: printercu
- License: mit
- Created: 2013-07-13T16:28:06.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2014-03-06T15:40:32.000Z (about 12 years ago)
- Last Synced: 2025-04-09T23:11:28.040Z (about 1 year ago)
- Language: CoffeeScript
- Homepage:
- Size: 285 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Costa
Built with [CoffeeClasskit](https://github.com/printercu/coffee_classkit) &
[FlowCoffee](https://github.com/printercu/flow-coffee).
Inspired by Rails. Is kept tiny to be fast as fire.
## How can it look like
### Controller
```coffee
Post = require '../models/post'
resource_class = Post
module.exports =
class PostsController extends require './application_controller'
@extendsWithProto()
@beforeFilter 'buildResource', only: ['new', 'create']
@beforeFilter 'findResource', only: ['show', 'edit', 'update', 'delete']
indexAction: ->
resource_class.all @handleErrors (err, @collection) =>
@render {@collection}
newAction: ->
@render {@resource}
createAction: ->
@resource.save (err) =>
if err
if @resource.errors.hasAny
@res.format
json: => @res.status(422).jsonp @resource.exportFor 'api' # unprocessable_entity
html: =>
@res.flash 'error', @resource.errors.first()
@res.redirect "/posts/new"
else
@next(err)
else
url = "/posts/#{@resource.id}"
@res.format
json: => @res.status(201).set('Location', url).jsonp @resource.exportFor 'api' # created
html: =>
@res.flash 'Item created'
@res.redirect url
showAction: ->
@render {@resource}
editAction: ->
@render {@resource}
updateAction: ->
@resource.update @itemParams, (err) =>
if err
if @resource.errors.hasAny
@res.format
json: => @res.status(422).jsonp @resource.exportFor 'api' # unprocessable_entity
html: =>
@res.flash 'error', @resource.errors.first()
@res.redirect "/posts/#{@resource.id}/edit"
else
@next(err)
else
url = "/posts/#{@resource.id}"
@res.format
json: => @res.jsonp @resource.exportFor 'api'
html: =>
@res.flash 'Item updated'
@res.redirect url
buildResource: (callback) ->
@resource = resource_class.create @itemParams
callback()
findResource: (callback) ->
resource_class.find @req.param('id'), @handleErrors (err, @resource) =>
callback()
allowed_params = [
'title'
'body_md'
]
Object.defineProperty @::, 'itemParams', get: ->
@params allowed_params...
```
And now just call `PostsController.dispatch(action, req, res, next)`
the way you like with `action` one of `index, new, create, show, edit, update, destroy`.
### Model
```coffee
marked = require 'marked'
hljs = require 'highlight.js'
_ = require 'lodash'
module.exports =
class Post extends require './base_record'
@extendsWithProto()
@include require './concerns/with_fixtures'
@MARKED_OPTIONS =
highlight: (code, lang) ->
code = 'coffeescript' if code is 'coffee'
try
return hljs.highlight(lang, code).value
catch error
return hljs.highlightAuto(code).value
@exportAttrs 'body', 'body_md', 'title', 'created_at'
@validatesPresenceOf 'body_md', 'title'
@beforeCreate (callback) ->
@created_at = new Date
callback()
@beforeSave (callback) ->
@compileBody()
callback()
@all: (callback) ->
@maxId (err, max_id) =>
return callback err if err
@getMulti [1..max_id], (err, items) ->
return callback err if err
callback err, _.compact items
compileBody: ->
@body = marked @body_md, @constructor.MARKED_OPTIONS
```
## More documentation in tests & source
## License
MIT