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 / application.html.erb
Created June 21, 2012 22:47
Simple breadcrumbs for Rails
<!-- Simple breadcrumbs for Rails -->
<!-- Example output -->
<!-- This example uses the Twitter Bootstrap classes for breadcrumbs -->
<!-- http://twitter.github.com/bootstrap/components.html#breadcrumbs -->
<% if breadcrumbs %>
<ul class="breadcrumb">
<% breadcrumbs.each do |crumb| %>
<li>
@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 / FlashToJSON_example.rb
Created July 2, 2012 19:29
Ruby serialization with JSON and YAML
> flash = ActionDispatch::Flash::FlashHash.new()
> flash[:notice] = "Success!"
> flash
#=> #<ActionDispatch::Flash::FlashHash:0x007fc46f95edc8 @used=#<Set: {}>, @closed=false, @flashes={:notice=>"Success!"}, @now=nil>
> encoded = ActiveSupport::JSON.encode(flash)
#=> "[[\"notice\",\"Success!\"]]"
> ActiveSupport::JSON.decode(encoded)
#=> [["notice", "Success!"]]
@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 / class_vs_instance_method.rb
Created July 9, 2012 18:28
Benchmark for Class method vs. instance method
# The verdict: Doesn't matter which one.
class MethodTesterObject
class << self
def perform(instances)
instances.each { |obj| obj.action }
end
end
@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 / controller.rb
Created July 12, 2012 19:05
Debug of the FlashHash
class PostsController < ApplicationController
def index
flash[:notice] = "Testing flash[:notice]"
flash.now[:notice] = "Testing flash.now[:notice]"
flash[:alert] = "Testing flash[:alert]"
flash.now[:alert] = "Testing flash.now[:alert]"
end
end
@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