Post on 05-Apr-2018
7/31/2019 Custom Rails Helpers
1/23
Rails is like an instructional
laboratory for buildinggood software.
7/31/2019 Custom Rails Helpers
2/23
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 Rakefile 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.
Most Rails applications Ive seen are pretty welldesigned.
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.
7/31/2019 Custom Rails Helpers
3/23
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 frameworkwrestles with this problem.)
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 arentused much in the rest of your application.
7/31/2019 Custom Rails Helpers
4/23
Simple HTML builders, form builders, errorhandling, Ajax links, Prototype wrappers
A well-designed Rails app needs a lot of customhelpers.
Thats what this talk is about.
7/31/2019 Custom Rails Helpers
5/23
7/31/2019 Custom Rails Helpers
6/23
It depends on what you're doing.
There are a lot of helpers that are most useful forbuilding other helpers:
tag, content_tag, url_for,
Great for small stuff.
Larger amounts?
Build strings using
7/31/2019 Custom Rails Helpers
7/23
Don't overdesign to make something reusable.
On the other hand, taking HTML options is easyand goes a long way.
7/31/2019 Custom Rails Helpers
8/23
7/31/2019 Custom Rails Helpers
9/23
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 withjavascript_include_tag :defaults
Other JavaScript include files included with yourlayout.
7/31/2019 Custom Rails Helpers
10/23
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 namespaceclean.
Learn Prototype!
Prototype has lots of cool stuff to make your JavaScript
better.And it's documented now. :-)
7/31/2019 Custom Rails Helpers
11/23
Designed to encapsulate form styles that areused across your application.
Form fields usually come with labels and abunch of style info.
They might be in lists or table cells or specialdivs.
It's a mess.
7/31/2019 Custom Rails Helpers
12/23
It's a class that encapsulates a bunch of helpers.
7/31/2019 Custom Rails Helpers
13/23
7/31/2019 Custom Rails Helpers
14/23
A big gob of HTML.
A fair bit of JavaScript support.
7/31/2019 Custom Rails Helpers
15/23
id_field id_submit_button id_busy
busyid
id_field_autocompleteadd_trigger_for_id
Show the form and hide the link when the link isclicked.
Submit an autocomplete request when keys arepressed.
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.
7/31/2019 Custom Rails Helpers
16/23
Build the HTML in our helper.
Put the JavaScript in application.js
It may not stay there, but it's a good start.
Keep them well factored.
Document them.Test them!
7/31/2019 Custom Rails Helpers
17/23
7/31/2019 Custom Rails Helpers
18/23
7/31/2019 Custom Rails Helpers
19/23
7/31/2019 Custom Rails Helpers
20/23
Be intolerant of messiness and duplication inyour 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.
7/31/2019 Custom Rails Helpers
21/23
Views are messy because different languages mixtogether.
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 ora partial.
Put JavaScript in .js files, or in its own helpers.
The built-in helpers have strong conventions forparameters and options.
Its easy to follow those conventions in your own
helpers.
Your teammates (current and future) will thankyou.
7/31/2019 Custom Rails Helpers
22/23
Put new helpers in current_controller_helper.rbfirst.
Move them to application_helper.rb if they turnout to be more widely useful.
Think about making a plugin if you use them onmultiple projects.
You always need to understand the next level ofabstraction.
tag, content_tag, text_field_tag, etc.
capture and concat
array_or_string_for_javascript, escape_javascript,options_for_javascript
observe_field, observe_form,
link_to_if, link_to_unless, url_for
Prototype calls like Form.focusFirstElement(form)
7/31/2019 Custom Rails Helpers
23/23
How are the built-in helpers implemented?
Do you know?
Why not?
Click the show source link.