Today Icelab Learned

Displaying booleans in Active Admin

In Active Admin if you want to display a boolean property that doesn’t directly map to a database column you can use status_tag to display the value in a friendly way:

column :featured do |thing|
  thing.featured? ? status_tag("yes", :ok) : status_tag("no")

There’s a bit more info in the Active Admin docs which shows you how to add classes too!

Table columns should be typed

Processing.Table columns should be typed as INT or FLOAT if you want to do maths with them:

table.addColumn("name"); // defaults to STRING
table.addColumn("count", Table.INT);
table.addColumn("height", Table.FLOAT);

If you don’t do that, trying to cast the value on the way out:


Seems like it’d work. Nope.

(Processing 2.2.1)

Using namespaced components in React

The last time I used React we had some pretty awkward workarounds for the fact that components couldn’t be namespaced. I was pretty stoked to find this has been fixed now.

So if you have a component myNavComponent you can do:

var NavComponent = React.createClass(...);

Define your sub-components as attributes:

NavComponent.Next = React.createClass(...);
NavComponent.Prev = React.createClass(...);
NavComponent.Goto = React.createClass(...);
var Nav = NavComponent;

And then use them as you would expect to in your JSX:

  <Nav.Prev />
  <Nav.Goto />
  <Nav.Next />

Formatting strings with C-like formatting codes.

I learnt this while putting together the report emails for Ticketscout.

Most of the time, regular string interpolation is all that we need when working with strings. Though sometimes we want a little more control over the formatting.

Let’s say we have a set of numbers that we want to calculate the average for, then print that average to 1 decimal place.

avg = FLOATS.reduce(0, :+) / FLOATS.size
"Average is: #{avg}" # => "Average is: 25.714285714285715
"Average is: #{avg.round(1)}" # => "Average is: 25.7

Normally, this is fine, but when working with some content, it may be nicer to use Ruby’s String Format instead (especially if trying to format more complex numbers).

"Average is %.1f" % avg # => "Average is 25.7"

We can also make printing hashes a little prettier as well.

dog = {name: 'poppy',breed: 'labrador',colour: 'black'}

"This is #{dog[:name]}, she is a #{dog[:colour]} #{dog[:breed]}."


"This is %{name}, she is a %{colour} %{breed}." % [dog]

Which, to me at least, reads a little nicer.

Kernel::sprintf has a more complete list of formatting strings.

Creating and pushing a git tag

If you’re releasing gems (or any other kinds of packages), git tags are a useful way to track those release milestones in the source code repo.

Creating a tag is easy:

git tag v1.0.0

So is pushing it to a remote:

git push origin v1.0.0

The Git Basics - Tagging section in the online git book is an easy-to-read overview of the various tag commands.

Dynamic classnames in Slim

While glancing through one of Narinda’s pull-requests, I noticed she’d used a syntax for dynamic classnames in Slim that I had not seen before:

/ Aww yeah
- dynamic_classname = (truthy_test ? 'foo' : 'bar')
.static-class-name-one class=["static-class-name-two", dynamic_classname]
  ' Foo or bar?

Slim will magically convert concatenate the classnames based on the truthiness of the truthy_test. Much more flexible (and less stinky) than the string concatenation I would usually use:

/ Eww
- dynamic_classname = (truthy_test ? 'foo' : 'bar')
.static-class-name-one class="static-class-name-two #{dynamic_classname}"
  ' Foo or bar?

Using for dynamically adding class methods

Say you’re building a system where you want to dynamically add some class methods to a class at a particular moment, but without leaving them around for use on the class at other times.

This is pretty easy if you create an anonymous subclass whenever you need to use the special class methods:

module MyCustomFinders
  def special_finder
    where(something: "is special")

class MyThing < ActiveRecord::Base