Skip to content

Instantly share code, notes, and snippets.

View bloudermilk's full-sized avatar

Brendan Loudermilk bloudermilk

View GitHub Profile
@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])
)
jruby-1.7.10
@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 / com.bloudermilk.reddit_image_downloader.plist
Created October 11, 2013 23:56
Automate reddit_image_downloader on OS X with launchctl/launchd
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.bloudermilk.reddit_image_downloader</string>
<key>ProgramArguments</key>
<array>
<string>/Users/bloudermilk/.rbenv/versions/1.9.3-p327-perf/bin/ruby</string>
<string>/Users/bloudermilk/.rbenv/versions/1.9.3-p327-perf/bin/reddit_image_downloader</string>
@bloudermilk
bloudermilk / yo.md
Last active December 22, 2015 08:18
yo yo yo

Yo yo yo

yo yo yo

hey hey hey

server server server

local local local

@bloudermilk
bloudermilk / database_cleaner.rb
Created July 25, 2013 23:07
Example DatabaseCleaner config for fast tests
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
@bloudermilk
bloudermilk / bind_role.js
Created April 10, 2013 22:52
Backbone extension to bind roles to Views
// Automatically instantiates views based on the `role` attribute.
//
// Example:
// AA.Views.Workflow = Backbone.View.extend();
// AA.Views.Workflow.bindRole("workflow");
Backbone.View.bindRole = function (role) {
var view = this;
$(function () {
$("@" + role).each(function () {

N.B. Things that are starred with * are local variable

  • Ok, first generate a new rails app using this format:
    • rails new app_name
    • rails new amit_test_blog
  • Ok, next sketch out your models.
    • We're going to have two models for this blog: User and Post
    • One users can have many posts and many posts can belong to one users
    • Figure out the fields in each model.
    • Users will have these: field:type
@bloudermilk
bloudermilk / simple-git-flow.sh
Created March 20, 2013 23:48
A simple git workflow
#
# Starting
#
# Start a new feature branch
git checkout -b my_feature
# Make changes
# Add/Commit
@bloudermilk
bloudermilk / chronic_attribute.rb
Created February 7, 2013 02:34
Puts Chronic in front of date, time, and datetime attributes to allow for flexile input (anything supported by Chronic)
module ChronicAttribute
extend ActiveSupport::Concern
module ClassMethods
def chronic(*attrs)
attrs.each do |attr|
define_method "#{attr}=" do |value|
if value.is_a? String
with_utc do
self[attr] = Chronic.parse(value)