schema_comments 0.1.3 → 0.1.4

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.
@@ -8,7 +8,7 @@ SchemaComments
8
8
 
9
9
  === as a gem
10
10
  insert following line to config/environment.rb
11
- config.gem 'schema_comments', :version => '0.1.3'
11
+ config.gem 'schema_comments', :version => '0.1.4'
12
12
  and
13
13
  $ sudo rake gems:install
14
14
 
data/Rakefile CHANGED
@@ -12,6 +12,7 @@ begin
12
12
  gem.authors = ["akimatter"]
13
13
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
14
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ gem.test_files = Dir.glob('spec/**/*.rb') + Dir.glob('spec/**/*.yml')
15
16
  end
16
17
  Jeweler::GemcutterTasks.new
17
18
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -15,7 +15,10 @@ module SchemaComments
15
15
  self.yaml_path = DEFAULT_YAML_PATH
16
16
 
17
17
  mattr_accessor :quiet
18
-
18
+
19
+ class YamlError < StandardError
20
+ end
21
+
19
22
  class << self
20
23
  def setup
21
24
  base_names = %w(Base Migration Migrator Schema SchemaDumper) +
@@ -3,6 +3,7 @@ require 'yaml/store'
3
3
  require 'hash_key_orderable'
4
4
 
5
5
  module SchemaComments
6
+
6
7
  # 現在はActiveRecord::Baseを継承していますが、将来移行が完全に終了した
7
8
  # 時点で、ActiveRecord::Baseの継承をやめます。
8
9
  #
@@ -128,8 +129,8 @@ module SchemaComments
128
129
  io.rewind
129
130
  root = YAML.load(io)
130
131
  end
131
- table_comments = root['table_comments']
132
- column_comments = root['column_comments']
132
+ table_comments = root['table_comments'] || {}
133
+ column_comments = root['column_comments'] || {}
133
134
  # 大元は
134
135
  # table_comments:
135
136
  # ...
@@ -138,17 +139,21 @@ module SchemaComments
138
139
  # その他
139
140
  # ...
140
141
  # の順番です。
142
+ raise YamlError, "Broken schame_comments.yml" unless root.is_a?(Hash)
141
143
  root.extend(HashKeyOrderable)
142
144
  root.key_order = %w(table_comments column_comments)
143
145
  # table_comments はテーブル名のアルファベット順
144
146
  table_names = ActiveRecord::Base.connection.tables.sort - ['schema_migrations']
147
+ raise YamlError, "Broken schame_comments.yml" unless table_comments.is_a?(Hash)
145
148
  table_comments.extend(HashKeyOrderable)
146
149
  table_comments.key_order = table_names
147
150
  # column_comments もテーブル名のアルファベット順
151
+ raise YamlError, "Broken schame_comments.yml" unless column_comments.is_a?(Hash)
148
152
  column_comments.extend(HashKeyOrderable)
149
153
  column_comments.key_order = table_names
150
154
  # column_comments の各値はテーブルのカラム順
151
155
  column_comments.each do |table_name, column_hash|
156
+ column_hash ||= {}
152
157
  column_names = nil
153
158
  begin
154
159
  columns = ActiveRecord::Base.connection.columns_without_schema_comments(table_name, "#{table_name.classify} Columns")
@@ -157,6 +162,7 @@ module SchemaComments
157
162
  column_names = column_hash.keys.sort
158
163
  end
159
164
  column_names.delete('id')
165
+ raise YamlError, "Broken schame_comments.yml" unless column_hash.is_a?(Hash)
160
166
  column_hash.extend(HashKeyOrderable)
161
167
  column_hash.key_order = column_names
162
168
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{schema_comments}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["akimatter"]
12
- s.date = %q{2010-04-13}
12
+ s.date = %q{2010-04-14}
13
13
  s.description = %q{schema_comments generates extra methods dynamically for attribute which has options}
