Petri Wessman's weblog

Minireview: Rails Antipatterns, by Chad Pytel and Tammer Saleh

Ruby on Rails, nowadays sporting a major version number of 3, has become one of the more popular web development platforms – for good reason, in my opinion. The design is quite elegant, and the emphasis on “convention over configuration” lets you design apps with surprisingly few lines of code. Also, being based on Ruby gives it another layer of elegance (again, in my opinion).

However, all this seeming simplicity hides the fact that both the language (Ruby) and the platform are actually quite complex, underneath the simple exterior. Things work well if the designer follows the Rails (and Ruby) conventions and has at least some understanding of how a web app stack works. Unfortunately, in reality all too many programmers try to use Rails to emulate what they are already familiar with (PHP, in many cases), and even “professional programmers” seem to sometimes have only the faintest idea of how web apps actually work underneath the hood. The result can be a horror show.

Which brings us to this book, Rails Antipatterns. While it’s nominally about Rails “antipatterns” (design patterns that should not be emulated), it’s more properly a book about best practices and refactoring Rails apps. Written by people with extensive and painful knowledge about the horrors involved, it contains a ton of useful advice about how to (and how not to) design Rails apps. Each section focuses on a certain area of the software stack, and highlights the most common mistakes (and how to fix them).

I found it to be a great read, and even though Rails itself is nearing the publication of version 4.x at this point, virtually all of the advice within still holds true (the tiny part that doesn’t is the stuff about plugins, which are being phased out completely). If you already know Rails to some extent, this book will probably teach you a thing or three. If you don’t, you’ll be better of first reading some intro text and doing some coding of your own, and then going back to this book to see all the ways you screwed up.

As always with power tools, Rails only works properly if you know how to use it, and a large part of that is conventions, best practices and other not-initially-obvious design factors.

I personally most enjoyed the section on how to “thin down” a “fat” controller action, using ActiveModel callbacks and other related tricks. I used the advice here to refactor some of my own code with similar problems, and the end result was much cleaner.

Published on by Orava, tags , , , ,

Avatar and other stuff

We stayed cooped up inside for most of the New Year extended weekend; we originally intended to “do some stuff”, but the extra-chilly weather and general laziness put a stop to that. It was nice to just cozy up inside for days, don’t get me wrong. Anyway, yesterday we felt like doing something, so we decided to go see Avatar since a) it’s by Cameron who is generally awesome and b) it had been getting generally positive though slightly mixed reviews.

Well, it was very much worth seeing. It’s not a perfect movie; the plot is more than a tad predicable and follows the classic “boy meets girl and becomes hero” pattern a bit too much by-the-numbers. Maybe the most fitting thing to say about the plot is that it was serviceable; while it could have been (a lot) more original, it didn’t get in the way. But the visuals. Damn, they were jaw-dropping, especially when seen on the large screen with 3d. This is the best visualization of a science fiction world and native culture that I recall even seeing. Even though I knew that most of what I was seeing was computer-generated, my eyes were telling me the stuff was real. During the whole 3-hour span, there was only one short clip where the CGI was obviously CGI in a way that jarred me. That’s damn well done.

So. The complaints about the plot do have merit. But it’s not that bad, and this is one movie that is worth seeing for the visuals alone. They are just that good. The star of this movie is the planet Pandora.

(Added later: here is a hilarious plot summary. Spoiler alert, you might want to skip until you’ve seen the movie!)

Apart from that, I actually spent a large part of the mini-vacation coding and watching “TV” – coding in one window with a video player running in another. I’m working a hobby-project Exalted character generator web thingy. I’m not sure when if ever it will be ready, let alone ready for general use, but it’s one of those things – a way to learn lots of new tech while building something useful. I’m using a lot of bleeding-edge stuff, and Ruby on Rails of course. Among the toys are: authlogic, declarative_authorization, formtastic, css_dryer, jQuery & jQuery-UI, AJAX (with dynamic jQuery/Javascript generation via Rails views), and rspec test cases for all models and controllers. Fun stuff, and complicated enough to keep it interesting.

