Install as a standalone gem
$ gem install aam
Or install within application using Gemfile
$ bundle add aam
$ bundle install
$ rake aam
This will create column information in related files. Embed warnings if there are no indexes in places you need.
ActiveRecord::Schema.define do
suppress_messages do
create_table :users do |t|
t.string :name
t.boolean :flag, :default => false
end
add_index :users, :name, :unique => true
create_table :articles do |t|
t.belongs_to :user, index: true
t.belongs_to :foo, polymorphic: true, :index => false
end
create_table :blogs do |t|
t.string :name
end
end
end
class User < ActiveRecord::Base
has_many :articles
has_one :article, :as => :foo
end
class Article < ActiveRecord::Base
belongs_to :user
belongs_to :foo, polymorphic: true
end
class Blog < ActiveRecord::Base
has_many :sub_articles, :foreign_key => :foo_id
end
class SubArticle < Article
belongs_to :blog, :class_name => "Blog", :foreign_key => :foo_id
end
puts Aam::Generator.new(User).generate
# >> # == Schema Information ==
# >> #
# >> # Userテーブル (users as User)
# >> #
# >> # +----------+------+---------+-------------+------+-------+
# >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
# >> # +----------+------+---------+-------------+------+-------+
# >> # | id | Id | integer | NOT NULL PK | | |
# >> # | name | Name | string | | | A! |
# >> # | flag | Flag | boolean | DEFAULT(f) | | |
# >> # +----------+------+---------+-------------+------+-------+
puts Aam::Generator.new(Article).generate
# >> # == Schema Information ==
# >> #
# >> # Articleテーブル (articles as Article)
# >> #
# >> # +----------+----------+---------+-------------+-----------------------+-------+
# >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
# >> # +----------+----------+---------+-------------+-----------------------+-------+
# >> # | id | Id | integer | NOT NULL PK | | |
# >> # | user_id | ユーザー | integer | | => User#id | A |
# >> # | foo_type | Foo type | string | | SpecificModel(polymorphic) | |
# >> # | foo_id | Foo | integer | | => (foo_type)#id | |
# >> # +----------+----------+---------+-------------+-----------------------+-------+
# >> #
# >> #- 備考 -------------------------------------------------------------------------
# >> # ・Article モデルは User モデルから has_many :articles されています。
# >> # ・[Warning: Need to add index]create_articles マイグレーションに add_index :articles, [:foo_id, :foo_type] を追加してください
# >> #--------------------------------------------------------------------------------
puts Aam::Generator.new(Blog).generate
# >> # == Schema Information ==
# >> #
# >> # Blogテーブル (blogs as Blog)
# >> #
# >> # +----------+------+---------+-------------+------+-------+
# >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
# >> # +----------+------+---------+-------------+------+-------+
# >> # | id | Id | integer | NOT NULL PK | | |
# >> # | name | Name | string | | | |
# >> # +----------+------+---------+-------------+------+-------+
puts Aam::Generator.new(SubArticle).generate
# >> # == Schema Information ==
# >> #
# >> # なんとかテーブル (articles as SubArticle)
# >> #
# >> # +----------+----------+---------+-------------+--------------------------------------+-------+
# >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
# >> # +----------+----------+---------+-------------+--------------------------------------+-------+
# >> # | id | Id | integer | NOT NULL PK | | |
# >> # | user_id | ユーザー | integer | | => User#id | A |
# >> # | foo_type | Foo type | string | | SpecificModel(polymorphic) | |
# >> # | foo_id | Foo | integer | | :blog => Blog#id と => (foo_type)#id | |
# >> # +----------+----------+---------+-------------+--------------------------------------+-------+
# >> #
# >> #- 備考 -------------------------------------------------------------------------
# >> # ・SubArticle モデルは User モデルから has_many :articles されています。
# >> # ・[Warning: Need to add index]create_articles マイグレーションに add_index :articles, [:foo_id, :foo_type] を追加してください
# >> # ・SubArticle モデルは Blog モデルから has_many :sub_articles, :foreign_key => :foo_id されています。
# >> #--------------------------------------------------------------------------------