Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/akicho8/aam

Advanced Annotate Models
https://github.com/akicho8/aam

database-schema generator rails

Last synced: 6 days ago
JSON representation

Advanced Annotate Models

Awesome Lists containing this project

README

        

* Advanced Annotate Models

** Installation

Install as a standalone gem

#+BEGIN_SRC shell-script
$ gem install aam
#+END_SRC

Or install within application using Gemfile

#+BEGIN_SRC shell-script
$ bundle add aam
$ bundle install
#+END_SRC

** How to use

#+BEGIN_SRC shell
$ rake aam
#+END_SRC

This will create column information in related files.
Embed warnings if there are no indexes in places you need.

** Examples of using alone

#+BEGIN_SRC ruby
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 されています。
# >> #--------------------------------------------------------------------------------
#+END_SRC

** 参考

- https://github.com/ctran/annotate_models