On the TV side, I managed to watch:

  • True Blood season two. Good stuff, and very interesting “bad guy”. Still among the best of the current crop of series. Not for the prudish, though.

  • Californication season two. Very good and very funny. Also not for the prudish, and I mean it: this show is mostly about sex. And about how people deal with it. I really like this show, because the characters are excellent and it somehow manages to be lewd & irreverent and warm & understanding at the same time. The characters screw up (and just screw) all the time, but you can’t help understanding and liking them at the same time.

  • The Doctor Who “specials” Waters of Mars and End of Time (parts 1 & 2). Watchable but nothing all that special. Will be interesting to see how the new Doctor works out, since these were the final ones with the (great!) David Tennant.

  • Some other bits and pieces… some Heroes (meh, but at least getting a bit better), Sanctuary (meh), etc. Nothing much worth commenting on.

Railscasts considered awesome

I have to give a shoutout to Railscasts, a screencast series by Ryan Bates, which is made of Awesome. As might be guessed, the subject is Ruby On Rails programming. In each episode, he talks about one specific subject (be it a technique, plugin, gem, or whatever) and shows how things work. I typically at least try out most of the things he talks about, often adopting them permanently for my own use (sometimes not; I’m not wild about Cucumber for example).

Many of the episodes are also available as “asciicasts”, for those without required video codec etc, and/or for those who want easy cut+paste access to code.

Without exaggeration I can say that at least half of the advanced Rails techniques I’ve learned (and sometimes forgotten) over the past few years have been due to Ryan’s screencasts. So… thanks, man. Keep on rocking.

Published on by Orava, tags , , , ,

Life wins, slowly

Spent Sunday at the salle, Ilkka held a seminar on Bolognese sidesword and dagger techniques. Was a lot of fun, once again, and it’s really nice to be back to normal health (if not good shape, yet). I had a really long-term flu / throat infection thingy, and since that carries a small but non-zero danger of heart injury it you exert yourself too much, that meant a month+ of no exercise for me. Which was doubly nasty because the flu wore me down and the lack of exercise did the same. Finally felt well enough to come to the salle last Monday, but a month’s pause in training shows. Oh well. It’ll come back, it always does.

The training, I mean. The flu can stay away, as far as I’m concerned.

I’m also slowly waking up to the fact that Christmas will soon be here. I’ve only begun to think about presents, and only have a few taken care of so far. So… some shopping this week.

Work has been really interesting (if very busy) during the last month or so, since I’ve managed to convince my bos(ses) that doing a certain new app with Rails, with a REST datasource in our Java/JBoss main server end, is a good idea (and I honestly do think it is a good idea). This means that I’ve been coding Ruby/Rails a lot, and also tinkering with some lesser-known aspects of it like ActiveResource. It took a while to puzzle out the HTTP/XML format Rails expects and to duplicate that on the Java side, but once it’s in place it’s pretty impressive: the Rails app can now get a data feed from our server with just a few lines of code, and that feed behaves in many respects like a normal ActiveRecord database object. Cool stuff. I’ve also dived into testing with rSpec, with heavy use of mock objects and dynamic fixtures (since I want to keep the tests independent of server data). Figuring out HttpMock took a while, but now that also works.

Rails rocks, it’s by far my favorite wep app framework nowadays. Nothing else comes even close.

Added later: …though I do have to say that some of the lesser-used facets of Rails (like ActiveResource, especially with nested resources) can be pretty poorly documented, or not at all. I’ve quite often had to resort to hacking the Rails core code to figure out what the hell is going on. Today has mostly been spent in figuring out how the hell I can actually create nested REST resources. Finally figured it out, but it wasn’t exactly documented anywhere I could find. Today’s headaches also include puzzling out the exact XML format for server-side validation errors (no, it’s not what the docs claim it is) and in working around the fact that the XML formatter wants to turn all underscores into dashes in element names. Sigh.

Rails still rocks. But prepare to enter “Here There By Tygers” territory if you use some of the more bleeding-edge facets of it.

