Category: Rails
Rails model visualization
If you’re a developer, you know how it goes. At the beginning of a project, you draw some UML sketches to get you going. There’s a nice tool to do that. As your project goes on, and your code grows, these diagrams don’t get updated too often. After all, every piece of knowledge must have a single, unambiguous, authoritative representation within a system. And for a class model, that representation is the code.
But at some points during your development, you have to communicate with some stakeholders. They want to know what your class model looks like. So you have to provide them with a drawing. Until recently, we went through the trouble of creating these diagrams manually. But those days are over: we found yUMLmeRails
To get yourself a diagram, follow these steps:
cd your_project
script/plugin install \
git://github.com/nelsonsilva/yUMLmeRails.git
rake yUMLmeRails:download
Your diagram will be saved in your_project/diagrams.
Easy. Nice. Handy. Did I mention ‘pie’ ?
Find or create with hash attributes
Dynamic finders in Rails, represent some the ‘magic’ we’ve all gotten used to. One can use these dynamic finders to create or initialize a new object when it doesn’t already exist.
# Find a user by screen_name_
user = User.find_by_screen_name("atog")
# So instead of ...
user = User.find_by_screen_name("atog")
unless user
user = User.create(:name => "atog")
end
# ... you can
user = User.find_or_create_by_screen_name("atog")
It gets even better. By passing a hash to the finder you can initialize or create a new object with all the values while only the attribute named in the finder will be used to find the object.
user = User.find_or_create_by_screen_name(
:screen_name => "atog",
:name => "Koen Van der Auwera")
Working with multiple ruby versions - the sequel
Remember when we talked about how to use multiple versions of Ruby on your Mac?
Well, apparently that post is already outdated. Now all you have to do is to install the rvm gem
I’ve just tried it, and it works without a hickup. I’ve noticed one little thing: the installation of the gem creates a file ~/.bash_profile. This, apparently, overrides ~/.bashrc on my Mac. I lost all of my neatly-crafted terminal environment. Deleting ~/.bash_profile fixed this. Happy camper!
Present
If this isn’t a discovery! I couldn’t have said it better myself.

Just like Nick Quaranto, I prefer positive conditions. For example, you’ll find more unless object.blank? in my code than if !object.blank?. So I’m very happy that I learned about present? today.
present?, the inverse of blank?. Heaven.
Working with multiple ruby versions
For a while, I’ve felt an itch to experiment with Ruby 1.9. It would be better to use it in our day-to-day work, but Ruby 1.9 isn’t that far yet. What has hold me back until now, is that I don’t want to make an unstable Windows machine out of my Maccie. It’s my primary workstation, and I have to work on it constantly.
Via Dr Nic I found Working With Multiple Ruby Versions Has Never Been This Easy.
With the help of the RubySwitcher script, it is laughingly easy to use several Ruby versions next to one another. There’s no more excuse not to do it.
Out of the box, you can install and use the following versions of Ruby:
install_ruby_191(install)
use_ruby_191(use)install_ruby_186(install)
use_ruby_186(use)install_ruby_187(install)
use_ruby_187(use)install_jruby(install)
use_jruby(use)install_jruby_120(install)
use_jruby_120(use)install_ree_186(install)
use use_ree_186(use)
To use the standard Ruby version: use_leopard_ruby
i18n and Rails Engines
Rails Engines, what are they? What do they do? But most importantly: how do they do it?
Rails 2.3 brings us much of the same functionality as the Rails Engines plugin. Learn how to embed one application into another in this episode.
Engines allow us to use one application in another in the form of a plugin. As the screencast shows, you can integrate the app folder of a Rails application in the plugin of another one. All models, controllers and views are available. If you still need custom functionality, you can add them in your applications ‘own’ app folder by redefining the model, controller or view. The same goes for routes; if you have routes.rb in your plugin dir, it is loaded as well.
And i18n, how about that? You would expect config/locals/*.yml to work just as nicely as the app dir and routes.rb. But it doesn’t.
Luckily, it’s not that hard to solve:
In environment.rb, you add
config.i18n.load_path += Dir[Rails.root.join('vendor',
'plugins', 'your_plugin', 'config', 'locales',
'*.{rb,yml}')]
If you prefer to read the Railscast: ASCIIcasts - 149: Rails Engines
Generating a timestamp string in Ruby
In the hope that the Google Gods will help me next time I need this :-)
An easy way to generate a human-readable timestamp string, following the ISO 8601 standard, is:
Time.now.utc.iso8601.gsub('-', '').gsub(':', '')
Very handy if you need a timestamp in a file you’re writing.
Roleify, a Rails authorization plugin
Today I’ve pushed a few updates to the Roleify rails plugin.
The changes are
- you can now use ‘namespaced’ controllers
- I added a helper method to hide/show blocks for a specified role
Example
The initializer
Roleify::Role.configure("role_a", "role_b") do
{
:role_a => { :dashboard_issues => :all },
:role_b => { :issues => "index" }
}
end
So, role_a refers to a Dashboard::IssuesController and role_b refers to an IssuesController.
The helper
module ApplicationHelper
include Roleify::RoleifyableHelper
end
The view
<% allowed?(Roleify::Role::ROLE_A) do %>
// whatever you want for role_a eyes only
<% end %>
More info on GitHub.
Rails `try`
try is one of those small new additions in the Rails 2.3 release. Luckily I found out about it via a Railscast
What is it?
Invokes the method identified by the symbol method, passing it any arguments and/or the block specified, just like the regular Ruby Object#send does. Unlike that method however, a NoMethodError exception will not be raised and nil will be returned instead, if the receiving object is a nil object or NilClass.
Liquid error: Broken pipe
What did you just say?
The code above normally throws an exception if someobject is nil. By using try it just returns nil.
Don’t overuse this.
Using Java 6 with RubyMine on OS X
What do you need?
Next, open RubyMine’s Info.plist:
mate /Applications/RubyMine\ 1.0.5.app/Contents/Info.plist
Find the line with value 1.5* en change it to 1.6*.
Start RubyMine and check the ‘about’. It should look something like this:

Enjoy. It does feel snappier on my side.
On a sidenote: You can set the default Java version via the ‘Java Preferences’ app.
