Skip to content

Instantly share code, notes, and snippets.

View inossidabile's full-sized avatar

Boris Staal inossidabile

View GitHub Profile
synchronize: (block) ->
context = new Joosy.SynchronizationContext(this)
block.call(this, context)
@wait context.expectations, => context.after.call(this)
context.actions.each (data) =>
data[0].call this, =>
@trigger data[1]
@inossidabile
inossidabile / nginx.conf
Created March 19, 2012 22:26
Hashbang nginx config sample
upstream hashbang-localhost {
server localhost:22333;
}
server {
listen 80;
server_name ...;
location / {
# example taken from http://quickleft.com/blog/presenters-as-a-solution-to-asjson-woes-in-rails-apis
class Api::V1::ResourcePresenter
attr_reader :resource
def initialize( resource )
@resource = resource
end
# app/decorators/article_decorator.rb
class ArticleDecorator < ApplicationDecorator
decorates :article
def the_very_important_fields_set( include_root = false )
data_hash = {
:attr1 => att1,
:attr2 => attr2
}
data_hash = { :resource => data_hash } if include_root
# app/decorators/article_decorator.rb
class ArticleDecorator < ApplicationDecorator
decorates :article
def the_very_important_fields_set( include_root = false )
data = Jbuilder.encode do |j|
j.(self, :attr1, :attr2)
end
data = { :resource => data } if include_root
end
class Article < ActiveRecord::Base
include Heimdallr::Model
belongs_to :owner, :class_name => 'User'
restrict do |user, record|
if user.admin?
# Administrator or owner can do everything
scope :fetch
scope :delete
Article.restrict(current_user).where(:typical => true)
class ArticlesController < ApplicationController
include Heimdallr::Resource
load_and_authorize_resource
# or set the name explicitly:
#
# load_and_authorize_resource :resource => :article
# if nested:
Article.restrictions(current_user).allowed_fields[:create]
article = Article.restrict(current_user).first
@article = article.implicit
@article.protected_thing # => nil