14
14
  s.email = %q{akm2000@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -50,12 +50,18 @@ Gem::Specification.new do |s|
50
50
  "spec/migrations/valid/005_change_products_name.rb",
51
51
  "spec/migrations/valid/006_change_products_name_with_comment.rb",
52
52
  "spec/migrations/valid/007_change_comments.rb",
53
+ "spec/migrations/valid/008_create_users_without_comment.rb",
53
54
  "spec/rcov.opts",
54
55
  "spec/resources/models/product.rb",
55
56
  "spec/resources/models/product_name.rb",
56
57
  "spec/schema.rb",
57
58
  "spec/schema_comments/.gitignore",
58
59
  "spec/schema_comments/connection_adapters_spec.rb",
60
+ "spec/schema_comments/schema_comment_spec.rb",
61
+ "spec/schema_comments/schema_comments_broken_column_comments.yml",
62
+ "spec/schema_comments/schema_comments_broken_column_hash.yml",
63
+ "spec/schema_comments/schema_comments_broken_table_comments.yml",
64
+ "spec/schema_comments/schema_comments_users_without_column_hash.yml",
59
65
  "spec/schema_comments/schema_dumper_spec.rb",
60
66
  "spec/spec.opts",
61
67
  "spec/spec_helper.rb",
@@ -80,13 +86,23 @@ Gem::Specification.new do |s|
80
86
  "spec/migrations/valid/005_change_products_name.rb",
81
87
  "spec/migrations/valid/006_change_products_name_with_comment.rb",
82
88
  "spec/migrations/valid/007_change_comments.rb",
89
+ "spec/migrations/valid/008_create_users_without_comment.rb",
83
90
  "spec/resources/models/product.rb",
84
91
  "spec/resources/models/product_name.rb",
85
92
  "spec/schema.rb",
86
93
  "spec/schema_comments/connection_adapters_spec.rb",
94
+ "spec/schema_comments/schema_comment_spec.rb",
87
95
  "spec/schema_comments/schema_dumper_spec.rb",
88
96
  "spec/spec_helper.rb",
89
- "spec/yaml_export_spec.rb"
97
+ "spec/yaml_export_spec.rb",
98
+ "spec/database.yml",
99
+ "spec/human_readable_schema_comments.yml",
100
+ "spec/schema_comments/schema_comments.yml",
101
+ "spec/schema_comments/schema_comments_broken_column_comments.yml",
102
+ "spec/schema_comments/schema_comments_broken_column_hash.yml",
103
+ "spec/schema_comments/schema_comments_broken_table_comments.yml",
104
+ "spec/schema_comments/schema_comments_users_without_column_hash.yml",
105
+ "spec/schema_comments.yml"
90
106
  ]
91
107
 
92
108
  if s.respond_to? :specification_version then
@@ -3,10 +3,6 @@ require File.join(File.dirname(__FILE__), 'spec_helper')
3
3
 
4
4
  describe SchemaComments::Base do
5
5
 
6
- MIGRATIONS_ROOT = File.join(File.dirname(__FILE__), 'migrations')
7
-
8
- IGNORED_TABLES = %w(schema_migrations)
9
-
10
6
  before(:each) do
11
7
  SchemaComments.yaml_path = File.expand_path(File.join(File.dirname(__FILE__), 'schema_comments.yml'))
12
8
  FileUtils.rm(SchemaComments.yaml_path, :verbose => true) if File.exist?(SchemaComments.yaml_path)
@@ -3,10 +3,6 @@ require File.join(File.dirname(__FILE__), 'spec_helper')
3
3
 
4
4
  describe ActiveRecord::Migrator do
5
5
 
6
- MIGRATIONS_ROOT = File.join(File.dirname(__FILE__), 'migrations')
7
-
8
- IGNORED_TABLES = %w(schema_migrations)
9
-
10
6
  before(:each) do
11
7
  SchemaComments.yaml_path = File.expand_path(File.join(File.dirname(__FILE__), 'schema_comments.yml'))
12
8
  FileUtils.rm(SchemaComments.yaml_path, :verbose => true) if File.exist?(SchemaComments.yaml_path)
@@ -17,15 +13,15 @@ describe ActiveRecord::Migrator do
17
13
  ActiveRecord::Base.connection.initialize_schema_migrations_table
18
14
  ActiveRecord::Base.connection.execute "DELETE FROM #{ActiveRecord::Migrator.schema_migrations_table_name}"
19
15
  end
20
-
16
+
21
17
  it "test_valid_migration" do
22
18
  (ActiveRecord::Base.connection.tables - %w(schema_migrations)).should == []
23
-
19
+
24
20
  migration_path = File.join(MIGRATIONS_ROOT, 'valid')
25
21
  Dir.glob('*.rb').each do |file|
