We recently enhanced Clearance with a configuration block that could be used in config/initializers/clearance.rb.
I liked the way Airbrake did it and wanted to implement the same pattern:
Airbrake.configure do |config|
config.api_key = 'your_key_here'
end
Here’s the implementation:
module Clearance
class << self
attr_accessor :configuration
end
def self.configure
self.configuration ||= Configuration.new
yield(configuration)
end
class Configuration
attr_accessor :mailer_sender
def initialize
@mailer_sender = 'donotreply@example.com'
end
end
end
The configure class method stores a Configuration object inside the Clearance module.
Anything set from the configure block is an attr_accessor on the Configuration class.
So now config/initializers/clearance.rb is possible:
Clearance.configure do |config|
config.mailer_sender = 'donotreply@example.com'
end
Easy for the application developer to understand. Clean internal implementation:
Clearance.configuration.mailer_sender
In the library’s tests, set configuration attributes without worrying about undefining constants:
Clearance.configuration.mailer_sender= 'new.email@example.com'
Yay.

Written by <a rel=”author” href=”https://profiles.google.com/115463449660844194795”>Dan Croak.
Authentication is a common pattern in Rails apps. Thus, there have been many authentication plugins. We’ve tried acts_as_authenticated and restful_authentication over the years.
We found that user authentication is hard to generalize. Most abstracted authentication plugins had both too much and too little for us.
We then tried writing authentication from scratch on our clients’ Rails apps for about a year. We felt better about test coverage but it was still a pain to re-write similar code. App after app, we talked about extracting common code into a library. Each time we resisted.
After a while, we thought that maybe 60% of authentication could be re-used. We extracted Hoptoad’s authentication, then merged code from two of our clients’ apps. We named the gem Clearance.
On the first attempt, we went overboard on re-use. We backed off and wrote hooks in places we were finding logical extension points. For the past few months, patches have trickled in from Github and we’ve carefully included code that fits in the “60%”.
We recently started a new project. In the process, we’ve polished the gem and are happy to announce its official release.
Get it on Github.
Clearance is focused on maintainability of your application’s authentication code.
This approach keeps your Rails application’s code clean and alerts you if you ever break your authentication code.
Due to the work we’ve been doing to make Shoulda test framework-agnostic, you will be able to use RSpec in the 0.5.0 release of Clearance.
Test::Unit and Cucumber features are also supported:
script/generate clearance
script/generate clearance_features
To keep our approach simple, we made a series of design decisions:
Clearance does not try to be a Swiss Army knife but it does have some hooks if you want admin roles, sign up and sign in by username in addition to email, or something else.
Please report bugs and request features on GitHub Issues.
Written by Dan Croak.
Lots of shoulda news to report. The 2.0.x gem has been released, which includes bug fixes, new features, and deprecations.
The gem is being hosted at github.com/thoughtbot/shoulda
Specify the gem dependency in your config/environment.rb file:Rails::Initializer.run do |config|
config.gem 'thoughtbot-shoulda', :lib => 'shoulda/rails', :source => "http://gems.github.com"
end
Then:
rake gems:install
rake gems:unpack
Listen… if you’re still a plugin, I look at you (I’m looking at you, Paperclip), shake my head, and say, “So much untapped potential. Could’ve been big.”
Why gems?
Ryan McGeary and Josh Nichols did the bulk of this conversion and related refactoring. They deserve extra acclaim because they’ve made it easier for everyone to contribute to shoulda.
should_assign_to :user, :equals => "@user"
should_assign_to also now takes a :class option. This is especially good for new actions.
should_assign_to :user, :class => User
should_have_one now supports the :dependent option.
should_have_one :address, :dependent => :destroy
ActiveRecord::Errors.default_error_messages is used instead of home-brewed regexes for all Active Record macros.
context "Creating a post"
setup { Post.create }
should_change "Post.count", :by => 1
end
# :from and :to examples
should_change "Post.count", :from => 0, :to => 1
should_change "@post.title", :from => "old", :to => "new"
should_not_change creates a test asserting no change between the return value of an expression that is run before and after the current setup block is run.
context "Updating a post"
setup { @post.update_attributes(:title => "new") }
should_not_change "Post.count"
end
should_filter_params creates a test asserting that filter_parameter_logging is set for the specified keys
should_filter_params :password, :ssn
should_render_with_layout creates a test asserting that the controller rendered with the given layout.
should_render_with_layout # defaults to application
should_render_with_layout 'special'
should_render_without_layout
should_route creates a routing test. It tries to use the given HTTP method on the given path, and asserts that it routes to the given options.
should_route :get, "/posts", :controller => :posts, :action => :index
should_route :delete, "/posts/1", :action => :destroy, :id => 1
should_route :get, "/users/1/posts/1",
:action => :show, :id => 1, :user_id => 1
should_respond_with_content_type creates a test asserting that the response content type was ‘content_type’.
should_respond_with_content_type 'application/rss+xml'
should_respond_with_content_type :rss
should_respond_with_content_type /rss/
should_return_from_session creates a test asserting that a value returned from the session is correct. The given string is evaled to produce the resulting redirect path. All of the instance variables set by the controller are available to the evaled string.
should_return_from_session :user_id, "@user.id"
class UserControllerTest < Test::Unit::TestCase
context "the index action" do
setup do
@users = [Factory(:user)]
User.stubs(:find).returns(@users)
end
context "on GET" do
setup { get :index }
should_respond_with :success
# runs before "get :index"
before_should "find all users" do
User.expects(:find).with(:all).returns(@users)
end
end
end
end
Writing custom macros is a common practice for The Modern Shoulda Developer. For example, should_have_attached_file used to be a handy custom macro to have around when you’re using Paperclip.
There were a few problems with that, however. Where should that macro go? RAILS_ROOT/test/test_helper.rb? Why do we have the same macro in multiple apps for the same plugin?
Enter the new shoulda_macros directory. Shoulda will automatically load custom macros she finds in:
Now, every Paperclip user can use the latest should_have_attached_file because it is where it belongs.
should_be_restful will be removed in a future release. It is currently in a grace period where you’ll see a warning during your test runs. It will be added to the new woulda gem if you care to keep using it. Its problems:
Ryan McGeary, Matt Jankowski, Mike Boone, Tammer Saleh, Josh Nichols, Joe Ferris, Keith Morrison, Me