This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
def bar | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Author | |
def invoke_inside(method, *args) | |
self.send(method, *args) | |
end | |
end | |
author = Article.first.author | |
def author.foobar | |
"foobar" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# => 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
OlderNewer