Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/printercu/costa
In the search of smth Rails-like for node
https://github.com/printercu/costa
Last synced: 27 days 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 (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-03-06T15:40:32.000Z (over 10 years ago)
- Last Synced: 2024-04-14T15:27:40.094Z (7 months ago)
- Language: CoffeeScript
- Homepage:
- Size: 285 KB
- Stars: 4
- Watchers: 4
- 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 = Postmodule.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 urlshowAction: ->
@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 urlbuildResource: (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 itemscompileBody: ->
@body = marked @body_md, @constructor.MARKED_OPTIONS
```## More documentation in tests & source
## License
MIT