A little touch of spice / Home

I thought today I would be a little different today and not have a bitchfest but instead reward those that have put up with that dribble (and use rails, bad luck if you don’t).

I present to you a couple of helpers that actually are very handy to have. I don’t know about you but I like to break things down to small and management bits, with code and especially with stylesheets and javascript. There’s no need to have one cosmic-sized stylesheet that even contains the kitchen sink (I hate that pun) which is why I break things down to smaller elements, a file for forms, a file for layout, etc. The problem I quickly discovered was that my layout was including all my stylesheets even when I didn’t need them, a big no-no in my books. So I opted to use content_for but that was just ugly and not the ruby way — NOTE I do believe in the DRY philosophy but I also believe you should not be pedantic about it. My ruby philosophy is if you can fit it on one line and is small and easy to understand then leave it at that. The idea is to keep it readable without having to scroll sideways which is almost DRY :) — So I journeyed forth to find a better solution, in my travels I discovered a neat feature within stylesheet_link_tag and javascript_include_tag. These functions had the uncanny ability to aggregate the files passed to them which is great for performance. Wait wait, wait a minute! But you just said that you hate having a colossal file “that even contains the kitchen sink”. Yes I did say that, what I meant to say is that you should not be the one to create it but its ok if say a script or a remote task does, this way you can be all neat and tidy and not have to look at the mess that something else creates. If you don’t see it then its all good :)

So like I was saying, I created a couple helpers that “help” you add references to your stylesheets and javascript files when your building your templates. How does this work? Easy-peasy. All you do is in your template just call the method and ask for all the files you want to include, then in your layout file you just have to add the files however way you want. Let me demonstrate:

Say I’m working on a form template called new.html.erb and I want to reference my form stylesheet file then with my little helper loaded I would just call append_stylesheets 'form'. This method works the same as stylesheet_path in that you can just pass the name of the file and it’ll figure out the path and extension etc. You can also declare the media type of the stylesheet as so append_stylesheets 'form-print', :media => :print. The default media is all.

So how do we include the stylesheets in our layout then mister? That can be done in many ways, just remember that there may be multiple different stylesheets for different media types so your going to have to be specific in what type your after. You can include your stylesheets like this stylesheet_link_tag *\stylesheets[:all]@ but that means you’d have to do it for all media types and what if there’s are a few, you don’t want to run a check what exists no siree. Instead I give you the smart cough lazy cough method to doing just that stylesheet_link_tag_for_all \stylesheets@. Ain’t she a beauty, she will also do caching on files for each media type and uses the same arguments as stylesheet_link_tag because she uses it herself.

But the fun doesn’t stop there folks, not by a long shot. What if I have a stylesheet that I want to load before a currently included file, well then instead of calling append_stylesheets you would call prepend_stylesheets and takes the same kind of arguments as the former. Included a stylesheet that you don’t need or want, then prevent_stylesheets is the thing to use. Again same arguments, none of the fat. What I like to do actually in my layout files is to do this stylesheet_link_tag_for_all prepend_stylesheets('form', 'layout'), you see all those functions actually returns all the currently included stylesheets, so I just pass them straight into stylesheet_link_tag_for_all with no problems whatsoever.

I’ve also done the same for javascripts to give you the full special treatment. Same deal as before but substitute javascripts for stylesheets and there you go. Also there’s no need for a super-dooper for all function and can use javascript_include_tag instead.

Do enjoy my gifts and if there’s a problem or anything please let me know.

Peace.

UPDATE
Didn’t realise that the public didn’t have permission to download the attachments, my bad :S

AttachmentSize
javascript_helper.rb1.14 KB
stylesheet_helper.rb1.32 KB

Damn you and your comment

Damn you and your comment approval queue!

The only problem I have is

The only problem I have is you using Peace at the end of the post.... interesting info though. If I only I understood anything after "I hate that pun" :s

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Incorrect please try again
Enter the words above: Enter the numbers you hear:

Twitter