Association extensions を block で指定して gettext updatepo を行うとエラーになる

↓ のようなテーブルがあるとします

+---------+           +----------+
| authors | --------- | articles |
+---------+ 1       N +----------+

ActiveRecord は ↓ のように Association extensions を使ったもの。

class Author < ActiveRecord::Base
  has_many :articles do
    def foobar
      :foobar
    end
  end
end

class Article < ActiveRecord::Base
end

この状態で updatepo を走らせると次のようなエラーが…。

$ rake updatepo
(in /home/foobar/src/rails-gettext)
Ignored 'app/models/author.rb'. Solve dependencies first.
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations.rb:1245:in `const_set': wrong constant name GetText::ActiveRecordParser::AuthorArticlesAssociationExtension
po/foobar.pot
. done.

AuthorArticlesAssociationExtension ってどこから出てきたんだ…と思って ActiveRecord のソースを見ると Association extensions を block で指定するとトップレベルにそういう名前でモジュールを定義するようです。無名モジュールを使っているんだとずっと思ってた…。

とりあえず ↓ のようにトップレベル以外のところで名前を付けたモジュールを用意するように修正して回避。

class Author < ActiveRecord::Base
  module ArticlesExtension
    def foobar
      :foobar
    end
  end

  has_many :articles, :extend => ArticlesExtension
end

gettext の repository を見ると ActiveRecordParser が修正されててこの問題が起きないようになった模様。Diff of /gettext/lib/gettext/parser/active_record.rb