Published on by Orava, tags , , , , , , ,

Rails, Passenger, Shoulda

My Rails coding project is proceeding, I’m currently working on getting a user permission system in shape so I can open up my alpha test Exalted character tool to a friend who is interested in checking it out. Still quite a bit of work to do, but it’s proceeding nicely. A big helper has been the fact that I’ve dived pretty deep into test-oriented programming here – I probably have more test code than “actual” code, and have most of the model and controller functionality covered. It’s nice to type ”rake test” and see hundreds of tests get run in an instant, it gives a nice instant sanity check to any changes I make to what is at times somewhat tricky code.

I’ve been using some of the new Rails 2.1 features; named scopes are useful, as are many other helpful additions. In addition, I recently decided to give Phusion Passenger a whirl and was pleasantly surprised to be able to move all the current Rails apps on my server (including this blog) to run under it with minimal hassle. Much nicer than Mongrel, especially since it does automatic load balancing “out of the box” – and it makes Rails as easy to host as PHP, which is extremely welcome.

One other nice recent find was the Shoulda plugin, which provides syntactic sugar on top of Rails unit and functional tests. Very cool, and encourages grouping of tests into functional blocks. In addition, it provides macros which generate tons of tests for restful resources with just a few lines of code.

Published on by Orava, tags , , ,

Coding on Rails

This week I’ve been coding quite a bit on my free time, for a change. I’ve been following Ruby on Rails since it was at version 1.0 and have done some small toy test apps, but to date I haven’t really built anything biggger with it. I’ve had plans, sure, but somehow just never got started – part of that is just an overabundance of stuff I’d like to code, I could never decide what to do. I’d like a wiki that works just like I want, I’d want a nice simple photo gallery, I’d like lots of tools to help with rpg gamemastering, I’d like a pony… well, scratch the pony, but you get the drift.

Rails is (really!) great, and it allows you to develop more or less as fast as with PHP without needing to touch the Mindshatteringly Horrid Piece of Shit(tm) that is PHP. You can write web apps in an “agile” fashion while keeping the architecture clean, with a level of abstraction that I like. It’s somewhere between the over-engineered N-levels-of-cumbersome-abstraction maze of J2EE and the “easy web apps for people who don’t know how to code” of PHP. Me likes.

…but naturally there’s some setup involved when you want a system with user management and authentication, deployment with Mongrel & Capistrano, and all that stuff. The restful authentication plugin makes building a user auth system a bit less painful, but it’s still quite a bit of work (I’ve been following hints from various sources). So there’s quite a bit of startup intertia involved whenever you want to write a non-toy app. It’s a whole lot less intertia than with most other system, but it’s there, still.

Anyway, I realized that I don’t need to write N separate web apps, each duplicating a large part of this functionality – I can just write one app, with lots of subsystems, expanding them as needed and using one user auth system for all. Since this will be mostly for my own use and I don’t need to worry about it being modular and useful for others, keeping the apps separate would just involve (a lot) more work for me and bring no real benefit.

So I’ve been coding during evenings, using a snapshot of the new Rails 2.1 and using all the new conventions and toys (REST, named paths, named scopes, etc). It’s been fun, it’s been a while since I had a coding project, and I’ve been learning a lot of practical Rails stuff at the same time.

To date, I have a working (if simplistic) wiki, a user authentication system with email confirmation, and the beginnings of an Exalted character builder/manager tool for my Exalted game. Deployment is handled by Capistrano, and the thing runs on a (currenly single) Mongrel instance with Apache proxying. The intention is to expand it with a blog engine at some point, and maybe move this blog over – but that’s (far) in the future. That would also need some sort of image upload capability, which overlaps with the photo gallery I’d also like to have one of these days. Work work.

In any case, it’s a fun project.

