Meta programming, serialization
Oh wow, has it been that long already, I need to force myself to post something at least once a week, even if it’s just posting pictures of cats!
So I’m currently working on a social media platform for London, with it’s main purpose being to promote a wellbeing lifestyle in a hectic city. I wrote this handy little utility module for creating methods from serialized data.
Say, for example you have an object, but it can have serialized data stored within a hash in the database, instead of getting the data through the hash notation, you can now access it directly as Object.first.hash_key_name instead of Object.first.hash[:hash_key_name]
This is handy in two ways, you can access serialized data directly as methods of the class instance. Plus you can use the serialized fields directly in a form, as the module automatically creates the setters for the hashes keys. Pretty handy, you just have to specify in an array of serialized field symbols as a private method in the parent class.
Rendering sane markup with Rails helpers
If you, like me, have tried to use helpers to generate complex chunks of HTML programmaticly, then you will have noticed that content_tag doesn’t really cut it for large chunks of HTML
content_tag will render the HTML inline unless you manually insert line breaks and tabs in order to make it readable, not to mention you end up with a horribly nested set of methods wrapping around the content you want to display
Looking something a little like this
As you can see the code looks quite unreadable, and it’s almost backwards in the way that it is generated, I’m creating the inner HTML first, and then wrapping it inside a div depending on if the user is logged in or not, there must be a better way to achieve the same result, while making it look nicer
Enter Builder
The builder gem is something that I have used most often for defining XML documents, however you can also call the Builder::XmlMarkup.new method within any other methods and generate XML markup inline. Using the same example, this is the execution using Builder::XmlMarkup instead of content_tag
As you can see it’s much more readable than the previous example
However each to their own, but I think personally the Builder gem is incredibly useful for creating XML style markup instead of using Rails’ built in helpers to generate HTML. Give it a go, you might be pleasantly surprised!
Phusion Passenger 3
I have recently upgraded my slicehost box to Ubuntu 10.04 LTS, and upgraded the Passenger version to 3, the difference in performance is incredible, at least a 20% decrease in response times.
However the big great news is the inclusion of a new Apache directive,
PassengerMinInstances
Previously passenger would spool down all of the running instances of an app if it was idle for too long, you could override this behaviour by setting PassengerPoolIdleTime to 0, however this would mean that spawned instances would never spin down, utilising the server even if it was getting no traffic, however on the flip side it meant that application initial load times were kept to a minimum
The new directive allows Passenger to load the Rails framework into memory on the first application hit, and because all the processes will not spin down after that no matter how long between requests the page load will be near instantaneous
For me this was the only thing that made Passenger an issue, and clients often complained about the long load times, however with Passenger 3 this load time is negated to the first time the app is loaded after an Apache restart
If you haven’t already I recommend that you upgrade all of your existing Passenger implementations to version 3
Gravatar helper for Ruby on Rails
So, I was working on a project a while ago with user comments, and wanted each person to have their own avatar, however, the Gravatar plugins that existed for Rails seemed a bit too much overkill for my needs.
So I wrote this simple helper method. Either copy the method into your application_helper.rb, or alternatively drop it into your lib directory and include it.
Usage is as simple as just calling:
gravatize(user.email, :size => 50)
This will just render the Gravatar inside an image tag, simple no!