26
22
  require(file) if /^\d+?_.*/ =~ file
27
23
  end
28
-
24
+
29
25
  Product.reset_table_comments
30
26
  Product.reset_column_comments
31
27
 
@@ -34,7 +30,7 @@ describe ActiveRecord::Migrator do
34
30
  ActiveRecord::Migrator.current_version.should == 1
35
31
  Product.table_comment.should == '商品'
36
32
  {
37
- 'product_type_cd' => '種別コード',
33
+ 'product_type_cd' => '種別コード',
38
34
  "price" => "価格",
39
35
  "name" => "商品名",
40
36
  "created_at" => "登録日時",
@@ -42,17 +38,17 @@ describe ActiveRecord::Migrator do
42
38
  }.each do |col_name, comment|
43
39
  Product.columns.detect{|c| c.name.to_s == col_name}.comment.should == comment
44
40
  end
45
-
41
+
46
42
  ActiveRecord::Migrator.down(migration_path, 0)
47
43
  # SchemaComments::SchemaComment.count.should == 0
48
-
44
+
49
45
  ActiveRecord::Migrator.up(migration_path, 1)
50
46
  ActiveRecord::Migrator.up(migration_path, 2)
51
47
  ActiveRecord::Migrator.current_version.should == 2
52
-
48
+
53
49
  ProductName.table_comment.should == '商品'
54
50
  {
55
- 'product_type_cd' => '種別コード',
51
+ 'product_type_cd' => '種別コード',
56
52
  "price" => "価格",
57
53
  "name" => "商品名",
58
54
  "created_at" => "登録日時",
@@ -60,13 +56,13 @@ describe ActiveRecord::Migrator do
60
56
  }.each do |col_name, comment|
61
57
  ProductName.columns.detect{|c| c.name == col_name}.comment.should == comment
62
58
  end
63
-
59
+
64
60
  ActiveRecord::Migrator.down(migration_path, 1)
65
61
  ActiveRecord::Migrator.current_version.should == 1
66
-
62
+
67
63
  Product.table_comment.should == '商品'
68
64
  {
69
- 'product_type_cd' => '種別コード',
65
+ 'product_type_cd' => '種別コード',
70
66
  "price" => "価格",
71
67
  "name" => "商品名",
72
68
  "created_at" => "登録日時",
@@ -74,11 +70,11 @@ describe ActiveRecord::Migrator do
74
70
  }.each do |col_name, comment|
75
71
  Product.columns.detect{|c| c.name == col_name}.comment.should == comment
76
72
  end
77
-
73
+
78
74
  ActiveRecord::Migrator.up(migration_path, 4)
79
75
  ActiveRecord::Migrator.current_version.should == 4
80
76
  # SchemaComments::SchemaComment.count.should == 5
81
-
77
+
82
78
  ActiveRecord::Migrator.down(migration_path, 3)
83
79
  ActiveRecord::Migrator.current_version.should == 3
84
80
  # SchemaComments::SchemaComment.count.should == 6
@@ -104,6 +100,9 @@ describe ActiveRecord::Migrator do
104
100
  Product.reset_column_comments
105
101
  Product.columns.detect{|c| c.name == 'name'}.comment.should == '商品名称'
106
102
  Product.columns.detect{|c| c.name == 'product_type_cd'}.comment.should == 'カテゴリコード'
103
+
104
+ ActiveRecord::Migrator.up(migration_path, 8)
105
+ ActiveRecord::Migrator.current_version.should == 8
107
106
  end
108
-
107
+
109
108
  end