Otherwise it’s been pretty quiet. Went to basic swords training on Tuesday, was fun – but somehow managed to do something bad to my heel. Didn’t notice anything that evening, but the next day it was very sore and I had to limp along. Janka thinks I may have inadvertently over-stretched my Achilles tendon (or some such) before it was warmed up properly, but that’s just a theory. In any case, it seems to be getting better now and it hardly hurts at all even if I stretch the sore spot… so should be able to go to training next week without problem. It might have been ok to go yesterday, but I tend to be very careful when it comes to foot/joint/tendon problems. I’d much rather miss out one week than to really aggravate some spot, that an easy recipe for getting problems that will need weeks or months to heal. Sore muscles, no problem. Seriously hurting tendons or joints? Danger sign. For me, at least.

I’m still a bit mystified by what caused the thing, but sometimes these things happen. My body is telling me that something is (or was) wrong, so that’s that.

Looks to be a busy weekend, a party on Saturday and an Exalted game to run on Sunday. Need to squeeze in some game prep today and Saturday, even though I’m mostly ready… some details still need fleshing out.

Published on by Orava, tags , , , , , ,

Weekend approaching

End of the week nearing, which is nice – except that it doesn’t look like a weekend full of rest. Hopefully some fun, though. Teemu is running some VTES tournaments on Saturday, a quick Duffin Draft starting at 10am, followed by a normal constructed tournament at 12am. I still don’t have a deck ready, looks like I’ll have to play with an untested deck yet again. Defeat is always an option!

Sunday will have me doing cutting practice with sharp swords. Never done that before, should be interesting.

A funny thing with exercise: I’ve been feeling less dead after the 3h sword practice sessions lately. Very tired, yes, and often aching in various places, but less physically wiped out. Which is nice. Yesterday was mentally a catastrophe, though, the first 2 hours of longsword was no problem but the last hour of backsword just totally overwhelmed me, I probably got whacked on the mask more that I managed to parry anything. Somehow I just could not process the (simple) stuff at the pace we were going, and was just perpetually confused. It happens, sometimes. Not sure why, since on other days the same things flow by with no problems.

I’ve been playing around with Rails again a bit, I have a small project going on which might even end up being useful but is mainly intended as a exercise in trying out all sorts of cool Rails stuff. I’m still waiting for my physical copy of the 2nd edition of Agile Web Development With Rails to get here, in the meantime I’m making do with the PDF edition. Yesterday I noticed that O’Reilly has a “buy 2 books, get 3” deal that also applies to PDFs, so I bought myself PDF copies of Rails Cookbook, Ajax on Rails and CSS Cookbook, 2nd ed, for a total of a bit over $40. Not bad. Haven’t had time to read them yet (obviously), but a quick browse leaves me happy with what I bought, they look like good additions to my tech bookshelf.

I should probably also mention dokuwiki. I’ve played around with various Wiki engines so far, some have been horrible, some have been ok, and some have been very good. Up to now I’ve preferred mediawiki, since it has a nice layout and features, but I’ve always found the setup and configuration of it to be more cumbersome and complicated than I like. Enter dokuwiki, which is ultra-simple to set up; it doesn’t even use a database, just flat files. In spite of the simplicity, it offers a nice selection of features and nice, clean default layout. All in all, it looks like just the right balance of features vs setup/config complexity for me, and I set up an instance for a personal wiki. I also have a private instance for (duh) private stuff, the fast and simple setup makes creating a new wiki a breeze. Me likes. Sure, the thing probably doesn’t scale all that well with no database, but who cares; I’m not running wikipedia here. Also, it’s PHP which I normally avoid like a plague due to security concerns, but this app is just so good I’m willling to ignore the evil of PHP.

A fact of life, which I’ve discovered, is that most people are absolutely horrible programmers. Even the ones who make their living as one. Especially some of them. If I had a dollar for each utterly horrible piece of code I’ve had to see (or fix), I’d be somewhere warm with a drink in my hand and “should I buy a Jaguar?” as my main concern. Take that into account, and enter PHP into the equation. PHP, which lets people who have no business writing anything more complicated than Excel macroes suddenly become “web developers”, with naturally no idea of what a “web exploit” even means let alone how to protect against one. Stir into that pot the fact that the PHP development team has a Microsoft-like attitude about security, i.e. features and ease of use are always seen as more important… and voila!, you have a framework which is singlehandedly responsible for a massive amount of server compromises. Sure, a competent person can use PHP responsibly and write a secure app, just like a competent person can write a massive transaction processing system in Visual Basic. Doesn’t make either of them into good tools, or change the fact that PHP is what is technically known as a ”massive piece of shit”.

