Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
# This patch gives us an easy way to globally disable/enable the cache
Rails.cache.class.class_eval do
attr_accessor :enabled
def read_with_enabled(*args, &block)
enabled ? read_without_enabled(*args, &block) : nil
end
alias_method_chain :read, :enabled
end
if File.basename($0) == "rake" && ARGV.include?("db:migrate")
class Author
def invoke_inside(method, *args)
self.send(method, *args)
end
end
author = Article.first.author
def author.foobar
"foobar"
end
describe "Mocking/Stubbing Associations with RSpec" do
it "can be done with proxy_target" do
# Suppose the Article model has an after_save callback that invokes notify_change on the author
# if the article has been changed.
@article.author.proxy_target.should_receive(:notify_change)
@article.update_attributes(:body => "new body")
end
end
# In lib/tasks/deploy.rake
namespace :deploy do
def write_error_page(status, locale = nil)
dest_filename = [status.to_s, locale, "html"].compact.join(".")
File.open(File.join(Rails.root, "public", dest_filename), "w") do |file|
path = File.join(Rails.root, "app", "views", "errors", "#{status}.rhtml")
file.print ERB.new(File.read(path)).result
end
end
namespace :db do
# NOTE: In Rails 3 there will be a db:setup task but it only works if ActiveRecord::Base.schema_format == :ruby
# See http://afreshcup.com/2009/05/11/seed-data-in-rails-3
# The following patch fixes db:schema:load to work with schema_format :sql:
# https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2789-make-rake-task-dbschemaload-compatible-with-sql-schema-format
desc 'Recreate the database, load the schema, and initialize with the seed data'
task :super_setup => [ 'db:drop', 'db:create' ] do
system("cat db/development_structure.sql db/development_data.sql | script/dbconsole")
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
load(seed_file) if File.exist?(seed_file)
class Foo
def bar
end
end