@@ -0,0 +1,19 @@
1
+ # -*- coding: utf-8 -*-
2
+ class CreateUsersWithoutComment < ActiveRecord::Migration
3
+ def self.up
4
+ create_table :users do |t|
5
+ t.string :login
6
+ t.string :email
7
+ t.string :crypted_password
8
+ t.string :password_salt
9
+ t.string :persistence_token
10
+ t.string :single_access_token
11
+ t.string :perishable_token
12
+ t.timestamps
13
+ end
14
+ end
15
+
16
+ def self.down
17
+ drop_table :users
18
+ end
19
+ end
@@ -0,0 +1,78 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), '../spec_helper')
3
+
4
+ describe SchemaComments::SchemaComment do
5
+
6
+ IGNORED_TABLES = %w(schema_migrations)
7
+
8
+ before(:each) do
9
+ SchemaComments.yaml_path = File.expand_path(File.join(File.dirname(__FILE__), 'schema_comments.yml'))
10
+ FileUtils.rm(SchemaComments.yaml_path, :verbose => true) if File.exist?(SchemaComments.yaml_path)
11
+
12
+ (ActiveRecord::Base.connection.tables - IGNORED_TABLES).each do |t|
13
+ ActiveRecord::Base.connection.drop_table(t) rescue nil
14
+ end
15
+ ActiveRecord::Base.connection.initialize_schema_migrations_table
16
+ ActiveRecord::Base.connection.execute "DELETE FROM #{ActiveRecord::Migrator.schema_migrations_table_name}"
17
+ end
18
+
19
+ describe :yaml_access do
20
+ before{@original_yaml_path = SchemaComments.yaml_path}
21
+ after {SchemaComments.yaml_path = @original_yaml_path}
22
+
23
+ # http://d.hatena.ne.jp/akm/20091213#c1271134505
24
+ # プラグインの更新ご苦労さまです。
25
+ # とても便利に使わせていただいてます。
26
+ #
27
+ # ところが本日更新してみたら0.1.3になっていて、
28
+ # コメントの生成に失敗するようになってしまいました。
29
+ #
30
+ # 原因を探ってみましたところ、
31
+ # コメントを一個も書いていないマイグレーションでは、
32
+ # nilにHashKeyOrderableをextendしようとして落ちていました。
33
+ #
34
+ # プラグインによって自動で作られるマイグレーションがあるのですが、
35
+ # 必ずしもコメントを書くとは限らないので、
36
+ # コメントがないときは無視?もしくはそのままカラム名をいれるのがいいのかなと思いました。
37
+ #
38
+ # # schema_comment.rb:154-164 あたり
39
+ #
40
+ # よろしければ対応していただけたらと思います。
41
+ it "dump without column comment" do
42
+ migration_path = File.join(MIGRATIONS_ROOT, 'valid')
43
+ Dir.glob('*.rb').each{|file| require(file) if /^\d+?_.*/ =~ file}
44
+
45
+ ActiveRecord::Migrator.up(migration_path, 8)
46
+ ActiveRecord::Migrator.current_version.should == 8
47
+
48
+ SchemaComments.yaml_path =
49
+ File.expand_path(File.join(
50
+ File.dirname(__FILE__), "schema_comments_users_without_column_hash.yml"))
51
+ SchemaComments::SchemaComment.yaml_access do |db|
52
+ db['column_comments']['products']['name'] = "商品名"
53
+ end
54
+ end
55
+ {
56
+ "table_comments" => lambda{|db| db['column_comments']['users']['login'] = "ログイン"},
57
+ "column_comments" => lambda{|db| db['table_comments']['users'] = "物品"},
58
+ "column_hash" => lambda{|db| db['column_comments']['users']['login'] = "ログイン"}
59
+ }.each do |broken_type, proc|
60
+ it "raise SchemaComments::YamlError with broken #{broken_type}" do
61
+ migration_path = File.join(MIGRATIONS_ROOT, 'valid')
62
+ Dir.glob('*.rb').each{|file| require(file) if /^\d+?_.*/ =~ file}
63
+
64
+ ActiveRecord::Migrator.up(migration_path, 8)
65
+ ActiveRecord::Migrator.current_version.should == 8
66
+
67
+ SchemaComments.yaml_path =
68
+ File.expand_path(File.join(
69
+ File.dirname(__FILE__), "schema_comments_broken_#{broken_type}.yml"))
70
+ lambda{
71
+ SchemaComments::SchemaComment.yaml_access(&proc)
72
+ }.should raise_error(SchemaComments::YamlError)
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ end
@@ -0,0 +1,7 @@
1
+ ---
2
+ table_comments:
3
+ products: "商品"
4
+ column_comments:
5
+ - "ここには"
6
+ - "配列ではなく"
7
+ - "Hashを期待しています"
@@ -0,0 +1,10 @@
1
+ ---
2
+ table_comments:
3
+ products: "商品"
4
+ column_comments:
5
+ products:
6
+ - "ここには"
7
+ - "配列ではなく"
8
+ - "Hashを期待しています"
9
+ users:
10
+ login: "ログインID"
@@ -0,0 +1,13 @@
1
+ ---
2
+ table_comments:
3
+ - "ここには"
4
+ - "配列ではなく"
5
+ - "Hashを期待しています"
6
+ column_comments:
7
+ products:
8
+ product_type_cd: "カテゴリコード"
9
+ name: "商品名称"
10
+ created_at: "登録日時"
11
+ updated_at: "更新日時"
12
+ users:
13
+ login: "ログインID"
@@ -0,0 +1,10 @@
1
+ ---
2
+ table_comments:
3
+ products: "商品"
4
+ column_comments:
5
+ products:
6
+ product_type_cd: "カテゴリコード"
7
+ name: "商品名"
8
+ created_at: "登録日時"
9
+ updated_at: "更新日時"
10
+ users:
@@ -21,9 +21,7 @@ describe ActiveRecord::SchemaDumper do
21
21
  (ActiveRecord::Base.connection.tables - %w(schema_migrations)).should == []
