27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Classes outside your control<br />

Forem::TopicsController and Forem::PostsController so that RSpec doesn’t barf<br />

when you run your tests. There are also the helpers in spec/helpers and the models in<br />

spec/models, which need to undergo similar changes.<br />

Did you break anything? Let’s find out with bin/rspec spec:<br />

2 examples, 0 failures<br />

It doesn’t look like it, and that’s a good thing! Let’s commit this change to provide<br />

yourself with a nice little checkpoint:<br />

git add .<br />

git commit -m "Added the ability to reply to posts"<br />

Users of your engine are now able to add replies to topics created by themselves or<br />

other users.<br />

You’ve got a nice couple of features going for your engine, but they’re very inclusive.<br />

You haven’t yet seen how to use the application’s User model to add authorship<br />

to your topics and posts, nor any way of integrating this engine with your Ticketee<br />

application. But never fear! That’s what the next two sections are for.<br />

17.7 Classes outside your control<br />

When creating an engine such as this one, you may want to rely on classes from the<br />

application. To relate posts and topics to users within the application, you could do<br />

this in the models:<br />

belongs_to :user<br />

But what if the concept of users in the application isn’t kept within a User model at<br />

all? Then this would break. You could store a couple of common model names, such<br />

as User, Person, or Account and check for those, but that’s prone to breakage as well.<br />

Here we’ll cover the theory behind configuration options, which you can use to let<br />

your engine know the application’s User model.<br />

17.7.1 Engine configuration<br />

Within this engine you’re going to want to reference the User model from your application<br />

so that you are able to attribute posts and topics to whomever has created<br />

them. However, there’s a catch: within the application, the model that refers to the<br />

user in the system may not be called User! Therefore, you’re going to need to create a<br />

way to inform the engine of what this class is called from within the application.<br />

The best way to inform the engine about the User model would be to have a file<br />

called config/initializers/forem.rb which will run when the application loads like all<br />

other initializers. This file would contain a single line, which would tell the engine<br />

what class represents users within this application, like this:<br />

Forem::Engine.user_class = User<br />

This configuration setting will then be maintained by your engine across requests, and<br />

you’ll be able to reference Forem::Engine.user_class wherever you need it within<br />

497

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

Saved successfully!

Ooh no, something went wrong!