All posts in Programming

Hacked By GeNErAL

Hacked By GeNErAL

I18n power tool: stylesheet_flipper for rails’ asset pipeline

Spice up your localization toolbox with stylesheet_flipper: Make you left-to-right (LTR) designed css stylesheets support right-to-left (RTL) locales with a snap (and vice versa).

For the impatient: All this ended up as a gem, that you can find on rubygems and on github.

In my most recent startup, monibuds, we recently saw an opertunity to enter the Israeli market after having built our app for english and danish locales. But maintaining two sets of stylesheets, one for LTR and one for RTL seemed like a mess, so I started looking for better solutions. I wanted, using rails’ asset pipeline (sprockets), to enable right-to-left locales as well as left-to-right, but I only wanted to maintain a single set of stylesheets.

By building a railtie that registers a sprockets bundle_processor, you can hook into the asset pipeline and process any asset both on-the-fly (development) and during assets:precompile (production). Like this:

module StylesheetFlipper
  class Railtie < Rails::Railtie
    initializer "stylesheet_flipper.initialize_rails", :group => :assets do |app|
      app.assets.register_bundle_processor 'text/css', :stylesheet_flipper do |context, data|
        if context.logical_path.include?('-flipped')
          R2.r2 data

Then you only need to serve the stylesheet with the ‘-flipped’ postfix when you want to flip it. To do that takes a few steps:


 *= require application


def stylesheet_flipper
  if [:ar, :he].include? I18n.locale


<%= stylesheet_link_tag stylesheet_flipper %>


# for flipped versions to work, we need to bundle
# the stylesheet in dev mode as well
config.assets.debug = false


config.assets.precompile += %w( application-flipped.css )

Oh, and dont forget to add a direction to you body tag if you don’t have one

body {
  direction: ltr;

Et voila. Happy flipping!

Compiling assets during slug compilation on heroku cedar stack with rails 3.1

Trying to deploy a rails 3.1 app to heroku, I got the following error during slug compilation:

path/to/image.png isn’t precompiled

Precompiling assets failed, enabling runtime asset compilation

Turns out it is a rails 3.1.0 bug and upgrading rails to 3.1.1 (3.1.1.rc1 as of the time of this writing) solves the problem.

Don’t use windows for Ruby on Rails development

Starting a new task at a major company, I was given a PC running windows 7 (believe it or not, they never bothered to ask me for preferences) for developing an internal rails app.

I took the challenge – here’s my story.
Continue Reading →

Prototyping: Axure vs. Rails

In my quest to conquer the prototyping arena, I’ve met a tool called Axure a couple of times now. Since I want to do prototypes using my favorite tool, Ruby on Rails, I thought I’d better check out Axure to see what the competition is. Continue Reading →

Rails + jQuery request datatypes condensed

Recently I’ve noticed that the default behaviour for the jQuery UJS adapter has changed a couple of times.

This is my condensed take on getting ajax working smooth with jQuery UJS and Rails 3
Continue Reading →

Validate presence of at least one associated record

I’ve previously had the need to validate, that at least on record in a has_many association existed. For that I used a custom validator. But today i realized that the presence validator in rails 3 will do exactly that:
Continue Reading →