Skip to content

Instantly share code, notes, and snippets.

@bricker
bricker / sessions.py
Created June 21, 2012 21:43
Simple PyYAML constructor for handling Ruby Flash Hash
# Simple PyYAML constructor for handling Ruby Flash Hash
# Not perfect or graceful - just takes the Ruby Flash object
# and the Set object inside of it, and nullifies them.
# This could be expanded to handled different tags.
# If you wanted to maintain the Ruby objects across requests,
# or use convert a Ruby object to a Python object, you'll
# need something a little more complex, but this should get
# you started.
@bricker
bricker / example.rb
Created June 21, 2012 23:43
Ruby + Lyris HQ (EmailLabs) - A basic Ruby wrapper for the Email Labs API
# Ruby + Lyris HQ (EmailLabs) - A basic Ruby wrapper for the Email Labs API
# Example usage.
# Message is an imaginary object that you'll have to setup
@message = Message.new(email_subject: "Welcome to My Website!", body: "Welcome to My Website! You can browse blog entries, and watch videos, and much more! Thanks for joining! -The Management" email_sent: false>
lyris = Lyris.new(@message)
if lyris.add_message and lyris.send_message
@message.update_column(:email_sent, true)
@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

@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 / 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 / 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
 =&gt; 4300488 
@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 / 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 / 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 / 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