schema_comments 0.2.0 → 0.3.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.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +13 -0
  4. data/Gemfile +11 -8
  5. data/README.md +187 -0
  6. data/Rakefile +6 -0
  7. data/bin/console +14 -0
  8. data/bin/setup +7 -0
  9. data/gemfiles/.gitignore +1 -0
  10. data/gemfiles/Gemfile.rails-4.0 +15 -0
  11. data/gemfiles/Gemfile.rails-4.1 +12 -0
  12. data/gemfiles/Gemfile.rails-4.2 +12 -0
  13. data/lib/annotate_models.rb +12 -4
  14. data/lib/schema_comments/connection_adapters.rb +2 -1
  15. data/lib/schema_comments/railtie.rb +11 -0
  16. data/lib/schema_comments/schema_comment.rb +1 -1
  17. data/lib/schema_comments/schema_dumper/mysql.rb +2 -2
  18. data/lib/schema_comments/version.rb +3 -0
  19. data/lib/schema_comments.rb +2 -0
  20. data/schema_comments.gemspec +28 -0
  21. metadata +52 -194
  22. data/Gemfile.lock +0 -106
  23. data/README.rdoc +0 -150
  24. data/spec/annotate_models_spec.rb +0 -63
  25. data/spec/database.yml +0 -15
  26. data/spec/fake_app.rb +0 -38
  27. data/spec/hash_key_orderable_spec.rb +0 -50
  28. data/spec/human_readable_schema_comments.yml +0 -14
  29. data/spec/i18n_export_spec.rb +0 -44
  30. data/spec/migration_spec.rb +0 -108
  31. data/spec/migrations/valid/001_create_products.rb +0 -17
  32. data/spec/migrations/valid/002_rename_products.rb +0 -10
  33. data/spec/migrations/valid/003_rename_products_again.rb +0 -10
  34. data/spec/migrations/valid/004_remove_price.rb +0 -10
  35. data/spec/migrations/valid/005_change_products_name.rb +0 -10
  36. data/spec/migrations/valid/006_change_products_name_with_comment.rb +0 -10
  37. data/spec/migrations/valid/007_change_comments.rb +0 -12
  38. data/spec/migrations/valid/008_create_users_without_comment.rb +0 -19
  39. data/spec/resources/models/product.rb +0 -2
  40. data/spec/resources/models/product_name.rb +0 -2
  41. data/spec/schema.rb +0 -2
  42. data/spec/schema_comments/connection_adapters_spec.rb +0 -54
  43. data/spec/schema_comments/schema_comment_spec.rb +0 -76
  44. data/spec/schema_comments/schema_comments.yml +0 -10
  45. data/spec/schema_comments/schema_comments_broken_column_comments.yml +0 -7
  46. data/spec/schema_comments/schema_comments_broken_column_hash.yml +0 -10
  47. data/spec/schema_comments/schema_comments_broken_table_comments.yml +0 -13
  48. data/spec/schema_comments/schema_comments_users_without_column_hash.yml +0 -10
  49. data/spec/schema_comments/schema_dumper_spec.rb +0 -92
  50. data/spec/schema_comments.yml +0 -9
  51. data/spec/spec_helper.rb +0 -53
  52. data/spec/yaml_export_spec.rb +0 -52
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 24b3283a263104ee9046e1997be8c81c8147833a
4
+ data.tar.gz: cb88b5c63f9beee0dda6e4a5fa914a46a338ea8c
5
+ SHA512:
6
+ metadata.gz: 02a04a92bf9a60b4830636e8788eb561bafb454a80ebdeec79d2960e473e96c4d302b50d80fbd739f8c3832e2d2b4ff4eed4873c95b5812f039887108749b124
7
+ data.tar.gz: d344cb682fcd641ca133968b395cb9ef4bfa533af50cd9e7a70773efb66688b19b703a61d36dd86b4c15d6c55e17615a3b1eb2adeda562d00e0bb036e7983567
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.log
2
+ *~
3
+ /.rbenv-version
4
+ /.rvmrc
5
+ /.yardoc
6
+ /coverage
7
+ /doc
8
+ /pkg
9
+ /selectable_attr_test.sqlite3.db
10
+ /Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ script: "bundle exec rake spec"
2
+ rvm:
3
+ # - 2.0.0 # it can work but spec fails
4
+ - 2.1.6
5
+ - 2.2.2
6
+ gemfile:
7
+ - gemfiles/Gemfile.rails-4.0
8
+ - gemfiles/Gemfile.rails-4.1
9
+ - gemfiles/Gemfile.rails-4.2
10
+ env:
11
+ - DB=sqlite3
12
+ - DB=mysql2
13
+ before_install: gem install bundler --version '>= 1.9.0'
data/Gemfile CHANGED
@@ -1,12 +1,15 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- group :test do
4
- gem "sqlite3", :platform => :ruby
5
- gem "mysql" , (RUBY_VERSION == '1.9.2' ? "~> 2.8.1" : ">= 0"), :platform => :ruby
6
- gem "mysql2" , :platform => :ruby
3
+ gemspec
7
4
 