Ahem. Anyway… Rails good. Dokuwiki good. PHP bad. Beer good. Wife cute.

Published on by Orava, tags , , , , , , , , ,

Rails 1.1.6

The 1.1.5 patch to Rails didn’t quite fix all the holes, so version 1.1.6 is now out, along with details on the hole. It’s good to see the Rails team take this seriously and move fast – and I really don’t understand the whiners about “security through obscurity”. Yes, in the long run that’s a bad policy, but when you’ve just discovered a hole in a popular framework, the thing to do is precisely what the Rails team did: announce the fact that there’s a potential expoit and offer a new version of the software, but withhold details for a day or two until most people have managed to update their systems. To do otherwise would be to give the script kiddies of the world a free ride. Yes, you can possibly figure out the hole by comparing diffs… but most of the script kids out there are just that: kids who will use a ready-made “hack”, but won’t bother to figure it out for themselves.

This site is now upgraded to 1.1.6, naturally. Typo is still version 4.0.0 – there is now a version 4.0.2 out which fixes some bugs and includes Rails 1.1.6, but the gem updater for that one gave me an error. I’ll try it again after Ropecon, no time to hack now.

Published on by Orava, tags , , ,

Backpack revisited

Backpack is a web-based service/app run by 37signals, the same company responsible for unleashing Ruby On Rails on the world. The base/demo version is free, if you pay you get extra functionality – this seems to be a growing trend these days.

So, what is it? That’s a reasonable and simple question, but the answer is a bit more elusive. It’s something like an ultra-simplified wiki with todo (and other) list support. It provides some structure to your data (unlike a traditional wiki which is totally freeform), but doesn’t mandate a strict system or layout. It’s also extremely streamlined and simple, by design – the design idea was to only include the things people really need, and nothing else.

So we’re back to “what is it”?

It’s an sort of freeform, online personal organizer tool, a place to hang all the virtual (or physical) Post-It notes and lists that are always hanging around. It’s a place to store all the general small “stuff” and info that don’t have any proper place, and which you need to refer to often. Todo-lists. Reminder notes (Backpack provides email notifications on those). General “notes to self”. Just… stuff.

I tried it out when it first came out less than a year ago, and was mildly interested by the concept. On the other hand, my gut feel was “I could do all this myself with a wiki and some other stuff”. Which is true… except I never got around to organizing something like that, so the “I could do something like this myself” has been replaced with “well, I could, but why should I when this exists”?

At the time, Backpack was missing any sort of calendar, and that was really the deal-breaker for me – I need some sort of calendar in my organizer tool.

Well, just about a week(?) ago the bastards added a calendar to it. I tried it out, and it works – like everything in Backpack it’s ultra-simplified; there is only one view, and you can’t really configure anything. But it works, and my calendar needs are very simple. As a sneaky move, the calendar is only available in the for-pay accounts, so I was “forced” to move into the $5 per month account scheme. I don’t really mind.

So. With the addition of the calendar feature, Backpack has become a surprisingly useful tool for me. I still find it hard to describe to people exactly what the thing actually is or exactly how I use it, but… “works for me”.

While I’ve come to appreciate 37signal’s “keep it as simple as humanly possible” design method, I still think they take it a bit too far sometimes. There are a lot of things in Backpack I’d like to customize. On the other hand, I suppose everyone has their list of things, and if all were accomodated the whole app would quickly become a confusing, Microsoft-like mess. The zen-like simplicity does have a certain charm, keeping that in mind.

Published on by Orava, tags , , , , , ,

Powered by Publify – Thème Frédéric de Villamil | Photo Glenn