22
22
 
23
23
  migration_path = File.join(MIGRATIONS_ROOT, 'valid')
24
- Dir.glob('*.rb').each do |file|
25
- require(file) if /^\d+?_.*/ =~ file
26
- end
24
+ Dir.glob('*.rb').each{|file| require(file) if /^\d+?_.*/ =~ file}
27
25
 
28
26
  Product.reset_table_comments
29
27
  Product.reset_column_comments
@@ -42,5 +42,10 @@ unless defined?(RAILS_ENV)
42
42
 
43
43
  $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
44
44
  require File.join(File.dirname(__FILE__), '..', 'init')
45
+
46
+
47
+ MIGRATIONS_ROOT = File.join(File.dirname(__FILE__), 'migrations')
48
+
49
+ IGNORED_TABLES = %w(schema_migrations)
45
50
  end
46
51
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - akimatter
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-13 00:00:00 +09:00
17
+ date: 2010-04-14 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -74,12 +74,18 @@ files:
74
74
  - spec/migrations/valid/005_change_products_name.rb
75
75
  - spec/migrations/valid/006_change_products_name_with_comment.rb
76
76
  - spec/migrations/valid/007_change_comments.rb
77
+ - spec/migrations/valid/008_create_users_without_comment.rb
77
78
  - spec/rcov.opts
78
79
  - spec/resources/models/product.rb
79
80
  - spec/resources/models/product_name.rb
80
81
  - spec/schema.rb
81
82
  - spec/schema_comments/.gitignore
82
83
  - spec/schema_comments/connection_adapters_spec.rb
84
+ - spec/schema_comments/schema_comment_spec.rb
85
+ - spec/schema_comments/schema_comments_broken_column_comments.yml
86
+ - spec/schema_comments/schema_comments_broken_column_hash.yml
87
+ - spec/schema_comments/schema_comments_broken_table_comments.yml
88
+ - spec/schema_comments/schema_comments_users_without_column_hash.yml
83
89
  - spec/schema_comments/schema_dumper_spec.rb
84
90
  - spec/spec.opts
85
91
  - spec/spec_helper.rb
@@ -128,10 +134,20 @@ test_files:
128
134
  - spec/migrations/valid/005_change_products_name.rb
129
135
  - spec/migrations/valid/006_change_products_name_with_comment.rb
130
136
  - spec/migrations/valid/007_change_comments.rb
137
+ - spec/migrations/valid/008_create_users_without_comment.rb
131
138
  - spec/resources/models/product.rb
132
139
  - spec/resources/models/product_name.rb
133
140
  - spec/schema.rb
134
141
  - spec/schema_comments/connection_adapters_spec.rb
142
+ - spec/schema_comments/schema_comment_spec.rb
135
143
  - spec/schema_comments/schema_dumper_spec.rb
136
144
  - spec/spec_helper.rb
137
145
  - spec/yaml_export_spec.rb
146
+ - spec/database.yml
147
+ - spec/human_readable_schema_comments.yml
148
+ - spec/schema_comments/schema_comments.yml
149
+ - spec/schema_comments/schema_comments_broken_column_comments.yml
150
+ - spec/schema_comments/schema_comments_broken_column_hash.yml
151
+ - spec/schema_comments/schema_comments_broken_table_comments.yml
152
+ - spec/schema_comments/schema_comments_users_without_column_hash.yml
153
+ - spec/schema_comments.yml