8
- # gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
9
- # gem "activerecord-jdbcmysql-adapter" , :platform => :jruby
10
- end
5
+ group :development do
6
+ gem "sqlite3"
7
+ gem "mysql2"
8
+ gem "rails"
11
9
 
12
- gemspec
10
+ gem "pry"
11
+ gem "pry-byebug"
12
+ gem "pry-stack_explorer"
13
+ gem "simplecov"
14
+ gem "fuubar"
15
+ end
data/README.md ADDED
@@ -0,0 +1,187 @@
1
+ # SchemaComments [![Build Status](https://secure.travis-ci.org/akm/schema_comments.png)](http://travis-ci.org/akm/schema_comments)
2
+
3
+ ## Install
4
+
5
+ ### With Bundler
6
+ add this line into Gemfile
7
+
8
+ gem "schema_comments"
9
+
10
+ And do bundle install
11
+
12
+ bundle install
13
+
14
+ ## Install(old)
15
+
16
+ ### as a plugin
17
+
18
+ ruby script/plugin install git://github.com/akm/schema_comments.git
19
+
20
+ ### as a gem
21
+ insert following line to config/environment.rb
22
+
23
+ config.gem 'schema_comments', :version => '0.2.0'
24
+
25
+ and
26
+
27
+ $ sudo rake gems:install
28
+
29
+ Or install gem manually
30
+
31
+ $ sudo gem install schema_comments
32
+
33
+ And make lib/tasks/schema_comments.rake
34
+
35
+ require 'schema_comments/task'
36
+
37
+ ## Configuration for Rails App
38
+ 1. make lib/tasks/schema_comments.rake
39
+ 2. edit the file like following
40
+
41
+ require 'schema_comments/task'
42
+ SchemaComments.yaml_path = File.expand_path("../../../db/schema_comments.yml", __FILE__)
43
+
44
+
45
+ ## Configuration (old)
46
+ If you install schema_comments as a gem, must create config/initializers/schema_comments.rb like this:
47
+
48
+ require 'schema_comments'
49
+ SchemaComments.setup
50
+
51
+
52
+ ## Overview
53
+ schema_commentsプラグインを使うと、テーブルとカラムにコメントを記述することができます。
54
+
55
+ class CreateProducts < ActiveRecord::Migration
56
+ def self.up
57
+ create_table "products", :comment => '商品' do |t|
58
+ t.string "product_type_cd", :comment => '種別コード'
59
+ t.integer "price", :comment => "価格"
60
+ t.string "name", :comment => "商品名"
61
+ t.datetime "created_at", :comment => "登録日時"
62
+ t.datetime "updated_at", :comment => "更新日時"
63
+ end
64
+ end
65
+
66
+ def self.down
67
+ drop_table "products"
68
+ end
69
+ end
70
+
71
+ こんな感じ。
72
+
73
+ でこのようなマイグレーションを実行すると、db/schema.rb には、
74
+ コメントが設定されているテーブル、カラムは以下のように出力されます。
75
+
76
+ ActiveRecord::Schema.define(:version => 0) do
77
+ create_table "products", :force => true, :comment => '商品' do |t|
78
+ t.string "product_type_cd", :comment => '種別コード'
79
+ t.integer "price", :comment => "価格"
80
+ t.string "name", :comment => "商品名"
81
+ t.datetime "created_at", :comment => "登録日時"
82
+ t.datetime "updated_at", :comment => "更新日時"
83
+ end
84
+ end
85
+
86
+
87
+ コメントは、以下のメソッドで使用することが可能です。
88
+
89
+ columns, create_table, drop_table, rename_table
90
+ remove_column, add_column, change_column
91
+
92
+
93
+ ## コメントはどこに保存されるのか
94
+ db/schema_comments.yml にYAML形式で保存されます。
95
+ あまり推奨しませんが、もしマイグレーションにコメントを記述するのを忘れてしまった場合、db/schema_comments.yml
96
+ を直接編集した後、rake db:schema:dumpやマイグレーションを実行すると、db/schema.rbのコメントに反映されます。
97
+
98
+
99
+ ## I18nへの対応
100
+
101
+ rake i18n:schema_comments:update_config_locale
102
+
103
+ このタスクを実行すると、i18n用のYAMLを更新できます。
104
+
105
+ rake i18n:schema_comments:update_config_locale LOCALE=ja
106
+
107
+ でデフォルトではconfig/locales/ja.ymlを更新します。
108
+
109
+ 毎回LOCALEを指定するのが面倒な場合は、config/initializers/locale.rb に
110
+
111
+ I18n.default_locale = 'ja'
112
+
113
+ という記述を追加しておくと良いでしょう。
114
+
115
+ また出力先のYAMLのPATHを指定したい場合、YAML_PATHで指定が可能です。
116
+
117
+ rake i18n:schema_comments:update_config_locale LOCALE=ja YAML_PATH=/path/to/yaml
118
+
119
+ ### コメント内コメント
120
+ コメント中の ((( から ))) は反映されませんので、モデル名/属性名に含めたくない箇所は ((( と ))) で括ってください。
121
+ ((( ))) と同様に[[[ ]]]も使用できます。
122
+ 例えば以下のようにdb/schema.rbに出力されている場合、
123
+
124
+ ActiveRecord::Schema.define(:version => 0) do
125
+ create_table "products", :force => true, :comment => '商品' do |t|
126
+ t.string "product_type_cd", :comment => '種別コード(((01:書籍, 02:靴, 03:パソコン)))'
127
+ t.integer "price", :comment => "価格"
128
+ t.string "name", :comment => "商品名"
129
+ t.datetime "created_at", :comment => "登録日時"
130
+ t.datetime "updated_at", :comment => "更新日時"
131
+ end
132
+ end
133
+
134
+
135
+ rake i18n:schema_comments:update_config_locale LOCALE=ja
136
+
137
+ とすると、以下のように出力されます。
138
+
139
+ ja:
140
+ activerecord:
141
+ attributes:
142
+ product:
143
+ product_type_cd: "種別コード"
144
+ price: "価格"
145
+ name: "商品名"
146
+ created_at: "登録日時"
147
+ updated_at: "更新日時"
148
+
149
+
150
+
151
+ ## MySQLのビュー
152
+ MySQLのビューを使用した場合、元々MySQLではSHOW TABLES でビューも表示してしまうため、
153
+ ビューはテーブルとしてSchemaDumperに認識され、development環境ではMySQLのビューとして作成されているのに、
154
+ test環境ではテーブルとして作成されてしまい、テストが正しく動かないことがあります。
155
+ これを避けるため、schema_commentsでは、db/schema.rbを出力する際、テーブルに関する記述の後に、CREATE VIEWを行う記述を追加します。
156
+
157
+
158
+ ## annotate_models
159
+ rake db:annotate で以下のようなコメントを、モデル、テスト、フィクスチャといったモデルに関係の強いファイルの
160
+ 先頭に追加します。
161
+
162
+ # == Schema Info
163
+ #
164
+ # Schema version: 20090721185959
165
+ #
166
+ # Table name: books # 書籍
167
+ #
168
+ # id :integer not null, primary key
169
+ # title :string(100) not null # タイトル
170
+ # size :integer not null, default(1) # 判型
171
+ # price :decimal(17, 14) not null, default(0.0) # 価格
172
+ # created_at :datetime # 登録日時
173
+ # updated_at :datetime # 更新日時
174
+ #
175
+ # =================
176
+ #
177
+
178
+ また、rake db:updateで、rake db:migrateとrake db:annotateを実行します。
179
+
180
+ annotate_modelsは、達人プログラマーのDave Thomasさんが公開しているプラグインです。
181
+ http://repo.pragprog.com/svn/Public/plugins/annotate_models/
182
+
183
+ 本プラグインでは、それを更に拡張したDave Boltonさんのプラグイン(
184
+ http://github.com/rotuka/annotate_models )をベースに拡張を加えています。
185
+
186
+ ## License
187
+ Copyright (c) 2008 Takeshi AKIMA, released under the Ruby License
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "schema_comments"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ /Gemfile.*.lock
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activesupport", "~> 4.0.0"
4
+ gem "activerecord" , "~> 4.0.0"
5
+
6
+ group :test do
7
+ gem "sqlite3", :platform => :ruby
8
+ gem "mysql2" , :platform => :ruby
9
+ gem "rails"
10
+
11
+ # gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
12
+ # gem "activerecord-jdbcmysql-adapter" , :platform => :jruby
13
+ end
14
+
15
+ gemspec :path => '..'
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activesupport", "~> 4.1.0"
4
+ gem "activerecord" , "~> 4.1.0"
5
+
6
+ group :test do
7
+ gem "sqlite3", :platform => :ruby
8
+ gem "mysql2" , :platform => :ruby
9
+ gem "rails"
10
+ end
11
+
12
+ gemspec :path => '..'
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activesupport", "~> 4.2.0"
4
+ gem "activerecord" , "~> 4.2.0"
5
+
6
+ group :test do
7
+ gem "sqlite3", :platform => :ruby
8
+ gem "mysql2" , :platform => :ruby
9
+ gem "rails"
10
+ end
11
+
12
+ gemspec :path => '..'
@@ -56,7 +56,7 @@ module AnnotateModels
56
56
  when NilClass then "NULL"
57
57
  when TrueClass then "TRUE"
58
58
  when FalseClass then "FALSE"
59
- when Float, Fixnum, Bignum then value.to_s
59
+ when Float, Fixnum, Bignum then value.to_s.inspect
60
60
  # BigDecimals need to be output in a non-normalized form and quoted.
61
61
  when BigDecimal then value.to_s('F')
62
62
  else
@@ -98,13 +98,21 @@ module AnnotateModels
98
98
  end
99
99
 
100
100
  def self.annotate_column(col, klass, max_size)
101
+ col_type = col.type.to_s
101
102
  attrs = []
102
103
  attrs << "not null" unless col.null
103
- attrs << "default(#{quote(col.default)})" if col.default
104
+ if col.default
105
+ default_value =
106
+ case col_type
107
+ when "decimal" then col.default.to_s.sub(/\.0+\z/, '.0')
108
+ else col.default
109
+ end
110
+ attrs << "default(#{quote(default_value)})"
111
+ end
104
112
  attrs << "primary key" if col.name == klass.primary_key
105
113
 
106
- col_type = col.type.to_s
107
- if col_type == "decimal"
114
+ case col_type
115
+ when "decimal" then
108
116
  col_type << "(#{col.precision}, #{col.scale})"
109
117
  else
110
118
  col_type << "(#{col.limit})" if col.limit
@@ -94,8 +94,9 @@ module SchemaComments
94
94
  end
95
95
  end
96
96
 
97
+ #TODO: columnsメソッドに第二引数移行がないので本来は消すべき?
97
98
  def columns_with_schema_comments(table_name, name = nil, &block)
98
- result = columns_without_schema_comments(table_name, name, &block)
99
+ result = columns_without_schema_comments(table_name)
99
100
  column_comment_hash = column_comments(table_name)
100
101
  result.each do |column|
101
102
  column.comment = column_comment_hash[column.name]
@@ -0,0 +1,11 @@
1
+ require 'schema_comments'
2
+
3
+ require 'rails'
4
+
5
+ module SchemaScomments
6
+ class Railtie < ::Rails::Railtie
7
+ rake_tasks do
8
+ require 'schema_comments/task'
9
+ end
10
+ end
11
+ end
@@ -133,7 +133,7 @@ module SchemaComments
133
133
  column_hash ||= {}
134
134
  column_names = nil
135
135
  begin
136
- columns = ActiveRecord::Base.connection.columns_without_schema_comments(table_name, "#{table_name.classify} Columns")
136
+ columns = ActiveRecord::Base.connection.columns_without_schema_comments(table_name)
137
137
  column_names = columns.map(&:name)
138
138
  rescue ActiveRecord::ActiveRecordError
139
139
  column_names = column_hash.keys.sort
@@ -35,7 +35,7 @@ module SchemaComments
35
35
  end
36
36
 
37
37
  def header(stream)
38
- define_params = @version ? ":version => #{@version}" : ""
38
+ define_params = @version ? "version: #{@version}" : ""
39
39
 
40
40
  if stream.respond_to?(:external_encoding)
41
41
  stream.puts "# encoding: #{stream.external_encoding.name}"
@@ -52,7 +52,7 @@ module SchemaComments
52
52
  # from scratch. The latter is a flawed and unsustainable approach (the more migrations
53
53
  # you'll amass, the slower it'll run and the greater likelihood for issues).
54
54
  #
55
- # It's strongly recommended to check this file into your version control system.
55
+ # It's strongly recommended that you check this file into your version control system.
56
56
 
57
57
  ActiveRecord::Schema.define(#{define_params}) do
58
58
 
@@ -0,0 +1,3 @@
1
+ module SchemaComments
2
+ VERSION = "0.3.0"
3
+ end
@@ -1,5 +1,7 @@
1
1
  require 'active_support/core_ext/module'
2
2
 
3
+ require 'schema_comments/railtie'
4
+
3
5
  module SchemaComments
4
6
  VERSION = File.read(File.expand_path("../../VERSION", __FILE__))
5
7
 
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'schema_comments/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "schema_comments"
8
+ spec.version = SchemaComments::VERSION
9
+ spec.authors = ["akm"]
10
+ spec.email = ["akm2000@gmail.com"]
11
+
12
+ spec.summary = %q{schema_comments generates extra methods dynamically for attribute which has options.}
13
+ spec.description = %q{schema_comments generates extra methods dynamically for attribute which has options.}
14
+ spec.homepage = "http://github.com/akm/schema_comments"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency('activesupport', ">= 4.0.0")
23
+ spec.add_runtime_dependency('activerecord', ">= 4.0.0")
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.9"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+ end