Skip to content

Formatting Boolean Values in Rails

2011/04/09

One thing that is missing from the Rails view system (ActionView) is the ability to easily and DRYly format boolean values. I would like to write something like the following examples into my .erb files:

<%# should output 'true' or 'false' depending on the value of the boolean %>
<%=b some_boolean_value %>

<%# should output the appropriate string given in the options hash %>
<%=b some_other_boolean, :true => 'yes', :false => 'no' %>

It would be even nicer if that imaginary b helper could easily internationalize its output appropriately. Here’s what I want:

<%# This should look up the translation of :positive/:negative in the current locale under the given scope %>
<%=b a_boolean, :scope => [:my, :boolean, :scope] %>

<%# This should look up :maybe/:maybe_not in the [:boolean] scope of the :de locale %>
<%=b another_boolean, :locale => :de, :true => :maybe, :false => :maybe_not %>

Long story short: If you agree with me that a helper method like this would be cool, then rejoice! I have already written this portentous b method and provided it in a Gist for your coding pleasure!

Get it while it’s hot.

Note the lack of documentation, but I think this blog post gets the point across. Also the method is not overly complicated. You should be able to derive its complete behavioral spectrum from the source code.

One thing that might be worth mentioning, though, is the seemingly awkward choice of default translation keys :positive and :negative. If you are interested in my reasoning behind that, you might want to read this.

Oh and if there already is such functionality provided by Rails or another well known Gem, then please let me know. i just couldn’t find something on Googles first page and so I figured it would be easier to come up with my own solution.

Update

I integrated the changes by hron84 from the comments; the helper now even has a test case! So thanks to hron for this. And yes, I do realise that those comments and commits are quite old by now, but I only now got around to pulling them and mentioning them here in the course of my current blog revival. ;P

About these ads

From → Ruby on Rails

9 Comments
  1. The problem seems a little specific, but the solution looks very good! Nicely done!

    • Julian Kniephoff permalink

      Thank you, I appreciate it. At first I also thought of this as overhead but if you have a lot of boolean attributes in your models that you wish to “visualize” in a localized manner, then I think this really saves you some typing. Also it is more expressive than having the t(boolean_value ? :true_string : :false_string) directly in your code and not only once.

  2. A tip:

    boolean.to_s.to_sym

      • Julian Kniephoff permalink

        Thanks for the great tip. The code really looks much better now. I still have so much to learn about ruby. One thing that I don’t quite get for example is this:

        key = !!(value || false)

        Especially the “|| false”-part is puzzling me: The only situation where this gets executed is when value evaluates to false. At least to my knowledge this is only the case for false itself and nil but substituting both of these into the above expression yields exactly the same value as a simple

        !!value

        doen’t it?

  3. @Julian sorry for late response. I included that because I tried the naive !!value – what you written too – but I experienced some strange problems with it, so I decided to be explicite.

    • Julian Kniephoff permalink

      Interesting. Could you elaborate on that a bit? What kind of problems?

Trackbacks & Pingbacks

  1. Reserved words in YAML and translating booleans in Rails « Julian Kniephoff
  2. Revisiting Using Objective-C-Objects in C++ Classes in the Age of ARC « Filling in the Details

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: