Skip to content

Instantly share code, notes, and snippets.

View bloudermilk's full-sized avatar

Brendan Loudermilk bloudermilk

View GitHub Profile
@bloudermilk
bloudermilk / text_areas.coffee
Created November 1, 2012 21:12
Github-style text areas
# Enables {command,ctrl}+enter on all textareas
# Author: Nick Giancola (@patbenatar)
$ ->
$("textarea").keydown (e) ->
if (e.ctrlKey or e.metaKey) and e.keyCode is 13
$("textarea").closest("form").submit()
@bloudermilk
bloudermilk / my_model.rb
Created October 19, 2012 20:11
Allow mass-assignment of type attribute in Rails
class MyModel < ActiveRecord::Base
private
# Allow mass-assignment of the type attribute
def self.attributes_protected_by_default
super - %W[ type ]
end
end
@bloudermilk
bloudermilk / dsl.rb
Created September 27, 2012 20:01
A syntax for tables
module Admin::TicketsHelper
def admin_tickets_table(tickets)
Tabular.build(tickets) do |table|
# If we're simply adding a few columns where the header would be the
# humanized attribute name and the row would contain the attribute `.to_s`
# then we can use the simple `#columns` method.
table.columns :id, :title
# If the column has custom logic for row values, we can use the `#column`
# method to define it
@bloudermilk
bloudermilk / _README.md
Created September 25, 2012 23:38
nginx config for use with Pow

Installation

Install nginx:

$ brew install nginx

Use custom launchctl plist (see homebrew.mxcl.nginx.plist below):

@bloudermilk
bloudermilk / benchmark.rb
Created September 9, 2012 00:40
Ruby single quotes vs. double quotes
require "benchmark"
Benchmark.bm(7) do |bench|
bench.report("single") do
1_000_000.times do
'This is a string of substantial length. I doubt you will have many string
literals in your code that are longer than this, but if there are actually
costs to parsing double quoted strings this should exacerbate that.'
end
end
@bloudermilk
bloudermilk / _routes.rb
Created September 7, 2012 20:18
devise + strong_parameters
# config/routes.rb
MyApp::Application.routes.draw do
devise_for :users, controllers: {
registrations: "users/registrations"
}
end
@bloudermilk
bloudermilk / fb_init.coffee
Created August 24, 2012 21:45 — forked from patbenatar/fb_init.coffee
Initialize Facebook JS SDK in Coffeescript
@FBootloader = new class FBootloader
callbacks: []
isReady: false
ready: ->
callback() for callback in @callbacks
bind: (callback) ->
if @isReady
callback()
@bloudermilk
bloudermilk / admin.rb
Created August 8, 2012 23:03
Stupid-simple invitations with Devise
class Admin < ActiveRecord::Base
attr_accessor :invite
devise :database_authenticatable, :registerable, :recoverable, :rememberable,
:trackable, :validatable
def self.new_invited(attributes)
new(attributes.merge(invite: true))
end
@bloudermilk
bloudermilk / gist:3147421
Created July 19, 2012 22:52
Lein (uber)jar fails
projects $ lein -v
Leiningen 2.0.0-preview7 on Java 1.6.0_33 Java HotSpot(TM) 64-Bit Server VM
projects $ lein new test
Generating a project called test based on the 'default' template.
To see other templates (app, lein plugin, etc), try `lein help new`.
projects $ cd test/
projects $ vim project.clj
test $ cat project.clj
(defproject test "0.1.0-SNAPSHOT"
:description "FIXME: write description"
@bloudermilk
bloudermilk / _README.md
Created June 6, 2012 21:32
Convert integer-keyed Hash parameters to Arrays in Rails

Fix for strong_parameters when using accepts_nested_attributes_for

The following before filter looks for any Hash parameters where all the keys are integers and turns them into Arrays. It searches recursively to allow the used of nested attributes within nested attributes. This is necessary to circumvent a bug in the strong_parameters gem which was reported in April 2012. Given params like:

{
  "person" => {
    "dogs_attributes" => {
      "0" => {
 "name" =&gt; "Fido"