Skip to content

Instantly share code, notes, and snippets.

View ericcf's full-sized avatar

Eric Carty-Fickes ericcf

  • Northwestern University
  • Chicago, IL
View GitHub Profile
MyApplication::Application.configure do
config.ingredient_api_url = "http://example.com"
end
class Ingredient < ActiveResource::Base
self.site = MyApplication::Application.config.ingredient_api_url
self.format = :json
end
Rails.application.routes.draw do
resources :ingredients, :only => [:index, :show]
end
class IngredientsController < ApplicationController
respond_to :json
def index
respond_with Ingredient.all
end
def show
respond_with Ingredient.find(params[:id])
class Ingredient < ActiveRecord::Base
end
class IngredientsController < ApplicationController
caches_action :index, :show
cache_sweeper :ingredients_sweeper
end
class IngredientsSweeper < ActionController::Caching::Sweeper
observe Ingredient
def after_save(record)
expire_action :controller => "ingredients",
:action => "index",
:format => :json
expire_action :controller => "ingredients",
:action => "show",
@ericcf
ericcf / init_fakeweb.rb
Created June 22, 2011 15:10
FakeWeb setup for testing
require 'fakeweb'
# this can also be set to a regular expression to whitelist URI's
FakeWeb.allow_net_connect = false
def load_fixture(name)
path = "#{Rails.root}/features/fixtures/#{name}.rb"
eval File.open(path).map { |line| line }.join("")
end
@ericcf
ericcf / ingredients.rb
Created June 22, 2011 15:14
example fixture
[
{
"id" => 1,
"name" => "sassafras"
}
]
@ericcf
ericcf / serialization.rb
Created June 22, 2011 15:15
don't include root in json
# instead of { "my_model": { "id": 1, "title": "foo" } }
# to_json produces { "id": 1, "title": "foo" }
ActiveRecord::Base.include_root_in_json = false