27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Classes outside your control<br />

Once you set this configuration option to the User class and attempt to reference<br />

Forem::Engine.user_class again, you assert that it should not raise that<br />

exception C.<br />

When you run this test using bin/rspec spec/configuration_spec.rb, it will fail<br />

with this error:<br />

uninitialized constant Forem::ConfigurationNotSet<br />

This is because your spec is attempting to reference the exception class before you’ve<br />

even defined it! No problem though, you can define this very easily within the lib/<br />

forem.rb file by using this code:<br />

class ConfigurationNotSet < StandardError<br />

end<br />

The StandardError class is used for custom errors within Ruby and serves as a great<br />

base for this exception. If you run bin/rspec spec/configuration_spec.rb, you’ll<br />

see this:<br />

expected Forem::ConfigurationNotSet with<br />

"[error]" but nothing was raised<br />

When you attempt to grab the user_class setting in your test, it’s not raising this<br />

exception when it should. To fix this, you’ll need to redefine the user_class method<br />

on the Forem::Engine class by putting this code underneath the attr_accessor line<br />

in lib/forem/engine.rb:<br />

def self.user_class<br />

error = "Please define Forem::Engine.user_class" +<br />

" in config/initializers/forem.rb"<br />

@user || raise(ConfigurationNotFound, error)<br />

end<br />

Previously, the user_class method would have returned the @user variable whether<br />

or not it was set. In this method, you now define the message that will be shown if this<br />

class variable is not set. After that, if the class variable is set then it will be returned by<br />

this method, and if not then the ConfigurationNotFound exception will be raised,<br />

which seems to be all the criteria needed for your test to pass. Let’s find out by running<br />

bin/rspec spec/configuration_spec.rb now:<br />

1 example, 0 failures<br />

Great! That’s all passing. You’ve now got a class-level user_class method that you can<br />

set up in any applications that use this engine, so that you can notify the engine of the<br />

class that represents users within the application. If this setting is not set by the application<br />

by the time the engine gets around to referencing it, then the Configuration-<br />

NotFound exception will be raised, informing the owner of the application that they<br />

need to set this variable up in config/initializers/forem.rb.<br />

Let’s now set up the User model within your dummy application so that you can<br />

use this setting.<br />

499

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!