Skip to content

Instantly share code, notes, and snippets.

View brettfishman's full-sized avatar

Brett Fishman brettfishman

  • Stitch Fix
  • San Francisco, CA
View GitHub Profile
@brettfishman
brettfishman / gist:5251931
Created March 27, 2013 05:31
Rails 3.2 autoload_paths mystery
# In application.rb
config.autoload_paths += %W(#{config.root}/lib/oauth_providers)
# In lib I have a directory (oauth_providers) with a file named oauth_provider.rb:
# lib/oauth_providers/oauth_provider.rb
module OauthProviders
class OauthProvider
def self.instantiate(name)
klass = "OauthProviders::#{name}".constantize
klass.new
@brettfishman
brettfishman / gist:3868277
Created October 10, 2012 20:39
Testing HTTP caching using RSpec and CanCan
# Given a controller that looks like this
class ModelsController < ApplicationController
load_and_authorize_resource #CanCan
def show
if stale? @model
respond_to do |format|
format.json do
@model
end
@brettfishman
brettfishman / gist:3056288
Created July 5, 2012 20:41
Issue with ActionController::TemplateAssertions#assert_template
context "template name is substring of actual rendered template" do
it "will pass" do
get :hello_world
# The following line will pass, even though the template rendered is actually "accounts/hello_world"
response.should render_template "accounts/hello"
end
end
@brettfishman
brettfishman / gist:3014703
Created June 28, 2012 23:29
ActiveRecord 3.2.6 JOIN issue when models are backed by tables in an alternate database
# In an initializer under config/initializers
[OtherPerson, OtherGroup].each do |model|
model.establish_connection(other_database[Rails.env].symbolize_keys!)
end
# Model definitions
class OtherGroup < ActiveRecord::Base
self.table_name = 'groups'
has_many :other_persons, :class_name => "OtherPerson", :foreign_key => 'group_id'
end
@brettfishman
brettfishman / gist:2956617
Created June 19, 2012 21:22
Prevent "already initialized constant" warnings when running specs
quietly do
SomeModuleOrClass.send(:remove_const, :SOME_CONSTANT)
SomeModuleOrClass.send(:const_set, :SOME_CONSTANT, "new constant value")
end
@brettfishman
brettfishman / gist:2942832
Created June 16, 2012 23:32
Configuring Nori when upgrading Savon from 0.8.6 to 1.0.0
# Calling #to_hash on the Savon response in earlier version of Savon (0.8.6) return namespace keys formatted
# as such: :type, :ns14, :xsi. But in Savon 1.0.0, #to_hash returns namespace keys like :"@xsi:type",
# :"@xmlns:ns14", and :"@xmlns:xsi". This broke some parsing logic we had set up for the hashed Savon response.
# To change the format of these tags (or any tag, for that matter) in the hashed response,
# you can configure Nori like so:
Nori.configure do |config|
config.convert_tags_to do |tag|
converted_tag = tag[0] == "@" ? (tag.split(":")[1] || tag[1..-1]) : tag