Skip to content

Instantly share code, notes, and snippets.

View trotter's full-sized avatar

Trotter Cashion trotter

View GitHub Profile
#Routes
map.resources :posts
map.resources :posts, :member => {:unpublish => :post}
#Posts controller
def unpublish
@post = Post.find(params[:id])
@post.current_state = "unpublished"
@post.save
respond_to do |format|
# Module w/ common states
module CommonStates
def answering_phones
state :answering_phones do
transitions_to :going_home, :if => :after_five?, :trigger => :shutdown_computer
end
end
end
# In your actual class
$.ajax({url: "http://www.cnn.com/asdfasdfasdf", success: function () { console.log("hi"); }})
# Rails style test definitions
class Test::Unit::TestCase
def self.setup(&block)
define_method(:setup, &block)
end
def self.teardown(&block)
define_method(:teardown, &block)
end
# Add this to your test helper to make it easy to add a debugger statement anywhere.
class Object
def debug
require 'ruby-debug'; debugger
end
end
class LabeledFormBuilder < ActionView::Helpers::FormBuilder
%w[text_field password_field select file_field text_area].each do |method_name|
alias_method("#{method_name}_without_randys_awesome", method_name)
define_method(method_name) do |field_name, *args|
content = ''
error = object.errors.on(field_name)
row_classes = 'form-row'
row_classes += ' error' unless error.blank?
label_classes = 'form-label'
label_classes += ' align' if %w|file_field select|.include?(method_name)
# The following two commands (inserted into the correct files) will let you
# easily tell what commits are on your current branch that are not on the target branch.
# Usage:
# git checkout master
# git diff-branch origin/master # Shows all unpushed changes
# git checkout my-new-feature
# git diff-branch master # Shows all commits on my-new-feature that are not on master
# In ~/.bashrc or ~/.profile
function parse_git_branch {
# usage: ruby convert-html-to-json.rb PATH_TO_FILE
require 'rubygems'
require 'hpricot'
require 'activesupport'
def node_to_s(node, indent_level=0)
return nil unless node.elem?
ret = ""
class Hash
def ref_with_destructuring(*args)
ret = args.map { |a| ref_without_destructuring(a) }
ret.size == 1 ? ret.first : ret
end
alias_method :ref_without_destructuring, :[]
alias_method :[], :ref_with_destructuring
end
# Convert the Cookie to its string representation.
def to_s
buf = ""
buf += @name + '='
if @value.kind_of?(String)
buf += CGI::escape(@value)
else
buf += @value.collect{|v| CGI::escape(v) }.join("&")
end