Railsで開発してると、 「このModelのテーブルにどんなカラムを作ったっけ」 って思うことがよくある。
しかし、いちいちrails dbとかrails cとかで中身を見に行くのもだるい。
そんな時にannotateさんに頑張ってもらう。
導入は簡単。
とりあえず 前回のやつ に導入する。 まあgemなので、毎度おなじみGemfileに
gem 'annotate'
と追記して
$ bundle install
でも叩いておけば導入できる。
それから
$ bundle exec annotate
とでも叩けば
Annotated (1): Item
という感じで、何の情報を更新したかを教えてくれる。
で。実際にそのapp/models/item.rbファイルを見てみると
# == Schema Information # # Table name: items # # id :integer not null, primary key # name :string(255) # price :integer # created_at :datetime not null # updated_at :datetime not null # class Item < ActiveRecord::Base attr_accessible :name, :price strip_commas_from :price end
こんな感じでコメントとしてファイル先頭にテーブルのスキーマ構造が投入されると。
さらにtest/unit/item_test.rbも
# == Schema Information # # Table name: items # # id :integer not null, primary key # name :string(255) # price :integer # created_at :datetime not null # updated_at :datetime not null # require 'test_helper' class ItemTest < ActiveSupport::TestCase # test "the truth" do # assert true # end end
こんな感じになってるし test/fixtures/items.ymlも
# == Schema Information # # Table name: items # # id :integer not null, primary key # name :string(255) # price :integer # created_at :datetime not null # updated_at :datetime not null # # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html one: name: MyString price: 1 two: name: MyString price: 1
こんな感じ。
あとはrspecを使ってる場合でも spec/models/item_spec.rbに書き込まれたはず。
あとはfactory_girlなどなどにも対応していたり、なんか色々オプションあるけど https://github.com/ctran/annotate\_models を見たら、英語だけどこのぐらいはわかるはずだから頑張ってね(丸投げ)

