schema_comments 0.4.3 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1d1635ab2f5896095a1be697387e369586309f2
4
- data.tar.gz: c8735f51f37748a54620b014c3b95e6dcf16ac5d
3
+ metadata.gz: 3e86f31a10d539ff6c38570c26e42ae901c381d0
4
+ data.tar.gz: 2cbd891ce4625a00b234429c4eb33f5a5841cdf4
5
5
  SHA512:
6
- metadata.gz: bfb57df30717ea9c95a48614d0f82123dcd54f9b27f4203fd8431ea86b86570b4907b99a60deb629fb732340abb382ce5c5328929774e4ec66cdefdde20167dd
7
- data.tar.gz: 5f41213ba5f2755be515609a4f6132f8a2fc4bbe9a60b14b192dd79efeaf761d1a50df57a60684f7f01081cb80e5f1c662b9efad6ce6b2cb2d032fc1851f4d3c
6
+ metadata.gz: bb6b51e194b02b595810294dfaf6390404118809082e68694644f5c97e10286dcf61ad47b873220df946b12f7504aeef321f0111d53cbdca223b1cd6b45bbab1
7
+ data.tar.gz: 4c6c5a2cc31b4b7e3541739623054d5df3a6f7badcb8359038b383d534a30876435821974f469778e7771430afccb584b71b0e1c61f82fa46999a4d79ba19965
data/.gitignore CHANGED
@@ -1,11 +1,6 @@
1
1
  *.log
2
2
  *~
3
- /.rbenv-version
4
- /.rvmrc
5
- /.yardoc
6
3
  /coverage
7
- /doc
8
4
  /pkg
9
- /selectable_attr_test.sqlite3.db
10
5
  /Gemfile.lock
11
6
  /tmp
data/.rspec CHANGED
@@ -1 +1,2 @@
1
+ --format Fuubar
1
2
  --color
@@ -3,7 +3,6 @@ rvm:
3
3
  - 2.2.4
4
4
  - 2.3.0
5
5
  gemfile:
6
- - gemfiles/Gemfile.rails-4.0
7
6
  - gemfiles/Gemfile.rails-4.1
8
7
  - gemfiles/Gemfile.rails-4.2
9
8
  env:
data/Gemfile CHANGED
@@ -1,7 +1,18 @@
1
- source "http://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
+ # Declare your gem's dependencies in schema_comments.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
3
6
  gemspec
4
7
 
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use a debugger
14
+ # gem 'byebug', group: [:development, :test]
15
+
5
16
  group :development, :test do
6
17
  gem "sqlite3"
7
18
  gem "mysql2"
@@ -0,0 +1,20 @@
1
+ Copyright 2016 TODO: Write your name
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -43,10 +43,7 @@ class CreateProducts < ActiveRecord::Migration
43
43
  end
44
44
  ```
45
45
 
46
- こんな感じ。
47
-
48
- でこのようなマイグレーションを実行すると、db/schema.rb には、
49
- コメントが設定されているテーブル、カラムは以下のように出力されます。
46
+ このようなマイグレーションを実行すると、db/schema.rb のコメントが設定されているテーブル、カラムには以下のように出力されます。
50
47
 
51
48
  ```
52
49
  ActiveRecord::Schema.define(:version => 0) do
@@ -60,15 +57,16 @@ ActiveRecord::Schema.define(:version => 0) do
60
57
  end
61
58
  ```
62
59
 
63
- コメントは、以下のメソッドで使用することが可能です。
60
+ コメントは以下のメソッドで使用することが可能です。
64
61
 
65
62
  ```
66
63
  columns, create_table, drop_table, rename_table
67
- remove_column, add_column, change_column
64
+ remove_column, add_column, change_column, rename_column
68
65
  ```
69
66
 
70
67
 
71
68
  ## コメントはどこに保存されるのか
69
+
72
70
  db/schema_comments.yml にYAML形式で保存されます。
73
71
  あまり推奨しませんが、もしマイグレーションにコメントを記述するのを忘れてしまった場合、db/schema_comments.yml
74
72
  を直接編集した後、rake db:schema:dumpやマイグレーションを実行すると、db/schema.rbのコメントに反映されます。
@@ -76,69 +74,28 @@ db/schema_comments.yml にYAML形式で保存されます。
76
74
 
77
75
  ## I18nへの対応
78
76
 
79
- ```
80
- rake i18n:schema_comments:update_config_locale
81
- ```
82
-
83
- このタスクを実行すると、i18n用のYAMLを更新できます。
77
+ `schema_comments:i18n:update`タスクを実行すると、i18n用のYAMLを更新できます。
84
78
 
85
79
  ```
