Prevent ActionMailer from sending to deleted users / blacklisted addresses

No need to check in normal code, just do everything as usual and this interceptor will prevent you from spamming people who do not want any emails.
Its not perfect(missing bc/bcc filters but if should be fine for 90% of cases)

#config/initializers/blacklisted_emails.rb
class MailInterceptor
  def self.delivering_email(message)
    if User.where(:email => message.to, :receive_emails => false).any?
      message.perform_deliveries = false
    end
  end
end

Mail.register_interceptor(MailInterceptor)

You dont need RSpec in :development

You can speed up development load time and get rid of rspec in development if you simply not require it in :development and just load the tasks in the Rakefile where they are needed.

# Gemfile
group :development, :test do
  gem 'rspec-rails', :require => false
end

# Rakefile
require File.expand_path('../config/application', __FILE__)
require 'rspec-rails' if ['test', 'development'].include?(Rails.env)
...

Repeat for any other testing lib for even more speedup 🙂

Running tests via spork directly from Rubymine

Very fast test execution from inside Rubymine.
Cmd+F8 + 2 seconds == test results 😀

1 Activate DRB in Rubymine
Run > Edit configurations > Defaults > RSpec > Use DRB Server

2 a) Start spork from Rubymine
Tools > Run Spork DRB Server

2 b) (alternatively) start spork from the commandline
Code

  # spec/spec_helper.rb
Spork.prefork do
  if ENV["RUBYMINE_HOME"]
    puts "Rubymine support"
    $:.unshift(File.expand_path("rb/testing/patch/common", ENV["RUBYMINE_HOME"]))
    $:.unshift(File.expand_path("rb/testing/patch/bdd", ENV["RUBYMINE_HOME"]))
  end
end

and run it via:

RUBYMINE_HOME=/Applications/Rubymine\ 3.2.4.app/ spork

Ruby Money vs nil — aka undefined method `round’ for nil:NilClass

Problem
When using composed_of with Money you get this nice error when trying to get the money object while the cents column is nil.
Additionally Money.new(nil) does not work, neither do strings like Money.new(params[:cents]).

Solution
Overwrite the initializer to also accept nil and strings

# config/initializers/money.rb
require 'money'
class Money
  def initialize_with_default_currency(*args)
    args[0] = args[0].to_i if args[0].is_a?(String) or args[0].blank?
    args[1] ||= Money.default_currency
    initialize_without_default_currency(*args)
  end
  alias_method_chain :initialize, :default_currency
end

Updating to Rails 2.3.11 without using html_safe

Only thing not working as expected is that the h helper did escape html_safe output, but we can fix that easily…

# make h work the same no matter if a string is safe
# ERB::Util.h("
".html_safe).should_not == "
" module ERB::Util def html_escape(s) s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] } end alias h html_escape module_function :h module_function :html_escape end