Skip to content

Instantly share code, notes, and snippets.

@Peeja
Peeja / stub_request.rb
Created December 19, 2008 20:01
stub_request: Catch requests to other domains in an integration test. Could use some features, but it's good enough for me right now.
class ActionController::Integration::Session
# Intercepts a request to a foreign domain. Use this to stub
# a service which the user is bounced through, such as an
# OpenID provider. The block should return a new URL to
# request. This is the URL which the foreign service would
# redirect the browser to if we were really using it.
#
# Currently, the return URL can only be requested with a GET.
#
# stub_request 'foreign.host.com' do |path|
@Peeja
Peeja / gist:55599
Created January 31, 2009 17:16
clearfix as a Sass mixin (saved for future reference)
# From http://wonderfullyflawed.com/2008/05/21/clearfix-as-mixin/
=clearfix
*display: inline-block
&:after
content: " "
display: block
height: 0
clear: both
visibility: hidden
# How does that session value get set!?
require 'metaid'
@response.session.metaclass.class_eval do
def []=(k,v)
puts "Setting session[#{k.inspect}] = #{v.inspect} (#{caller[0]})"
super
end
end
require 'osx/cocoa'
include OSX
things_path = NSWorkspace.sharedWorkspace.fullPathForApplication("Things")
things_bundle = NSBundle.bundleWithPath(things_path)
NSManagedObjectModel.mergedModelFromBundles([things_bundle]).entities.each { |e| puts e.name }
# >> Record
# >> SyncedTask
# >> MobileSyncItem
# >> Focus
@Peeja
Peeja / user.rb
Created March 5, 2009 17:55
Enforced abstract class in Ruby. Handy for refactoring.
class User < ActiveRecord::Base
# This is begging to be a plugin, or even a gem.
def self.new(*_)
raise "Code is an abstract class and cannot be instantiated" if self == User
super
end
# ...
end
@Peeja
Peeja / bindings.rb
Created June 12, 2009 21:00
Using bindings instead of alias_method_chain.
require 'rubygems'
require 'activesupport'
# Let's say there's some Widget class.
class Widget
def sproing
[:original]
end
end
@importer....should contain.blahblahs.that { should include("foo") and should_not include("bar") }
named_scope :valid_for_state_and_user, lambda { |state, user| lambda { |transition|
transition.from_state_id == state.id &&
!transition.protected? or user.role.can_apply_protected_note_transitions?
}}
# Dan Chak, author of O'Reilly's "Enterprise Rails" thinks that Ruby has inner methods.
# See "Enterprise Rails", pg. 140.
class Foo
def bar
"outer bar"
end
def foo
# Dan Chak thinks this can only be called from within #foo.
module Curryable
# Quirk: #curry on a nullary function calls the function.
def curry(args = [])
if self.arity == args.length
self[*args]
else
lambda do |next_arg|
self.curry(args + [next_arg])
end
end