Skip to content

Instantly share code, notes, and snippets.

View bloudermilk's full-sized avatar

Brendan Loudermilk bloudermilk

View GitHub Profile
@bloudermilk
bloudermilk / memoized-helper-methods.md
Last active March 9, 2023 02:28
Explaining the rationale behind using memoized helper methods for controller resources

Last year I started playing around with using memoized private helper methods in my controllers instead of the traditional instance variable assigns we see in RoR controllers. Here's an example:

class PostsController < ApplicationController
  helper_method :new_post, :post, :posts
  
  def new; end
  def show; end
  def edit; end
 def index; end
@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"
@bloudermilk
bloudermilk / README.md
Created March 23, 2012 19:07
Facebook Page Categories

All of the categories!

I wrote a really simple JavaScript script that uses jQuery to extract all the categories from Facebook's "Create a page" page.

Instructions

  1. Navigate to the Create a page page.
  2. jQuerify the page.
  3. Open up your JavaScript console of choice and run the following script:
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# TODO: Generic 500 response here and in Rails
openapi: '3.0.2'
info:
title: Open Lead Specification
version: 'v1.0'
description: >
## Overview
This spec implements a two-phase auction system commonly known as "ping
tree" or "ping post". The purpose of the system is to enable sellers of
@bloudermilk
bloudermilk / weighted_list.rb
Created June 8, 2018 00:12
Simple Ruby algorithm to generate a weighted list
# README
#
# To run tests:
# $ ruby weighted_round_robin.rb
# PASS [[0, "a"]] makes []
# PASS [[1, "a"]] makes ["a"]
# PASS [[1, "a"], [2, "b"]] makes ["b", "a", "b"]
# ...
#
# To debug (prints index as ID):
@bloudermilk
bloudermilk / vancouver-to-sf-tour-pack-list.md
Last active March 21, 2018 21:09
Pack List for my Vancouver to San Francisco Pacific Coast tour

To Do

  • Buy missing items
  • Test pack

Essentials

  • Phone
  • Wallet
  • Keys
@bloudermilk
bloudermilk / lolcal_variables.rb
Created February 10, 2014 23:56
Is this anti-pattern bad news?
# I find myself turning code like this
cat = Cat.find(params[:cat_id])
laugh_at(cat)
# Into this
laugh_at(
Cat.find(params[:cat_id])
)
@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 / _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):