Vous êtes sur la page 1sur 23

Custom Rails Helpers

K e e p i n g Yo u r V i e w s D RY

G l e n n Va n d e r b u r g glv@vanderburg.org

My Favorite Thing About Rails


Rails is like an instructional laboratory for building good software.

Think About It
A new Rails project gives you:
A place for everything, and everything in its place; Help creating test data; Help writing tests; rake stats to give you feedback; Automation for all the mundane stuff; A Rakele with all the targets that you shouldve had on every project you ever worked on (but didnt); A starting-point application with no broken windows.

Rails Programmers Respond to This!


Most Rails applications Ive seen are pretty well designed.
And an amazing number are well tested. Even when the teams are not that experienced.

Sure, there are always some issues. But for the most part, things are good. Except for the views.

Why Are Views a Problem?


Some guesses: The mix of languages (HTML, Ruby, JavaScript) Refactoring that mix is hard.
HTML isnt usually very well structured. We dont have as much experience with it.

Isnt that normal? (Its not just Rails every web framework wrestles with this problem.)

Also
Helpers arent very well understood. They can do several different kinds of things. Making them work like the built-in helpers takes practice. Writing them involves parts of Rails that arent used much in the rest of your application.

Learning to Build Helpers Is Crucial!


Simple HTML builders, form builders, error handling, Ajax links, Prototype wrappers A well-designed Rails app needs a lot of custom helpers. Thats what this talk is about.

Simple HTML Helpers

How Not To Do It

This is Better

What About This?

The Best Way?


It depends on what you're doing. There are a lot of helpers that are most useful for building other helpers:
tag, content_tag, url_for, Great for small stuff.

Larger amounts?
Build strings using <<EOS, %{}, StringIO Use Builder::XmlMarkup render :partial =>

Reusability
Don't overdesign to make something reusable. On the other hand, taking HTML options is easy and goes a long way.

Generating JavaScript

How Not To Do It

This Is Better

What About This?

JavaScript Helpers
Rails Recipes, Recipe 2.
Buy it, read it, use it.

When existing helpers can generate JavaScript for you, use them.
Even from within other helpers.

Use application.js
Included automatically with javascript_include_tag :defaults

Other JavaScript include les included with your layout.

Learn JavaScript!
JavaScript is a fun language, if you know it well. Use it as it was meant to be used.
Use objects. Put functions in objects to keep the global namespace clean.

Learn Prototype!
Prototype has lots of cool stuff to make your JavaScript better. And it's documented now. :-)

Form Builders

Form Builders
Designed to encapsulate form styles that are used across your application. Form elds usually come with labels and a bunch of style info. They might be in lists or table cells or special divs. It's a mess.

What Is a Form Builder?


It's a class that encapsulates a bunch of helpers.

A Complex Helper

Demo

What That Looks Like

The Version With a Complex Form

What the Helper Generates

A big gob of HTML. A fair bit of JavaScript support.

HTML Required
id_eld id_submit_button id_busy busyid

add_trigger_for_id

id_eld_autocomplete

JavaScript Required
Show the form and hide the link when the link is clicked. Submit an autocomplete request when keys are pressed. Show/hide autocomplete status image. Submit add request. Show/hide Adding status message. Add new item to the list when request returns. Highlight it to show the change.

Lets Separate Those Things


Build the HTML in our helper. Put the JavaScript in application.js
It may not stay there, but it's a good start.

Helpers Are Real Code!


Keep them well factored. Document them. Test them!

Tips (Review)

Strive
Be intolerant of messiness and duplication in your views. When you put logic in views, build helpers.
Anything more than simple conditionals

When you see duplication in views, build helpers.


HTML Options to helpers JavaScript

The best way to learn is to do.

Keep Languages Separate


Views are messy because different languages mix together. Dont make the same mistake in your helpers. Prefer generating HTML with other helpers
Rather than building strings. If generating a lot of HTML, use an inline template or a partial.

Put JavaScript in .js les, or in its own helpers.

Follow Conventions
The built-in helpers have strong conventions for parameters and options. Its easy to follow those conventions in your own helpers. Your teammates (current and future) will thank you.

Start Local, Think Global


Put new helpers in current_controller_helper.rb rst. Move them to application_helper.rb if they turn out to be more widely useful. Think about making a plugin if you use them on multiple projects.

Peek Behind the Curtain


You always need to understand the next level of abstraction.
tag, content_tag, text_eld_tag, etc. capture and concat array_or_string_for_javascript, escape_javascript, options_for_javascript observe_eld, observe_form, link_to_if, link_to_unless, url_for Prototype calls like Form.focusFirstElement(form)

Stand on Shoulders
How are the built-in helpers implemented? Do you know? Why not? Click the show source link.

Vous aimerez peut-être aussi