Posted over 7 years ago. Visible to the public.

Ruby GetText will eval scripts containing ActiveRecord classes

When the Ruby parser module of Ruby-GetText comes across a file in one of its search directories (e.g. lib/scripts/) and finds out that you are defining ActiveRecord classes inside it, it evaluates the whole file. Here is how to avoid that.

What's happening?

Let's say you have the following script which is only run once, manually, via script/runner:

Copy
# lib/scripts/doomsday.rb class User < ActiveRecord::Base; end User.destroy_all

In that case we would define a User model solely for the purpose to have access to the users table via ActiveRecord, without being affected by the application's real User class.

Unfortunately GetText::ActiveRecordParser.target? tries to be smart when looking for model attributes to put into po files. If it encounters any declaration like …

Copy
class User < ActiveRecord::Base

… it will claim that it can handle the class. Then, its parse method will evaluating the whole file to do so:

Copy
# from the Gem's lib/gettext_activerecord/parser.rb: eval(open(file).read, TOPLEVEL_BINDING)

This means that the script will be executed every time you run the rake task to rebuild your po files.

Holy cow!

Fix 1: Cheat

Use the following hack to define classes inside your scripts and trick the gem:

Copy
eval <<RUBY class User < Active#{}Record::Base; end RUBY User.destroy_all

Ugly as hell, but works.

Fix 2: Blacklist

Another solution would be to exclude lib/scripts explicitly in your GetText rakefile:

Copy
paths = Dir.glob("{app,lib,assets}/**/*.{rb,erb,rjs}").reject do |filename| filename =~ %r{^lib/scripts} end GetText.update_pofiles('my_project', paths.sort, 'my fancy project')

Note that this means it will break for you again once files are added in a non-excluded path.

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
about 1 year ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more