Skip to content

Instantly share code, notes, and snippets.

# Don't know about better, but fewer lines:
def mock_controller
['test_request','test_response'].each {|fname| require "action_dispatch/testing/#{fname}" }
controller = ApplicationController.new
controller.perform_caching = true
controller.cache_store = ActiveSupport::Cache::MemoryStore.new
['request','response'].each {|r| controller.send("#{r}=", ActionDispatch.const_get("Test#{r.capitalize}").new) }
controller.initialize_template_class controller.response
controller.assign_shortcuts controller.request, controller.response
@davidjbeveridge
davidjbeveridge / unsafe_receiver.rb
Created April 15, 2011 23:13
In case you need to send to a private in 1.9.2:
module UnsafeReceiver
def send! meth, *args
unless self.respond_to? meth
self.class.send :public, meth
retval = method(meth).call(*args)
self.class.send :private, meth
return retval
end
send meth, *args
end
@davidjbeveridge
davidjbeveridge / method_missing_example.rb
Created May 18, 2011 22:56
hacking hash access w/ method_missing
class Person
def initialize
@hash = Hash.new
end
def method_missing(name, value=nil, *args)
# Assign only valid keys:
if [:name, :dob, :height, :weight].include? name
@hash[name]
elsif value and [:name=, :dob=, :height=, :weight=].include?(name)
try {
// Some possibly erroneous code.
throw new Error("Whoops. Bad stuff happened.");
} catch (e) {
Hoptoad.notify(e);
}
@davidjbeveridge
davidjbeveridge / book.rb
Created July 11, 2011 19:26
Blocks and scope
class Book
attr_accessor :title
attr_accessor :author
attr_reader :chapters
attr_reader :pages
def initialize
@chapters = []
@pages = []
end
@davidjbeveridge
davidjbeveridge / application_helper.rb
Created July 18, 2011 16:59
helper method for helper methods...
class ApplicationHelper
# Allows you to mark helper methods as safe
# eg: helper :my_helper_method
class << self
private
def helper(*meths)
meths.each do |meth_name|
orig_name = "__helper_#{meth_name.to_s}"
module ScriptHelper
def extra_script(script)
@extra_scripts ||= []
@extra_scripts << script
end
def extra_scripts
if @extra_scripts
scripts = @extra_scripts.map {|script| javascript_include_tag script}

wtf.sh

A simple search for when you find yourself typing wtf on the command line. Works on mac.

Instructions

Put this somewhere in your path, make it executable, and either rename it to wtf, or create a symlink. Then, whenever you type wtf on the command line, you'll actually get something.

@davidjbeveridge
davidjbeveridge / application_controller.rb
Created November 14, 2011 17:10
My application controller
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user_session, :current_user
before_filter :ensure_domain
before_filter :production_down
APP_DOMAIN = 'davidbeveridge.net'
protected
module MyModule
module ClassMethods
# Class methods go here
end
module InstanceMethods
# Instance methods go here
end
def self.included(receiver)