86
- rake i18n:schema_comments:update_config_locale LOCALE=ja
80
+ rake schema_comments:i18n:update
87
81
  ```
88
82
 
89
- でデフォルトではconfig/locales/ja.ymlを更新します。
83
+ 環境変数`LOCALE`で対象のロケールを指定可能ですが、指定されていなければ`I18n.locale`から取得します。
90
84
 
91
- 毎回LOCALEを指定するのが面倒な場合は、config/initializers/locale.rb
85
+ これは `config/application.rb` で以下のように指定可能です。
92
86
 
93
87
  ```
94
- I18n.default_locale = 'ja'
88
+ config.i18n.default_locale = :ja
95
89
  ```
96
90
 
97
- という記述を追加しておくと良いでしょう。
98
91
 
99
92
  また出力先のYAMLのPATHを指定したい場合、YAML_PATHで指定が可能です。
100
93
 
101
94
  ```
102
- rake i18n:schema_comments:update_config_locale LOCALE=ja YAML_PATH=/path/to/yaml
103
- ```
104
-
105
- ### コメント内コメント
106
- コメント中の ((( から ))) は反映されませんので、モデル名/属性名に含めたくない箇所は ((( と ))) で括ってください。
107
- ((( ))) と同様に[[[ ]]]も使用できます。
108
- 例えば以下のようにdb/schema.rbに出力されている場合、
109
-
110
- ```
111
- ActiveRecord::Schema.define(:version => 0) do
112
- create_table "products", :force => true, :comment => '商品' do |t|
113
- t.string "product_type_cd", :comment => '種別コード(((01:書籍, 02:靴, 03:パソコン)))'
114
- t.integer "price", :comment => "価格"
115
- t.string "name", :comment => "商品名"
116
- t.datetime "created_at", :comment => "登録日時"
117
- t.datetime "updated_at", :comment => "更新日時"
118
- end
119
- end
120
- ```
121
-
122
- ```
123
- $ rake i18n:schema_comments:update_config_locale LOCALE=ja
124
- ```
125
-
126
- とすると、以下のように出力されます。
127
-
128
- ```
129
- ja:
130
- activerecord:
131
- attributes:
132
- product:
133
- product_type_cd: "種別コード"
134
- price: "価格"
135
- name: "商品名"
136
- created_at: "登録日時"
137
- updated_at: "更新日時"
95
+ rake schema_comments:i18n:update LOCALE=ja YAML_PATH=/path/to/yaml
138
96
  ```
139
97
 
140
98
 
141
-
142
99
  ## MySQLのビュー
143
100
  MySQLのビューを使用した場合、元々MySQLではSHOW TABLES でビューも表示してしまうため、
144
101
  ビューはテーブルとしてSchemaDumperに認識され、development環境ではMySQLのビューとして作成されているのに、
@@ -146,35 +103,5 @@ test環境ではテーブルとして作成されてしまい、テストが正
146
103
  これを避けるため、schema_commentsでは、db/schema.rbを出力する際、テーブルに関する記述の後に、CREATE VIEWを行う記述を追加します。
147
104
 
148
105
 
149
- ## annotate_models
150
- rake db:annotate で以下のようなコメントを、モデル、テスト、フィクスチャといったモデルに関係の強いファイルの
151
- 先頭に追加します。
152
-
153
- ```
154
- # == Schema Info
155
- #
156
- # Schema version: 20090721185959
157
- #
158
- # Table name: books # 書籍
159
- #
160
- # id :integer not null, primary key
161
- # title :string(100) not null # タイトル
162
- # size :integer not null, default(1) # 判型
163
- # price :decimal(17, 14) not null, default(0.0) # 価格
164
- # created_at :datetime # 登録日時
165
- # updated_at :datetime # 更新日時
166
- #
167
- # =================
168
- #
169
- ```
170
-
171
- また、rake db:updateで、rake db:migrateとrake db:annotateを実行します。
172
-
173
- annotate_modelsは、達人プログラマーのDave Thomasさんが公開しているプラグインです。
174
- http://repo.pragprog.com/svn/Public/plugins/annotate_models/
175
-
176
- 本プラグインでは、それを更に拡張したDave Boltonさんのプラグインannotate_models
177
- ( リポジトリは削除された様子・・・ )をベースに拡張を加えています。
178
-
179
106
  ## License
180
- Copyright (c) 2008 Takeshi AKIMA, released under the Ruby License
107
+ Copyright (c) 2008 - 2016 Takeshi AKIMA, released under the Ruby License
data/Rakefile CHANGED
@@ -1,3 +1,9 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
1
7
  require "bundler/gem_tasks"
2
8
  require "rspec/core/rake_task"
3
9
 
@@ -7,6 +7,8 @@ group :test do
7
7
  gem "sqlite3", :platform => :ruby
8
8
  gem "mysql2" , "~> 0.3.13", :platform => :ruby
9
9
  gem "rails"
10
+ gem "fuubar"
11
+ gem "simplecov"
10
12
 
11
13
  # gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
12
14
  # gem "activerecord-jdbcmysql-adapter" , :platform => :jruby
@@ -7,6 +7,8 @@ group :development, :test do
7
7
  gem "sqlite3", :platform => :ruby
8
8
  gem "mysql2" , "~> 0.3.13", :platform => :ruby
9
9
  gem "rails"
10
+ gem "fuubar"
11
+ gem "simplecov"
10
12
  end
11
13
 
12
14
  gemspec :path => '..'
@@ -7,6 +7,8 @@ group :test do
7
7
  gem "sqlite3", :platform => :ruby
8
8
  gem "mysql2" , :platform => :ruby
9
9
  gem "rails"
10
+ gem "fuubar"
11
+ gem "simplecov"
10
12
  end
11
13
 
12
14
  gemspec :path => '..'
@@ -7,8 +7,7 @@ module SchemaComments
7
7
 
8
8
  autoload :Base , 'schema_comments/base'
9
9
  autoload :ConnectionAdapters, 'schema_comments/connection_adapters'
10
- autoload :Migration , 'schema_comments/migration'
11
- autoload :Migrator , 'schema_comments/migrator'
10
+ autoload :DummyMigration , 'schema_comments/dummy_migration'
12
11
  autoload :Schema , 'schema_comments/schema'
13
12
  autoload :SchemaComment , 'schema_comments/schema_comment'
14
13
  autoload :SchemaDumper , 'schema_comments/schema_dumper'
@@ -21,8 +20,12 @@ module SchemaComments
21
20
 
22
21
  class << self
23
22
  def setup
24
- base_names = %w(Base Migration Migrator Schema) +
25
- %w(Column ColumnDefinition TableDefinition).map{|name| "ConnectionAdapters::#{name}"}
23
+ defined?(Rails) && Rails.env.production? ? setup_on_production : setup_on_development
24
+ end
25
+
26
+ def setup_on_development
27
+ base_names = %w(Schema) +
28
+ %w(ColumnDefinition TableDefinition).map{|name| "ConnectionAdapters::#{name}"}
26
29
 
27
30
  base_names.each do |base_name|
28
31
  ar_class = "ActiveRecord::#{base_name}".constantize
@@ -51,6 +54,24 @@ module SchemaComments
51
54
  end
52
55
  end
53
56
 
57
+ def setup_on_production
58
+ ActiveRecord::Migration.__send__(:prepend, DummyMigration)
59
+ end
60
+
61
+ [
62
+ :table_comment,
63
+ :column_comment,
64
+ :column_comments,
65
+ :save_table_comment,
66
+ :save_column_comment,
67
+ :destroy_of,
68
+ :update_table_name,
69
+ :update_column_name,
70
+ :clear_cache,
71
+ ].each do |m|
72
+ module_eval("def #{m}(*args); SchemaComment.#{m}(*args) end", __FILE__, __LINE__)
73
+ end
74
+
54
75
  end
55
76
 
56
77
  end
@@ -2,10 +2,6 @@
2
2
  module SchemaComments
3
3
  module ConnectionAdapters
4
4
 
5
- module Column
6
- attr_accessor :comment
7
- end
8
-
9
5
  module ColumnDefinition
10
6
  attr_accessor :comment
11
7
  end
@@ -76,16 +72,6 @@ module SchemaComments
76
72
  end
77
73
 
78
74
  module ConcreteAdapter
79
- #TODO: columnsメソッドに第二引数移行がないので本来は消すべき?
80
- def columns(table_name, name = nil, &block)
81
- result = super(table_name)
82
- column_comment_hash = column_comments(table_name)
83
- result.each do |column|
84
- column.comment = column_comment_hash[column.name]
85
- end
86
- result
87
- end
88
-
89
75
  def create_table(table_name, options = {}, &block)
90
76
  table_def = nil
91
77
  result = super(table_name, options) do |t|
@@ -0,0 +1,7 @@
1
+ module SchemaComments
2
+ module DummyMigration
3
+ def rename_column(table, old_col, new_col, options = {})
4
+ super(table, old_col, new_col)
5
+ end
6
+ end
7
+ end
@@ -6,6 +6,7 @@ module SchemaScomments
6
6
  class Railtie < ::Rails::Railtie
7
7
  rake_tasks do
8
8
  SchemaComments.yaml_path = Rails.root.join("db/schema_comments.yml").to_s
9
+ SchemaComments.setup
9
10
  require 'schema_comments/task'
10
11
  end
11
12
  end
@@ -11,19 +11,19 @@ module SchemaComments
11
11
 
12
12
  class << self
13
13
  def table_comment(table_name)
14
- @table_names ||= yaml_access{|db| db[TABLE_KEY]}.dup
14
+ @table_names ||= yaml_read{|db| db[TABLE_KEY]}.dup
15
15
  @table_names[table_name.to_s]
16
16
  end
17
17
 
18
18
  def column_comment(table_name, column_name)
19
- @column_names ||= yaml_access{|db| db[COLUMN_KEY] }.dup
19
+ @column_names ||= yaml_read{|db| db[COLUMN_KEY] }.dup
20
20
  column_hash = @column_names[table_name.to_s] || {}
21
21
  column_hash[column_name.to_s]
22
22
  end
23
23
 
24
24
  def column_comments(table_name)
25
25
  result = nil
26
- @column_names ||= yaml_access{|db| db[COLUMN_KEY] }.dup
26
+ @column_names ||= yaml_read{|db| db[COLUMN_KEY] }.dup
27
27
  result = @column_names[table_name.to_s]
28
28
  result || {}
29
29
  end
@@ -32,7 +32,7 @@ module SchemaComments
32
32
  yaml_access do |db|
33
33
  db[TABLE_KEY][table_name.to_s] = comment
34
34
  end
35
- @table_names = nil
35
+ clear_cache
36
36
  end
37
37
 
38
38
  def save_column_comment(table_name, column_name, comment)
@@ -40,7 +40,7 @@ module SchemaComments
40
40
  db[COLUMN_KEY][table_name.to_s] ||= {}
41
41
  db[COLUMN_KEY][table_name.to_s][column_name.to_s] = comment
42
42
  end
43
- @column_names = nil
43
+ clear_cache
44
44
  end
45
45
 
46
46
  def destroy_of(table_name, column_name)
@@ -48,7 +48,7 @@ module SchemaComments
48
48
  column_hash = db[COLUMN_KEY][table_name.to_s]
49
49
  column_hash.delete(column_name.to_s) if column_hash
50
50
  end
51
- @column_names = nil
51
+ clear_cache
52
52
  end
53
53
 
54
54
  def update_table_name(table_name, new_name)
@@ -56,8 +56,7 @@ module SchemaComments
56
56
  db[TABLE_KEY][new_name.to_s] = db[TABLE_KEY].delete(table_name.to_s)
57
57
  db[COLUMN_KEY][new_name.to_s] = db[COLUMN_KEY].delete(table_name.to_s)
58
58
  end
59
- @table_names = nil
60
- @column_names = nil
59
+ clear_cache
61
60
  end
62
61
 
63
62
  def update_column_name(table_name, column_name, new_name)
@@ -67,12 +66,45 @@ module SchemaComments
67
66
  table_cols[new_name.to_s] = table_cols.delete(column_name.to_s)
68
67
  end
69
68
  end
69
+ clear_cache
70
+ end
71
+
72
+ def model_comments
73
+ yaml_read{|db| db[TABLE_KEY] }.
74
+ each_with_object({}){|(k,v),d| d[k.singularize] = v }
75
+ end
76
+
77
+ def attribute_comments
78
+ yaml_read{|db| db[COLUMN_KEY] }.each_with_object({}) do |(k,v),d|
79
+ d[k.singularize] = v.each_with_object({}) do |(name, comment), dd|
80
+ dd[name.sub(/_id\z/, '')] = comment.sub(/id\z/i, '') if name =~ /_id\z/
81
+ dd[name] = comment
82
+ end
83
+ end
84
+ end
85
+
86
+ def activerecord_comments
87
+ {
88
+ 'activerecord' => {
89
+ 'models' => model_comments,
90
+ 'attributes' => attribute_comments,
91
+ }
92
+ }
93
+ end
94
+
95
+ def locale_yaml(locale)
96
+ YAML.dump({locale.to_s => activerecord_comments})
97
+ end
98
+
99
+ def clear_cache
70
100
  @table_names = nil
71
101
  @column_names = nil
102
+ self
72
103
  end
73
104
 
74
- def yaml_exist?
75
- File.exist?(SchemaComments.yaml_path)
105
+ def yaml_read(&block)
106
+ db = YAML.load_file(SchemaComments.yaml_path)
107
+ block_given? ? yield(db) : db
76
108
  end
77
109
 
78
110
  def yaml_access(&block)