2006 年 3 月 3 日,星期五

注解模型

由 marcel 发布

是否曾经在使用某个模型时忘记了它的所有列?是否曾经在单独的窗口打开 schema.rb 文件,这样就能看到表的结构?

Dave Thomas(以 Rails Pragmatic Studio 的名声著称,还包括其他无数的事情),破解了一款 插件,为所有模型类顶部添加了注释块,注释块记录了给定模型的当前架构。最终可能会显示类似内容

</p>
  1. Schema as of Sun Feb 26 21:58:32 CST 2006 (schema version 7)
    #
  2. id :integer(11) not null
  3. quantity :integer(11)
  4. product_id :integer(11)
  5. unit_price :float
  6. order_id :integer(11)
    #
class LineItem < ActiveRecord::Base belongs_to :product

</code></pre></notextile>

When the schema is updated, the comment is updated to reflect the new schema.

Install it with the plugin script:

script/plugin install http://svn.pragprog.com/Public/plugins/annotate_models

Run it with a custom rake task:


rake annotate_models

Check out the caveats in the README.

Thanks for sharing Dave.