aam 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 699245579213a9098682764ceed7c99eb8c421ffa666ab2b1f9e6cd6ebe56c4e
4
+ data.tar.gz: 9c9332ffe62d2bc489a0c599ca13f48b8a8cee10955d1465b4de59859d7b31f4
5
+ SHA512:
6
+ metadata.gz: 4947e3233adac7958abd5a096b997dcaf3fa345ce567406e2ed3fe23f1e494b3c7ca07314fc1302fd68431fb61044b69f6f4b467c3d7032b68c738436b5e0ef4
7
+ data.tar.gz: f28178f423a1cf5731e6891e0b638238c11a84a577d0cd3725a8aecf3b582b3c5eaacc4e57110ddf5cba76718b80560b941b6ab2643045a250a411bdebf329eb
@@ -0,0 +1,13 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ pkg
11
+ rdoc
12
+ test/tmp
13
+ tmp
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aam.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 akicho8
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,119 @@
1
+ * DBのテーブルと対応するファイルにカラム情報を埋め込みまくる自分用ツール
2
+
3
+ ** 使い方
4
+
5
+ #+BEGIN_SRC shell
6
+ $ rake aam
7
+ #+END_SRC
8
+
9
+ これで関連するファイルにカラム情報を埋め込みまくります。
10
+ 必要そうなところにインデックスが張ってないと警告まで埋め込みます。
11
+
12
+ ** 単体で使う例
13
+
14
+ #+BEGIN_SRC ruby
15
+ ActiveRecord::Schema.define do
16
+ suppress_messages do
17
+ create_table :users do |t|
18
+ t.string :name
19
+ t.boolean :flag, :default => false
20
+ end
21
+ add_index :users, :name, :unique => true
22
+
23
+ create_table :articles do |t|
24
+ t.belongs_to :user, index: true
25
+ t.belongs_to :foo, :polymorphic => true, :index => false
26
+ end
27
+
28
+ create_table :blogs do |t|
29
+ t.string :name
30
+ end
31
+ end
32
+ end
33
+
34
+ class User < ActiveRecord::Base
35
+ has_many :articles
36
+ has_one :article, :as => :foo
37
+ end
38
+
39
+ class Article < ActiveRecord::Base
40
+ belongs_to :user
41
+ belongs_to :foo, :polymorphic => true
42
+ end
43
+
44
+ class Blog < ActiveRecord::Base
45
+ has_many :sub_articles, :foreign_key => :foo_id
46
+ end
47
+
48
+ class SubArticle < Article
49
+ belongs_to :blog, :class_name => "Blog", :foreign_key => :foo_id
50
+ end
51
+
52
+ puts Aam::SchemaInfoGenerator.new(User).generate
53
+ # >> # == Schema Information ==
54
+ # >> #
55
+ # >> # Userテーブル (users as User)
56
+ # >> #
57
+ # >> # +----------+------+---------+-------------+------+-------+
58
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
59
+ # >> # +----------+------+---------+-------------+------+-------+
60
+ # >> # | id | Id | integer | NOT NULL PK | | |
61
+ # >> # | name | Name | string | | | A! |
62
+ # >> # | flag | Flag | boolean | DEFAULT(f) | | |
63
+ # >> # +----------+------+---------+-------------+------+-------+
64
+
65
+ puts Aam::SchemaInfoGenerator.new(Article).generate
66
+ # >> # == Schema Information ==
67
+ # >> #
68
+ # >> # Articleテーブル (articles as Article)
69
+ # >> #
70
+ # >> # +----------+----------+---------+-------------+-----------------------+-------+
71
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
72
+ # >> # +----------+----------+---------+-------------+-----------------------+-------+
73
+ # >> # | id | Id | integer | NOT NULL PK | | |
74
+ # >> # | user_id | ユーザー | integer | | => User#id | A |
75
+ # >> # | foo_type | Foo type | string | | モデル名(polymorphic) | |
76
+ # >> # | foo_id | Foo | integer | | => (foo_type)#id | |
77
+ # >> # +----------+----------+---------+-------------+-----------------------+-------+
78
+ # >> #
79
+ # >> #- 備考 -------------------------------------------------------------------------
80
+ # >> # ・Article モデルは User モデルから has_many :articles されています。
81
+ # >> # ・【警告:インデックス欠如】create_articles マイグレーションに add_index :articles, [:foo_id, :foo_type] を追加してください
82
+ # >> #--------------------------------------------------------------------------------
83
+
84
+ puts Aam::SchemaInfoGenerator.new(Blog).generate
85
+ # >> # == Schema Information ==
86
+ # >> #
87
+ # >> # Blogテーブル (blogs as Blog)
88
+ # >> #
89
+ # >> # +----------+------+---------+-------------+------+-------+
90
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
91
+ # >> # +----------+------+---------+-------------+------+-------+
92
+ # >> # | id | Id | integer | NOT NULL PK | | |
93
+ # >> # | name | Name | string | | | |
94
+ # >> # +----------+------+---------+-------------+------+-------+
95
+
96
+ puts Aam::SchemaInfoGenerator.new(SubArticle).generate
97
+ # >> # == Schema Information ==
98
+ # >> #
99
+ # >> # なんとかテーブル (articles as SubArticle)
100
+ # >> #
101
+ # >> # +----------+----------+---------+-------------+--------------------------------------+-------+
102
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
103
+ # >> # +----------+----------+---------+-------------+--------------------------------------+-------+
104
+ # >> # | id | Id | integer | NOT NULL PK | | |
105
+ # >> # | user_id | ユーザー | integer | | => User#id | A |
106
+ # >> # | foo_type | Foo type | string | | モデル名(polymorphic) | |
107
+ # >> # | foo_id | Foo | integer | | :blog => Blog#id と => (foo_type)#id | |
108
+ # >> # +----------+----------+---------+-------------+--------------------------------------+-------+
109
+ # >> #
110
+ # >> #- 備考 -------------------------------------------------------------------------
111
+ # >> # ・SubArticle モデルは User モデルから has_many :articles されています。
112
+ # >> # ・【警告:インデックス欠如】create_articles マイグレーションに add_index :articles, [:foo_id, :foo_type] を追加してください
113
+ # >> # ・SubArticle モデルは Blog モデルから has_many :sub_articles, :foreign_key => :foo_id されています。
114
+ # >> #--------------------------------------------------------------------------------
115
+ #+END_SRC
116
+
117
+ ** 参考
118
+
119
+ - https://github.com/ctran/annotate_models
@@ -0,0 +1,10 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "bundler"
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ task :default => :test
6
+
7
+ require "rake/testtask"
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aam/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aam"
8
+ spec.version = Aam::VERSION
9
+ spec.authors = ["akicho8"]
10
+ spec.email = ["akicho8@gmail.com"]
11
+ spec.description = %q{Index reference to Japanese correspondence relation annotate_models}
12
+ spec.summary = %q{Advanced Annotate Models}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "test-unit"
24
+ spec.add_development_dependency "sqlite3"
25
+
26
+ spec.add_dependency "rails"
27
+ spec.add_dependency "activerecord"
28
+ spec.add_dependency "activesupport"
29
+ spec.add_dependency "org_tp"
30
+ end
@@ -0,0 +1,112 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "bundler/setup"
3
+ Bundler.require
4
+
5
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
6
+ ActiveRecord::Schema.define do
7
+ suppress_messages do
8
+ create_table :users do |t|
9
+ t.string :name
10
+ t.boolean :flag, :default => false
11
+ end
12
+ add_index :users, :name, :unique => true
13
+
14
+ create_table :articles do |t|
15
+ t.belongs_to :user, index: true
16
+ t.belongs_to :foo, :polymorphic => true, :index => false # Rails5からindex:trueがデフォルトになっているため
17
+ end
18
+
19
+ create_table :blogs do |t|
20
+ t.string :name
21
+ end
22
+ end
23
+ end
24
+
25
+ class User < ActiveRecord::Base
26
+ has_many :articles
27
+ has_one :article, :as => :foo
28
+ end
29
+
30
+ class Article < ActiveRecord::Base
31
+ belongs_to :user
32
+ belongs_to :foo, :polymorphic => true
33
+ end
34
+
35
+ class Blog < ActiveRecord::Base
36
+ has_many :sub_articles, :foreign_key => :foo_id
37
+ end
38
+
39
+ class SubArticle < Article
40
+ belongs_to :blog, :class_name => "Blog", :foreign_key => :foo_id
41
+ end
42
+
43
+ Aam.logger = nil
44
+
45
+ if true
46
+ require "i18n"
47
+ I18n.enforce_available_locales = false
48
+ I18n.default_locale = :ja
49
+ I18n.backend.store_translations :ja, {:activerecord => {:models => {"sub_article" => "なんとか"} }, :attributes => {"user_id" => "ユーザー"}}
50
+ end
51
+
52
+ puts Aam::SchemaInfoGenerator.new(User).generate
53
+ puts Aam::SchemaInfoGenerator.new(Article).generate
54
+ puts Aam::SchemaInfoGenerator.new(Blog).generate
55
+ puts Aam::SchemaInfoGenerator.new(SubArticle).generate
56
+
57
+ # >> # == Schema Information ==
58
+ # >> #
59
+ # >> # Userテーブル (users as User)
60
+ # >> #
61
+ # >> # +----------+------+---------+-------------+------+-------+
62
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
63
+ # >> # +----------+------+---------+-------------+------+-------+
64
+ # >> # | id | Id | integer | NOT NULL PK | | |
65
+ # >> # | name | Name | string | | | A! |
66
+ # >> # | flag | Flag | boolean | DEFAULT(f) | | |
67
+ # >> # +----------+------+---------+-------------+------+-------+
68
+ # >> # == Schema Information ==
69
+ # >> #
70
+ # >> # Articleテーブル (articles as Article)
71
+ # >> #
72
+ # >> # +----------+----------+---------+-------------+-----------------------+-------+
73
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
74
+ # >> # +----------+----------+---------+-------------+-----------------------+-------+
75
+ # >> # | id | Id | integer | NOT NULL PK | | |
76
+ # >> # | user_id | ユーザー | integer | | => User#id | A |
77
+ # >> # | foo_type | Foo type | string | | モデル名(polymorphic) | |
78
+ # >> # | foo_id | Foo | integer | | => (foo_type)#id | |
79
+ # >> # +----------+----------+---------+-------------+-----------------------+-------+
80
+ # >> #
81
+ # >> #- 備考 -------------------------------------------------------------------------
82
+ # >> # ・Article モデルは User モデルから has_many :articles されています。
83
+ # >> # ・【警告:インデックス欠如】create_articles マイグレーションに add_index :articles, [:foo_id, :foo_type] を追加してください
84
+ # >> #--------------------------------------------------------------------------------
85
+ # >> # == Schema Information ==
86
+ # >> #
87
+ # >> # Blogテーブル (blogs as Blog)
88
+ # >> #
89
+ # >> # +----------+------+---------+-------------+------+-------+
90
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
91
+ # >> # +----------+------+---------+-------------+------+-------+
92
+ # >> # | id | Id | integer | NOT NULL PK | | |
93
+ # >> # | name | Name | string | | | |
94
+ # >> # +----------+------+---------+-------------+------+-------+
95
+ # >> # == Schema Information ==
96
+ # >> #
97
+ # >> # なんとかテーブル (articles as SubArticle)
98
+ # >> #
99
+ # >> # +----------+----------+---------+-------------+--------------------------------------+-------+
100
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
101
+ # >> # +----------+----------+---------+-------------+--------------------------------------+-------+
102
+ # >> # | id | Id | integer | NOT NULL PK | | |
103
+ # >> # | user_id | ユーザー | integer | | => User#id | A |
104
+ # >> # | foo_type | Foo type | string | | モデル名(polymorphic) | |
105
+ # >> # | foo_id | Foo | integer | | :blog => Blog#id と => (foo_type)#id | |
106
+ # >> # +----------+----------+---------+-------------+--------------------------------------+-------+
107
+ # >> #
108
+ # >> #- 備考 -------------------------------------------------------------------------
109
+ # >> # ・SubArticle モデルは User モデルから has_many :articles されています。
110
+ # >> # ・【警告:インデックス欠如】create_articles マイグレーションに add_index :articles, [:foo_id, :foo_type] を追加してください
111
+ # >> # ・SubArticle モデルは Blog モデルから has_many :sub_articles, :foreign_key => :foo_id されています。
112
+ # >> #--------------------------------------------------------------------------------
@@ -0,0 +1,121 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "aam"
3
+
4
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
5
+ ActiveRecord::Schema.define do
6
+ suppress_messages do
7
+ create_table(:users) do |t|
8
+ t.string :name, :limit => 32
9
+ t.boolean :flag, :default => false
10
+ end
11
+ add_index :users, :name, :unique => true
12
+
13
+ create_table(:articles) do |t|
14
+ t.belongs_to :user, :index => false
15
+ t.belongs_to :xxx, :polymorphic => true, :index => false
16
+ end
17
+
18
+ create_table(:blogs) do |t|
19
+ t.string :name
20
+ end
21
+
22
+ create_table(:foos) do |t|
23
+ t.belongs_to :user, :index => false
24
+ t.belongs_to :xxx, :polymorphic => true, :index => false
25
+ end
26
+ end
27
+ end
28
+
29
+ class User < ActiveRecord::Base
30
+ has_many :articles
31
+ has_one :article, :as => :xxx
32
+ end
33
+
34
+ class Article < ActiveRecord::Base
35
+ belongs_to :user
36
+ belongs_to :xxx, :polymorphic => true
37
+ end
38
+
39
+ class Blog < ActiveRecord::Base
40
+ has_many :sub_articles, :foreign_key => :xxx_id
41
+ end
42
+
43
+ class SubArticle < Article
44
+ belongs_to :blog, :class_name => "Blog", :foreign_key => :xxx_id
45
+ end
46
+
47
+ class Foo < ActiveRecord::Base
48
+ end
49
+
50
+ Aam.logger = nil
51
+ puts Aam::SchemaInfoGenerator.new(User).generate
52
+ puts Aam::SchemaInfoGenerator.new(Article).generate
53
+ puts Aam::SchemaInfoGenerator.new(SubArticle).generate
54
+ puts Aam::SchemaInfoGenerator.new(Foo).generate
55
+ # >> # == Schema Information ==
56
+ # >> #
57
+ # >> # Userテーブル (users as User)
58
+ # >> #
59
+ # >> # |----------+------+------------+-------------+------+-------|
60
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
61
+ # >> # |----------+------+------------+-------------+------+-------|
62
+ # >> # | id | Id | integer | NOT NULL PK | | |
63
+ # >> # | name | Name | string(32) | | | A! |
64
+ # >> # | flag | Flag | boolean | DEFAULT(f) | | |
65
+ # >> # |----------+------+------------+-------------+------+-------|
66
+ # >> # == Schema Information ==
67
+ # >> #
68
+ # >> # Articleテーブル (articles as Article)
69
+ # >> #
70
+ # >> # |----------+----------+---------+-------------+-----------------------+-------|
71
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
72
+ # >> # |----------+----------+---------+-------------+-----------------------+-------|
73
+ # >> # | id | Id | integer | NOT NULL PK | | |
74
+ # >> # | user_id | User | integer | | => User#id | |
75
+ # >> # | xxx_type | Xxx type | string | | モデル名(polymorphic) | |
76
+ # >> # | xxx_id | Xxx | integer | | => (xxx_type)#id | |
77
+ # >> # |----------+----------+---------+-------------+-----------------------+-------|
78
+ # >> #
79
+ # >> #- 備考 -------------------------------------------------------------------------
80
+ # >> # ・【警告:インデックス欠如】create_articles マイグレーションに add_index :articles, :user_id を追加してください
81
+ # >> # ・Article モデルは User モデルから has_many :articles されています。
82
+ # >> # ・【警告:インデックス欠如】create_articles マイグレーションに add_index :articles, [:xxx_id, :xxx_type] を追加してください
83
+ # >> #--------------------------------------------------------------------------------
84
+ # >> # == Schema Information ==
85
+ # >> #
86
+ # >> # Sub articleテーブル (articles as SubArticle)
87
+ # >> #
88
+ # >> # |----------+----------+---------+-------------+--------------------------------------+-------|
89
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
90
+ # >> # |----------+----------+---------+-------------+--------------------------------------+-------|
91
+ # >> # | id | Id | integer | NOT NULL PK | | |
92
+ # >> # | user_id | User | integer | | => User#id | |
93
+ # >> # | xxx_type | Xxx type | string | | モデル名(polymorphic) | |
94
+ # >> # | xxx_id | Xxx | integer | | :blog => Blog#id と => (xxx_type)#id | |
95
+ # >> # |----------+----------+---------+-------------+--------------------------------------+-------|
96
+ # >> #
97
+ # >> #- 備考 -------------------------------------------------------------------------
98
+ # >> # ・【警告:インデックス欠如】create_articles マイグレーションに add_index :articles, :user_id を追加してください
99
+ # >> # ・SubArticle モデルは User モデルから has_many :articles されています。
100
+ # >> # ・【警告:インデックス欠如】create_articles マイグレーションに add_index :articles, [:xxx_id, :xxx_type] を追加してください
101
+ # >> # ・SubArticle モデルは Blog モデルから has_many :sub_articles, :foreign_key => :xxx_id されています。
102
+ # >> #--------------------------------------------------------------------------------
103
+ # >> # == Schema Information ==
104
+ # >> #
105
+ # >> # Fooテーブル (foos as Foo)
106
+ # >> #
107
+ # >> # |----------+----------+---------+-------------+------+-------|
108
+ # >> # | カラム名 | 意味 | タイプ | 属性 | 参照 | INDEX |
109
+ # >> # |----------+----------+---------+-------------+------+-------|
110
+ # >> # | id | Id | integer | NOT NULL PK | | |
111
+ # >> # | user_id | User | integer | | | |
112
+ # >> # | xxx_type | Xxx type | string | | | |
113
+ # >> # | xxx_id | Xxx | integer | | | |
114
+ # >> # |----------+----------+---------+-------------+------+-------|
115
+ # >> #
116
+ # >> #- 備考 -------------------------------------------------------------------------
117
+ # >> # ・【警告:インデックス欠如】create_foos マイグレーションに add_index :foos, :user_id を追加してください
118
+ # >> # ・【警告】Foo モデルに belongs_to :user を追加してください
119
+ # >> # ・【警告:インデックス欠如】create_foos マイグレーションに add_index :foos, [:xxx_id, :xxx_type] を追加してください
120
+ # >> # ・【警告】Foo モデルに belongs_to :xxx, :polymorphic => true を追加してください
121
+ # >> #--------------------------------------------------------------------------------