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.

500 CHAPTER 17 Engines<br />

17.7.2 A fake User model<br />

Your engine has been deliberately designed to have no concept of authentication.<br />

This is so it can be used with any application, independent of whatever authentication<br />

system the application uses, be it Devise (which is what your main application uses) or<br />

something else. In this section and the ones following, you’re going to be associating<br />

topics and posts to users so that you know who’s been posting what. In order to this,<br />

you’re going to need to generate a dummy User model.<br />

When you have this model correctly set up, you’ll use it to restrict access to the new<br />

topic and new post actions to only logged-in users, as well as using it to assign ownership<br />

to posts and topics. You’ll be able to do this using the current_user made available<br />

by the host application. It will be accessible in your engine’s controllers, as<br />

Forem::ApplicationController inherits from ApplicationController.<br />

To generate this new User model, you’re going to have to run the generator from<br />

within the spec/dummy directory. This is so the generator will place the model code<br />

within the dummy application and not your engine. You don’t need anything on this<br />

User model besides a login field, which you’ll be using as the display value a little<br />

later on by defining a to_s method inside this class. Within the spec/dummy directory,<br />

run this command:<br />

rails g model user login:string<br />

To run the migration for this model, run rake db:migrate inside the spec/dummy<br />

directory as well.<br />

In this fake model, you’re going to need to define the to_s method your engine<br />

will use to display the user’s name. Right now your users table only has a login field,<br />

and so you’ll return that:<br />

class User < ActiveRecord::Base<br />

def to_s<br />

login<br />

end<br />

end<br />

With the fake model generated and the to_s method defined in it correctly, the only<br />

thing left to do is to set up the initializer in your dummy application. Create a new file<br />

at spec/dummy/config/initializers/forem.rb with this simple line:<br />

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

That is all the preparation you need to do to notify your engine of this class. To make<br />

your engine use this class, you’ll put this line in both the class definitions of app/<br />

models/forem/post.rb and app/models/forem/topic.rb:<br />

belongs_to :user, :class_name => Forem::Engine.user_class.to_s<br />

This line will now reference the setting that’s configured by config/initializers/<br />

forem.rb, thereby relating your posts and topics from your engine to the User class<br />

within the application, forming a lovely bridge between the two.

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

Saved successfully!

Ooh no, something went wrong!