Skip to content
This repository
Browse code

[engines guide] Complete section about using an application's class

  • Loading branch information...
commit 5bf0ea6830f9148f6b45d2774bfd7f54eec4a267 1 parent 4341480
Ryan Bigg radar authored

Showing 1 changed file with 26 additions and 5 deletions. Show diff stats Hide diff stats

  1. +26 5 railties/guides/source/engines.textile
31 railties/guides/source/engines.textile
Source Rendered
@@ -404,7 +404,7 @@ The first timestamp (+\[timestamp_1\]+) will be the current time and the second
404 404
405 405 To run these migrations within the context of the application, simply run +rake db:migrate+. When accessing the engine through +http://localhost:3000/blog+, the posts will be empty. This is because the table created inside the application is different from the one created within the engine. Go ahead, play around with the newly mounted engine. You'll find that it's the same as when it was only an engine.
406 406
407   -h4. Using an application's classes
  407 +h4. Using a class provided by the application
408 408
409 409 When an engine is created, it may want to use specific classes from an application to provide links between the pieces of the engine and the pieces of the application. In the case of the +blorgh+ engine, making posts and comments have authors would make a lot of sense.
410 410
@@ -416,17 +416,38 @@ To keep it simple in this case, the application will have a class called +User+
416 416 rails g model user name:string
417 417 </shell>
418 418
419   -Also to keep it simple, the posts form will have a new text field called +author+ where users can elect to put their name. The engine will then take this name and create a new +User+ object from it or find one that already has that name, and then associate the post with it.
  419 +Also to keep it simple, the posts form will have a new text field called +author_name_+ where users can elect to put their name. The engine will then take this name and create a new +User+ object from it or find one that already has that name, and then associate the post with it.
420 420
421   -First, the +author+ text field needs to be added to the +app/views/blorgh/posts/_form.html.erb+ partial inside the engine. This can be added above the +title+ field with this code:
  421 +First, the +author_name+ text field needs to be added to the +app/views/blorgh/posts/_form.html.erb+ partial inside the engine. This can be added above the +title+ field with this code:
422 422
423 423 <erb>
424 424 <div class="field">
425   - <%= f.label :author %><br />
426   - <%= f.text_field :author %>
  425 + <%= f.label :author_name %><br />
  426 + <%= f.text_field :author_name %>
427 427 </div>
428 428 </erb>
429 429
  430 +The +Blorgh::Post+ model should then have some code to convert the +author_name+ field into an actual +User+ object and associate it as that post's +author+ before the post is saved. It will also need to have an +attr_accessor+ setup for this field so that the setter and getter methods are defined for it.
  431 +
  432 +To do all this, you'll need to add the +attr_accessor+ for +author_name+, the association for the author and the +before_save+ call into +app/models/blorgh/post.rb+. The +author+ association will be hard-coded to the +User+ class for the time being.
  433 +
  434 +<ruby>
  435 +attr_accessor :author_name
  436 +belongs_to :author, :class_name => "User"
  437 +
  438 +before_save :set_author
  439 +
  440 +private
  441 + def set_author
  442 + self.author = User.find_or_create_by_name(author_name)
  443 + end
  444 +</ruby>
  445 +
  446 +By defining that the +author+ association's object is represented by the +User+ class a link is established between the engine and the application. Now just before a post is saved it will be associated with a record from the +users+ table of the application.
  447 +
  448 +h4. Configuring an engine
  449 +
  450 +The next step is to make the class that represents a +User+ in the application customizable for the engine. This is because, as explained before, that class may not always be +User+.
430 451
431 452 h3. Overriding engine functionality
432 453

0 comments on commit 5bf0ea6

Please sign in to comment.
Something went wrong with that request. Please try again.