Skip to content

Instantly share code, notes, and snippets.

@ngauthier
ngauthier / README.md
Created November 19, 2012 15:43
Model Accessors

Model Accessors

I really like this pattern for providing accessors to the models used in a controller. Alternatively, you usually see a before_filter to set the model from the params hash only on the actions that need it. IMO this is abusing a before_filter because the filter chain is for pre-processing the request. This does sort of make sense if you want to redirect away from the page gracefully if the record can't be found, but you can do that by just calling the accessor in a filter to make sure it's there.

This means that views are more like partials with locals, which is nice. It also means the accessor is as easy to test as a named filter. And as usual it pulls the method out of the action to share, which is also nice.

Also, it achieves the same ends as gems that try to OO the view, but without a gem and with terse code. The model can be swapped for a presenter if necessary.

@ngauthier
ngauthier / Rakefile
Created October 25, 2012 16:32
Coffeescript building with Rake and Jasmine testing with capybara
# This is a pure rake-style build process for building coffeescript files to js
# using the coffee-script ruby gem and Rake's `rule` and `file` directives.
# That means that a js file will only be built if it is out of date.
require 'rubygems'
require 'bundler/setup'
require 'coffee-script'
require 'capybara-jasmine'
require 'capybara/poltergeist'
require 'rake/clean'
@ngauthier
ngauthier / flyweight.coffee
Created October 22, 2012 19:13
Mobile Web Development with Backbone.js Teaser: Flyweight
class Flyweight
constructor: -> @objects = {}
wrap: (klass, keyFn) ->
=> @objects[keyFn(arguments...)] ?= new klass(arguments...)
class Widget extends Backbone.Model
Widget = Flyweight.wrap(Widget, (options) -> "widget.#{options.id}")
w1 = new Widget({id: 'alpha'})
@ngauthier
ngauthier / jslint_test.rb
Created August 29, 2012 17:42
jslint via capybara-webkit
require 'test_helper'
class JslintTest < ActionDispatch::IntegrationTest
include ActionView::Helpers::JavaScriptHelper
['file1', 'file2', 'file3'].each do |path|
path.gsub!(/^app\/assets\/javascripts\//, "")
path += '.js'
test "JSLINT #{path}" do
assert_jslint(path)
@ngauthier
ngauthier / Gemfile
Created August 29, 2012 14:36
Javascript Lint Lightning Talk
source :rubygems
gem 'showoff'
gem 'gli', '1.6.0'
@ngauthier
ngauthier / settimeout.js
Created July 18, 2012 14:35
Daisy-Chaining setTimeout
// if we have
function do() {
// thing that takes 100ms - 2s depending on browser
}
// Instead of:
setInterval(do, 500) // pray it doesn't lock the browser
// Do this:
@ngauthier
ngauthier / Gemfile
Created July 5, 2012 21:54
Prototype Gemfile
source 'http://rubygems.org'
gem 'rails'
gem 'pg'
gem 'puma'
gem 'newrelic_rpm'
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem 'jquery-rails'
@ngauthier
ngauthier / README.md
Created July 5, 2012 20:10
Rdio "native" in linux

I like Rdio and linux. Rdio works great in a browser except for one thing: keyboard shortcuts!!!

When coding, I like to be able to play/pause my music quickly, meaning I don't want to switch windows. I figured out a way to do this:

Google Chrome --app

First, I made a file in my ~/bin called rdio that runs:

google-chrome --app=http://rdio.com
@ngauthier
ngauthier / freelance-accounting.md
Created July 5, 2012 14:43
Freelance Accounting

Freelance Accounting

I'm currently using FreshBooks, but I'm considering using QuickBooks Online.

Problems with FreshBooks

  • I cannot send an invoice for prepaid hours that turns into client credit. I do this as a down payment or as a prepaid block of hours. I have to either collect a check outside freshbooks or make a "project" called "down payment" with a set budget to do time tracking properly.
  • I find it difficult to manage projects and time tracking, I click around a lot to get where I need to go, and some pages (like archived projects) I never remember how to get to.

Pluses of QuickBooks

#!/bin/sh
export GEM_HOME=.bundle/gems
export PATH=$GEM_HOME/bin:$PATH
cd `dirname $0`
echo "Working in `pwd`"
ruby -v
echo "Rubygems `gem -v`"
bundle -v 2>&1 || gem install bundler
bundle install --binstubs=.bundle/bin
foreman start