Override e-mail recipients in ActionMailer

Our gem Mail Magnet allows you to override e-mail recipients in ActionMailer so all mails go to a given address.

This is useful for staging environments where you want to test production-like mail delivery without sending e-mails to real users.

Use Shoulda's validate_uniqueness_of matcher correctly

This raises "Could not find first Keyword":

describe Keyword do
  it { should validate_uniqueness_of(:text) }
end

Do this instead:

describe Keyword do

  it 'should have a unique #text' do
    Keyword.make
    should validate_uniqueness_of(:text)
  end

end

This is the intended behavior.

Automatically run bundle exec if required

There will probably be better solutions as we become more experienced with using Bundler, and more command line tools become Bundler-aware.

b will use bundle exec if there is a Gemfile in the working directory, and run the call without Bundler otherwise.

b spec spec

This script is part of our geordi gem on github.

Unstage an added file in Git

If you added a file by mistake, you can unstage it (but keep local changes) by saying

git reset HEAD path/to/file

This is also what git status will tell you.

NB: It works for conflicts, too, if git checkout -- <file> does not (although git tells you it would).

Airbrake notification in Rake tasks

Put

def task_with_hoptoad_notification(options)
  task(options) do
    begin
      yield
    rescue Exception => e
      Airbrake.notify(e)
      raise e
    end
  end
end

at the top of the Rakefile, and replace all relevant

task :my_task => :environment do
    do_something
end

with

task_with_hoptoad_notification :my_task => :environment do
    do_something
end

This will use the usual notification rules, i.e. you won't get anything in the development or test environments.
...

Make box shadows look the same in IE and other browsers

The box shadows created rendered in IE by CSS3PIE look darker and are blurred differently than in browsers that render box-shadow natively.

If possible, try to be OK with this. If not, make an IE-only stylesheet that uses a different color and blur radius:

// Real browsers:
+box_shadow("0 4px 10px #bbb")

// IE with PIE:
+box_shadow("0 5px 15px #888")

We should try to package this solution in a neat way so we don't need different stylesheets.

See also this [cross-browser box-shadow comparison]...

Scope to records with a given state in state_machine

The state_machine gem ships with a scope with_state. This scope has some problems in complex queries or scope chains.

Use this instead:

named_scope :having_state, lambda { |*state_or_states|
  state_or_states = Array.wrap(state_or_states).map(&:to_s)
  { :conditions => [ 'articles.state IN (?)', state_or_states ] }
}

If you want a scope with hash options (with the side effects you should know about):

named_scope :having_st...

Bundler for Rails 2.3.x

Update RubyGems and Passenger

Bundler requires Rubygems >= 1.3.6. Run gem update --system if you have an older version.
It also is not compatible with older versions of passenger, so bring that up to date as well (2.2.15 works).

If you installed RubyGems through apt (which you should never do!), you may see a message giving you a hint to use apt to update.
Some people advise to install the 'rubygems-update-1.3.7' gem on Ubuntu systems if you used apt to install RubyGems.
I did that - and lost all...

Disable Rails XSS protection in ActionMailer views

This might eventually be fixed by Rails itself.\
Right now this is the way to have the rails_xss plugin not escape the body of ActionMailer mails.

Put this into config/initializers/mailers_without_rails_xss.rb:

Use the same template for multiple ActionMailer actions

One option is to use partials. Or you can set the @template field to the name of another action:

class Mailer < ActionMailer::Base
  def foo
    subject "Hello World"
  end
  
  def bar
    subject "Hello Universe"
    @template = 'foo'
  end
end

Recursively remove unnecessary executable-flags

Sometimes files attain executable-flags that they do not need, e.g. when your Windows VM copies them over a Samba share onto your machine.

From inside your Rails project directory call regularly:

geordi remove-executable-flags

Runs chmod -x on Ruby, HTML, CSS, image, Rake and similar files.


This script is part of our geordi gem on github.

Hide the last bottom margin in a container

When you create e.g. a sidebar box that contains headlines and paragraphs, the final paragraph's margin in that box will create an undesired 'bottom padding' inside that box.

Here is a Sass mixin that you can apply to such boxes. It makes the last child's bottom margin disappear:

=hide_last_margin
  >*:last-child
    margin-bottom: 0

Use it like this:

.sidebar_box
  p, table, ul
    margin-bottom: 1em
  +hide_last_margin

Internet Explorer (fix)

Does not work in versions of <IE8

Your best bet is to expli...

Linux: create a symbolic link

You may omit the /path/to/link_name to have a link with the same filename appear in the current directory

ln -s /path/to/file /path/to/link_name

unlink link_name       // to remove the link and not where it is pointing at