Misc Tips

before filter

If you wish to use an action before filter but want it to be used on all the actions but one, you will need to use the exclude option:

before :authenticate, :exclude => :new

you can also specify a limited list of actions that will use the filter:

before :authenticate, :only => [:create, :update, :destroy, :new]

Note that Rails uses

:except => :new
while Merb uses
:exclude => :new

Before Filter with Arguments

You can also pass arguments to your before filter method using :with => [], like this:

Your method defined elsewhere

def foo(bar)
  bar == "baz" 
end
Your Controller

before :foo, :with => "baz" 

Before Filter Conditions

You can pass a condition which must be satisfied before your filter will run using :if and :unless :


before :authenticate, :unless => :logged_in?

The condition can be a Symbol representing a message to send or a Proc which will be called.

use_test dependencies

If your tests require a gem that is not required in your main application code, it’s best not to include the gem when running in the production environment. use_test accepts optional dependency arguments that will only be included in the test environment:
use_test :rspec, "foo_fixtures"    # dependency "foo_fixtures"

how to freeze your Merb app

For more info read this blog post

Also of note, if you generated an app before the new style of merb-freezer, you’ll want to remove the freeze code inside your Rakefile. Otherwise you’ll get errors like “passing in 3 arguments for 2!”

how to write a Merb plugin

TODO: add documentation link. To generate a new plugin:
merb-gen plugin merb_awesome

Rails-style Initializers

Sometimes you’ll want to initialize a few things when your application starts. If you’re a neat freak, Rails-style initializers may be of use to you. It’s very simple to set this up in Merb:

  1. Create a directory in your config folder named ‘initializers’
  2. Add the following code to the bottom of your config/init.rb file:

  Dir[Merb.root / 'config' / 'initializers' / '*.rb'] do |initializer|
    load(initializer)
  end

You’re done! It’s easy, and it keeps things clean.

Custom Rake task

Add your custom rake task to:
rakefile
at the root of your Merb application

Auto-Reload a very-flat app

Add this to your file.

Merb::BootLoader.before_app_loads do
  Merb.push_path(:application, Merb.root, File.basename(__FILE__))
end

Edit Page | History | Version: