Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
class Foo
def bar
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)
# 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
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
class Author
def invoke_inside(method, *args)
self.send(method, *args)
end
end
author = Article.first.author
def author.foobar
"foobar"
end
# 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")
# => SELECT "posts".* FROM "posts" ORDER BY published DESC, id DESC LIMIT 1
Post.send(:with_scope, {:find => {:order => "id DESC"}}) do
Post.first(:order => "published DESC")
end
-- Order by one indexed column (FAST)
newsdesk_production=# explain analyze select * from pressreleases order by published_at DESC limit 100;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..249.91 rows=100 width=1207) (actual time=26.070..716.453 rows=100 loops=1)
-> Index Scan Backward using pressreleases_published_at_index on pressreleases (cost=0.00..964766.62 rows=386042 width=1207) (actual time=26.067..716.343 rows=100 loops=1)
Total runtime: 716.709 ms
(3 rows)
- Order by two separately indexed columns (SLOW)
# Notes on migrating from the RSpec builtin mocking framework to the Mocha framework
# 1. Comment in this line in spec_helper.rb:
config.mock_with :mocha
# 2. Specify your gem dependency (optional) in config/environments/test.rb:
config.gem "mocha", :version => "0.9.8"
# 3. Use query-replace to do most but not all of the DSL conversion
# should_receive -> expects
@peter
peter / gist:658203
Created November 1, 2010 13:56
Avoid Stubbing Non-Existent Methods with Mocha
# In spec_helper.rb or test_helper.rb:
Mocha::Configuration.prevent(:stubbing_non_existent_method)
module MochaExtensions
module ObjectMethods
def expects_message(*args, &block)
Mocha::Configuration.allow(:stubbing_non_existent_method) { expects(*args, &block) }
end
def stubs_message(*args, &block)