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 />

It’s time this method moved out of your imagination and into your engine. This<br />

method needs to return the last post for your topic, and because the topic_id lives on<br />

the posts table and you’re wanting only one of these posts, this is a great opportunity<br />

to use a has_one association.<br />

This association needs to find the chronological last post for your topics; you can<br />

do this by defining this association in app/models/forem/topic.rb like this:<br />

has_one :last_post, :class_name => "Forem::Post",<br />

:order => "created_at DESC"<br />

The class_name option you’ve used before; it tells Rails that objects of this association<br />

are of that class, rather than the inferred class, which is a constantized version of the<br />

association name. In this case, that would be LastPost. The :order option, however,<br />

will order the posts by the created_at field in reverse chronological order. The<br />

has_one method will then execute this query and limit the results to one, returning<br />

only the first post.<br />

With the association defined, you can re-run your test and see it passing:<br />

1 example, 0 failures<br />

You’ve now got a test that covers that a user can see the posts counter and the last post<br />

information on the topics page for a topic, which proves that this feature is working.<br />

Let’s run all the RSpec tests to make sure that everything’s working with bin/rspec<br />

spec:<br />

6 examples, 0 failures<br />

Great, everything’s good. Time for a commit:<br />

git add .<br />

git commit -m "Add last post information to topics index"<br />

This has been quite a long section and we’ve covered a lot of ground. The purpose of<br />

this section was to demonstrate how you could reference classes outside your control,<br />

such as those in the application or found in other engines. We opted for configuration<br />

options for some of the options, and a module for others.<br />

You then asked users to authenticate before they could create new topics and<br />

posts, and after that you linked your engine’s classes’ objects to the objects from an<br />

application’s class. This is kind of a big deal, as it shows that an engine is able to interact<br />

with an application without extravagant modification of the application’s code.<br />

Finally, you fixed up your topics index view to display information from these<br />

linked-in classes.<br />

This has been only the beginning of linking your application and engine. In the<br />

final sections of this chapter, you’ll see how you can release your engine as a gem and<br />

how you can integrate it into your application so that the users of Ticketee have a<br />

forum system they can use.<br />

511

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

Saved successfully!

Ooh no, something went wrong!