script/console is the coolest thing ever
Posted by Pat Thu, 17 Nov 2005 08:48:00 GMT
If you’re not using script/console, you really need to be. For those who don’t know what it is, it’s basically irb with the Rails environment loaded in. In fact, that might be precisely what it is, though I’m not sure.
Why is it so cool? Well you can access your entire Rails application from the command-line, which is cool if you’re a geek. A very nice use is to access your model and make database updates through there, rather than messing with your database directly. I remember a while ago I spent about an hour debugging some problem because I had used the psql client to delete some records…had I use script/console, my application logic would have taken care of it for me.
What I did that showed me how cool it is
I upgraded Typo to the current version. I found that they’ve got a sweet tag, <typo:code>, which you wrap around a code block and it formats it nicely. Adds scroll bars so that it doesn’t break your layout, puts a nice blue background, and you can probably specify more in CSS if you’re good with that.
In my previous posts, I was using the Textile @ to specify code formatting, and sometimes using <code> blocks. They didn’t look nearly as good as Typo’s cool <typo:code> tag, so I wanted to convert all my posts to use them. I barfed at the thought of having to go through all my posts and manually edit them…so I wondered if I could use script/console to load up all the posts and make the changes I wanted. I looked at a few and figured out all the cases that I had code blocks in, and wrote a little function to do all the substitutions. Then I loaded up script/console and made it happen.
def fix_code(article)
article.body = article.body.gsub("<br/>\r\n@", "\r\n<typo:code>")
article.body = article.body.gsub("@\r\n", "</ typo:code>")
article.body = article.body.gsub("</ typo:code><typo:code>", "\r\n")
article.body = article.body.gsub("<code><pre>", "<typo:code>")
article.body = article.body.gsub("</pre></code>", "</ typo:code>")
article.body = article.body.gsub("<pre><code>", "<typo:code>")
article.body = article.body.gsub("</code></pre>", "</ typo:code>")
article.save
end
articles = Content.find_all_by_type("Article")
articles.each { |a| fix_code(a) }Unfortunately the </typo:code> in the code I posted above messes things up, so I made it ”< /typo:code>”. Anyway, there’s obviously not supposed to be a space.
Using script/console saved me a lot of time, and turned a boring task into a pretty fun challenge/learning experience. I suggest you start using it.





You might want this patch to make the scroll bars work correctly on IE.
http://typo.leetsoft.com/trac/ticket/631
Without the patch, your sidebar gets pushed to the bottom of the page.
Thanks for pointing that out. I had never looked at my site in IE before – boy was it jacked up.