Skip to content

Instantly share code, notes, and snippets.

View juliocesar's full-sized avatar

Julio Cesar Ody juliocesar

View GitHub Profile
@juliocesar
juliocesar / gist:3337086
Created August 13, 2012 04:56
Ain't over 'till it's over
# Oldie but goodie, forward slash path chaining, by _why.
describe '#compile_to_build' do
it "with a template not index.*, it simply copies it over to build/<path> if the extension is .html" do
@compiler.compile_to_build 'article.html'
(Anagen.templates/'article.html').read.should == (Anagen.build_templates/'article.html').read
end
it "with templates/index.*, compiles to build/index.html" do
@compiler.compile_to_build 'index.haml'
@juliocesar
juliocesar / Rakefile
Created August 10, 2012 02:06
Poor man's dev suite
task :run do
rackup = Thread.start { `rackup -p 4567` }
compass = Thread.start { `compass watch --sass-dir sass --css-dir css` }
# ...
at_exit { Thread.kill(rackup) and Thread.kill(compass) }
sleep
end
# In the console, just run:
# $ rake run
@juliocesar
juliocesar / crafting-apis.md
Created July 24, 2012 09:43
Crafting APIs

Back in version 1.2 or so, Rails introduced/massified a concept known as RESTful. That's appropriately named because RESTful isn't REST It's REST-like, and that's a good thing. Pure REST is a pain in the ass anyway.

Yes, APIs existed before that, REST or otherwise. Rails however redefined the landscape, and heavily influenced API design afterwards. Along with REST, it made popular the notion of pretty (or human as I call it) URLs. It went like this:

POST    /books      [data]

That creates a book with whatever POST [data] you're sending. It makes sense. You're "posting a book", which isn't far away from how humans understand what happened. Continuing:

PUT     /books/1    [data]
@juliocesar
juliocesar / gist:3037975
Created July 3, 2012 05:58
Getting headers on Backbone.Collection.prototype.fetch
# Actually more of an example of how to pick headers value and put them in Backbone collections.
class HeadersAwareCollection extends Backbone.Collection
parse : (resp, xhr) ->
@total = xhr.getResponseHeader('X-Total-Results')
super
# Alternatively, call `xhr.getAllResponseHeaders()`, parse the text, and add the whole object to
# the collection.
@juliocesar
juliocesar / app.coffee
Created June 30, 2012 02:55
Sample Tres app
$ ->
HomeScreen = Tres.Screen.extend()
App = new Tres.App
App.on
'' : new HomeScreen
App.boot()
@juliocesar
juliocesar / awesome_router.coffee
Created May 21, 2012 17:00
AwesomeRouter: a cool Backbone.js router class
# AwesomeRouter: A class that I've been using throughout a few Backbone.js apps I've built.
#
# It adds 2 features: an event that gets fired when one calls `navigate` (aptly named
# "navigate"), so you can bind things to when a URL gets triggered. And a "before filter"
# method `before`, which you can use to run things before every a route gets triggered.
#
# This could easily be extended to have after filters too, or to only trigger a route
# upon @before returning something that's not false.
class AwesomeRouter extends Backbone.Router
@juliocesar
juliocesar / gist:2690951
Created May 14, 2012 00:43
Rails the the tale of API design

Rails the the tale of API design

This all began in a discussion between myself Matt Allen. We're working on a project together. A project in which he started development on before I joined, using Rails with plugins such as simple_form, and resources such as accepts_nested_attributes (henceforth referred to as ANA). As he should, really.

I then joined in as a JavaScript developer, building an independent front-end, and got Matt to turn the existing Rails side of the app into an API server.

I soon enough hit a spot where I was submitting a resource which had zero or many nested resources in it. That then led me to deal with Rails' ANA API.

First, let's get a few things out of the way. I know that:

get '/scripts.js' do
content_type 'application/javascript'
these, scripts = params[:these].split(',').map { |s| s + '*' }.join(','), []
Dir.chdir Sinatra::Application.root + '/public/js'
Dir["{#{these}}.js"].each do |script|
scripts << File.read(script)
end
scripts.join(";")
end
@juliocesar
juliocesar / gist:2499995
Created April 26, 2012 14:38
console.log filter
// Monkey patch: If the last argument to console.log is `false`, nothing is outputted.
(function(log) {
console.log = function() {
if (arguments[arguments.length - 1] === false) return;
return log.apply(console, arguments);
}
})(console.log);
@juliocesar
juliocesar / gist:1243745
Created September 26, 2011 23:24
Set collection to saved/unsaved on change
var CookieCollection = Backbone.Collection.extend({
saved : false,
model : Cookie,
initialize: function() {
this.bind('add', this.setUnsaved, this);
this.bind('remove', this.setUnsaved, this);
},
setUnsaved: function() {