Skip to content

Instantly share code, notes, and snippets.

@bricker
bricker / pastel-blocks.rb
Created September 19, 2012 22:16
Demo pastel colors
##
# Simple script which generates 20 "random" pastel colors and
# creates a (horrifically invalid) HTML template
# demonstrating those colors as blocks.
# Also provides the array of colors used.
#
# ruby ./pastel-blocks.rb
# open ./pastel-demo.html
#
require 'erb'
@bricker
bricker / unique_by_date_validator.rb
Created September 14, 2012 07:16
Makes sure an attribute is unique by a specified day, month, year, etc.
##
# Unique By validator
# Makes sure an attribute is unique by a specified day, month, year, etc.
# This validator is heavily based on ActiveRecord::Validations::UniquenessValidator
#
# It uses the `beginning_of_*` methods to fake the extraction of the filter (day, month, etc.)
# ActiveSupport::CoreExtensions::Date::Calculations
# http://apidock.com/rails/ActiveSupport/CoreExtensions/Date/Calculations
#
# `scope` must be a datetime field
@bricker
bricker / 2012-09-07-thought_on_auto_expiring_cache_keys.markdown
Created September 8, 2012 05:56
Auto-Expiring cache keys in Rails - easy, but what about performance?

Auto-Expiring cache keys in Rails - easy, but what about performance?

September 7th, 2012

This was sparked by this post by DHH: http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works

At first I was excited to read about a new method I hadn't seen before, ActiveRecord's cache_key. It seemed like I was going to restructure our entire cache strategy to take advantage of this cool technique.

@bricker
bricker / 2012070101_making_django_and_rails_play_nice.mdown
Created July 30, 2012 22:43
Making Django and Rails play nice: Part 2 (part 2)

Making Django and Rails play nice: Part 2 (part 2)

July 1, 2012

A few months ago, former KPCC developer Eric Richardson posted a fantastic solution to a seemingly simple problem: How do you share sessions between a Rails application and a Django application?

The issue: Ruby and Python serialize objects using different libraries (Marshal and Pickle, respectively).

@bricker
bricker / 2012073001_rails_dbsync.mdown
Created July 30, 2012 22:08
Rails: Synchronizing your development database with your production database using Rake tasks

Rails: Synchronizing your development database with your production database using Rake tasks

July 30, 2012

If you run an active website with lots of new content every day, it's often helpful to keep your local database up-to-date with your production database, so when you're working on the application locally you're working with fresh content.

Here are a couple rake tasks that you can copy-and-paste into your local Rails application which will make this syncing process a one-step process.

@bricker
bricker / 2012071901_interpolation_shorthand.mdown
Created July 19, 2012 23:21
Accidental Discovery: Interpolation shorthand in Ruby

Accidental Discovery: Interpolation shorthand in Ruby

July 19, 2012

I type in Textmate all day long. One of things its Ruby bundle does is auto-complete the string interpolation syntax for you. If you're inside of double-quotes (or a few other cases where string interpolation is possible), and you hit the # (hash) key, it writes #{} and places your cursor between the curly-brackets.

So, sometimes when I'm testing things in IRB, I forget that it doesn't auto-complete things like Textmate does. The result? Accidentally discovering a shorthand for string interpolation:

@name = "World"
puts "Hello #@name."
@bricker
bricker / 2012071401_symbols_vs_strings.mdown
Created July 15, 2012 06:38
The difference between symbols and strings in Ruby

The difference between symbols and strings in Ruby

July 14, 2012
1.9.3p0 :032 > :something.object_id
 => 4300488 
1.9.3p0 :033 > :something.object_id
 => 4300488 
1.9.3p0 :034 > :something.object_id
 => 4300488 
@bricker
bricker / 2012071301_replace_tags_in_nokogiri.mdown
Created July 13, 2012 22:49
Replacing tags inside of a Nokogiri Node

Replacing tags inside of a Nokogiri Node

July 13, 2012

I recently had some trouble trying to figure out how to do a simple gsub to the innner_html of a Nokogiri node. My problem was that I was trying something like this:

# Does not work!
document.at_css('p.content').inner_html.gsub!(/\n/, "<br />")
@bricker
bricker / has_secure_attribute.rb
Created July 10, 2012 04:34
has_secure_attribute, a smarter `has_secure_password` for Rails.
# This is a modified versions of Rails' built-in `has_secure_password`
# It allows you to do the same thing to any attribute.
# Useful for legacy databases, credit card information, addresses, etc.
# Use it pretty much the same way you would use has_secure_password
# You can also use any name for the digest column,
# just pass in an :encrypted_attribute option:
#
# has_secure_attribute :credit_card_number, encrypted_attribute: :ccn_hash
#
@bricker
bricker / rails_centOS_setup.mdown
Created July 8, 2012 06:37
Get a Rails app running on vanilla CentOS (5 or 6) via Nginx & Passenger in minutes

Setup RVM/Ruby, Nginx/Passenger, and Rails in minutes on vanilla CentOS (5 or 6)

July 1, 2012

Based on articles at http://articles.slicehost.com/centos

This gist only covers the steps that are necessary to get a Rails app running quickly via RVM, nginx and Passenger. It disregards security in many cases (such as RVM & iptables)! Go through the articles above for more on that.

Please note that I wrote this after I had already got it all working, so I apologize if