schema_comments 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -80,7 +80,8 @@ module SchemaComments
80
80
  spec[:null] = 'false' unless column.null
81
81
  default = schema_default(column) if column.has_default?
82
82
  spec[:default] = default unless default.nil?
83
- spec[:comment] = '"' << (column.comment || '').gsub(/\"/, '\"') << '"' # ここでinspectを使うと最後の文字だけ文字化け(UTF-8のコード)になっちゃう
83
+ column_comment = SchemaComment.column_comment(table, column.name)
84
+ spec[:comment] = '"' << (column_comment || '').gsub(/\"/, '\"') << '"' # ここでinspectを使うと最後の文字だけ文字化け(UTF-8のコード)になっちゃう
84
85
  (spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.inspect} => ")}
85
86
  spec
86
87
  end.compact
@@ -120,6 +121,10 @@ module SchemaComments
120
121
  stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}"
121
122
  stream.puts "# #{e.message}"
122
123
  stream.puts
124
+ if ENV['RAILS_ENV'] == 'test'
125
+ $stderr.puts "[#{e.class.name}] #{e.message}"
126
+ $stderr.puts e.backtrace.join("\n ")
127
+ end
123
128
  end
124
129
 
125
130
  stream
@@ -161,13 +166,13 @@ module SchemaComments
161
166
  klass = usage[:class]
162
167
  method_caption = nil
163
168
  if col = usage[:class].columns.detect{|c| c.name == usage[:enum_name]}
164
- method_caption = col.comment
169
+ method_caption = SchemaComment.column_comment(klass.table_name, col.name)
165
170
  end
166
171
  method_caption ||= usage[:enum_name]
167
172
  spec = {
168
173
  :class_name => klass.name,
169
174
  :methos_name => usage[:enum_name],
170
- :class_caption => klass.table_comment,
175
+ :class_caption => SchemaComment.table_comment(klass.table_name),
171
176
  :method_caption => method_caption
172
177
  }
173
178
  specs << spec
@@ -92,7 +92,7 @@ HEADER
92
92
  end
93
93
  tbl.print ", :force => true"
94
94
 
95
- table_comment = @connection.table_comment(table)
95
+ table_comment = SchemaComment.table_comment(table)
96
96
  tbl.print ", :comment => '#{table_comment}'" unless table_comment.blank?
97
97
 
98
98
  tbl.puts " do |t|"
@@ -121,7 +121,8 @@ HEADER
121
121
  if column.name == pk
122
122
  spec[:comment] = '"AUTO_INCREMENT PRIMARY KEY by rails"'
123
123
  else
124
- spec[:comment] = '"' << (column.comment || '').gsub(/\"/, '\"') << '"' # ここでinspectを使うと最後の文字だけ文字化け(UTF-8のコード)になっちゃう
124
+ column_comment = SchemaComment.column_comment(table, column.name)
125
+ spec[:comment] = '"' << (column_comment || '').gsub(/\"/, '\"') << '"' # ここでinspectを使うと最後の文字だけ文字化け(UTF-8のコード)になっちゃう
125
126
  end
126
127
  (spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.inspect} => ")}
127
128
  spec
@@ -172,6 +173,10 @@ HEADER
172
173
  stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}"
173
174
  stream.puts "# #{e.message}"
174
175
  stream.puts
176
+ if ENV['RAILS_ENV'] == 'test'
177
+ $stderr.puts "[#{e.class.name}] #{e.message}"
178
+ $stderr.puts e.backtrace.join("\n ")
179
+ end
175
180
  end
176
181
 
177
182
  stream
@@ -1,168 +1,36 @@
1
- # -*- coding: utf-8 -*-
2
- require 'yaml'
3
- # require 'yaml_waml'
4
- require 'active_record'
5
- require 'schema_comments'
6
- SchemaComments.setup
7
-
8
- db_namespace = namespace :db do
9
- namespace :schema do
10
-
11
- Rake.application.send(:eval, "@tasks.delete('db:schema:dump')")
12
- desc 'Create a db/schema.rb file that can be portably used against any DB supported by AR'
13
- task :dump => [:environment, :load_config] do
14
- begin
15
- require 'active_record/schema_dumper'
16
- filename = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
17
- File.open(filename, "w:utf-8") do |file|
18
- ActiveRecord::Base.establish_connection(Rails.env)
19
- # ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
20
- SchemaComments::SchemaDumper.dump(ActiveRecord::Base.connection, file)
21
- end
22
- db_namespace['schema:dump'].reenable
23
- rescue Exception => e
24
- puts "[#{e.class}] #{e.message}:\n " << e.backtrace.join("\n ")
25
- raise e
26
- end
27
- end
28
-
29
- end
30
- end
31
-
32
- class ActiveRecord::Base
33
- class << self
34
- attr_accessor :ignore_pattern_to_export_i18n
35
- end
36
-
37
- self.ignore_pattern_to_export_i18n = /\(\(\(.*\)\)\)/
38
-
39
- class << self
40
- def export_i18n_models
41
- subclasses = ActiveRecord::Base.send(:subclasses).select do |klass|
42
- (klass != SchemaComments::SchemaComment) and
43
- klass.respond_to?(:table_exists?) and klass.table_exists?
44
- end
45
- result = subclasses.inject({}) do |d, m|
46
- comment = (m.table_comment || '').dup
47
- comment.gsub!(ignore_pattern_to_export_i18n, '') if ignore_pattern_to_export_i18n
48
- # テーブル名(複数形)をモデル名(単数形)に
49
- model_name = (comment.scan(/\[\[\[(?:model|class)(?:_name)?:\s*?([^\s]+?)\s*?\]\]\]/).flatten.first || m.name).underscore
50
- comment.gsub!(/\[\[\[.*?\]\]\]/)
51
- d[model_name] = comment
52
- d
53
- end
54
- result.instance_eval do
55
- def each_with_order(*args, &block)
56
- self.keys.sort.each do |key|
57
- yield(key, self[key])
58
- end
59
- end
60
- alias :each_without_order :each
61
- alias :each :each_with_order
62
- end
63
- result
64
- end
65
-
66
- def export_i18n_attributes
67
- subclasses = ActiveRecord::Base.send(:subclasses).select do |klass|
68
- (klass != SchemaComments::SchemaComment) and
69
- klass.respond_to?(:table_exists?) and klass.table_exists?
70
- end
71
- result = subclasses.inject({}) do |d, m|
72
- attrs = {}
73
- m.columns.each do |col|
74
- next if col.name == 'id'
75
- comment = (col.comment || '').dup
76
- comment.gsub!(ignore_pattern_to_export_i18n, '') if ignore_pattern_to_export_i18n
77
-
78
- # カラム名を属性名に
79
- attr_name = (comment.scan(/\[\[\[(?:attr|attribute)(?:_name)?:\s*?([^\s]+?)\s*?\]\]\]/).flatten.first || col.name)
80
- comment.gsub!(/\[\[\[.*?\]\]\]/)
81
- attrs[attr_name] = comment
82
- end
83
-
84
- column_names = m.columns.map(&:name) - ['id']
85
- column_order_modeule = Module.new do
86
- def each_with_column_order(*args, &block)
87
- @column_names.each do |column_name|
88
- yield(column_name, self[column_name])
89
- end
90
- end
91
-
92
- def self.extended(obj)
93
- obj.instance_eval do
94
- alias :each_without_column_order :each
95
- alias :each :each_with_column_order
96
- end
97
- end
98
- end
99
- attrs.instance_variable_set(:@column_names, column_names)
100
- attrs.extend(column_order_modeule)
101
-
102
- # テーブル名(複数形)をモデル名(単数形)に
103
- model_name = ((m.table_comment || '').scan(/\[\[\[(?:model|class)(?:_name)?:\s*?([^\s]+?)\s*?\]\]\]/).flatten.first || m.name).underscore
104
- d[model_name] = attrs
105
- d
106
- end
107
-
108
- result.instance_eval do
109
- def each_with_order(*args, &block)
110
- self.keys.sort.each do |key|
111
- yield(key, self[key])
112
- end
113
- end
114
- alias :each_without_order :each
115
- alias :each :each_with_order
116
- end
117
- result
1
+ namespace :schema_comments do
2
+ desc 'Dump schema to db/schema.rb'
3
+ task :dump => :environment do
4
+ require 'active_record/schema_dumper'
5
+ filename = ENV['SCHEMA'] || Rails.root.join('db/schema.rb').to_s
6
+ File.open(filename, "w:utf-8") do |file|
7
+ ActiveRecord::Base.establish_connection(Rails.env)
8
+ # ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
9
+ SchemaComments::SchemaDumper.dump(ActiveRecord::Base.connection, file)
118
10
  end
119
11
  end
120
- end
121
-
122
- namespace :i18n do
123
- namespace :schema_comments do
124
- task :load_all_models => :environment do
125
- Dir.glob(Rails.root.join('app/models/**/*.rb')) do |file_name|
126
- require file_name
127
- end
128
- end
129
12
 
130
- desc "Export i18n model resources from schema_comments. you can set locale with environment variable LOCALE"
131
- task :export_models => :"i18n:schema_comments:load_all_models" do
13
+ namespace :i18n do
14
+ desc "Show locale YAML"
15
+ task :show => :environment do
132
16
  locale = (ENV['LOCALE'] || I18n.locale).to_s
133
- obj = {locale => {'activerecord' => {'models' => ActiveRecord::Base.export_i18n_models}}}
134
- puts YAML.dump(obj)
135
- end
136
-
137
- desc "Export i18n attributes resources from schema_comments. you can set locale with environment variable LOCALE"
138
- task :export_attributes => :"i18n:schema_comments:load_all_models" do
139
- locale = (ENV['LOCALE'] || I18n.locale).to_s
140
- obj = {locale => {'activerecord' => {'attributes' => ActiveRecord::Base.export_i18n_attributes}}}
141
- puts YAML.dump(obj)
17
+ puts SchemaComments::SchemaComment.locale_yaml(locale)
142
18
  end
143
19
 
144
20
  desc "update i18n YAML. you can set locale with environment variable LOCALE"
145
- task :update_config_locale => :"i18n:schema_comments:load_all_models" do
146
- require 'yaml/store'
21
+ task :update => :environment do
147
22
  locale = (ENV['LOCALE'] || I18n.locale).to_s
148
23
  path = (ENV['YAML_PATH'] || Rails.root.join("config/locales/#{locale}.yml"))
149
- print "updating #{path}..."
150
-
151
- begin
152
- db = YAML::Store.new(path)
153
- db.transaction do
154
- locale = db[locale] ||= {}
155
- activerecord = locale['activerecord'] ||= {}
156
- activerecord['models'] = ActiveRecord::Base.export_i18n_models
157
- activerecord['attributes'] = ActiveRecord::Base.export_i18n_attributes
158
- end
159
- puts "Complete!"
160
- rescue Exception
161
- puts "Failure!!!"
162
- puts $!.to_s
163
- puts " " << $!.backtrace.join("\n ")
164
- raise
24
+ open(path, 'w') do |f|
25
+ f.puts SchemaComments::SchemaComment.locale_yaml(locale)
165
26
  end
166
27
  end
28
+
29
+ end
30
+ end
31
+
32
+ if Rails.env.development?
33
+ Rake::Task['db:schema:dump'].enhance do
34
+ Rake::Task['schema_comments:dump'].invoke
167
35
  end
168
36
  end
@@ -1,3 +1,3 @@
1
1
  module SchemaComments
2
- VERSION = "0.4.3"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :schema_comments do
3
+ # # Task goes here
4
+ # end
File without changes
@@ -1,28 +1,27 @@
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'
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "schema_comments/version"
5
3
 
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"]
4
+ Gem::Specification.new do |s|
5
+ s.name = "schema_comments"
6
+ s.version = SchemaComments::VERSION
7
+ s.authors = ["akm"]
8
+ s.email = ["akm2000@gmail.com"]
9
+ s.homepage = "http://github.com/akm/schema_comments"
10
+ s.summary = "schema_comments generates extra methods dynamically for attribute which has options."
11
+ s.description = "schema_comments generates extra methods dynamically for attribute which has options."
12
+ s.license = "MIT"
11
13
 
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"
14
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ # s.bindir = "exe"
16
+ # s.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
16
18
 
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"]
19
+ s.test_files = Dir["test/**/*"]
21
20
 
22
- spec.add_runtime_dependency('activesupport', ">= 4.0.0")
23
- spec.add_runtime_dependency('activerecord', ">= 4.0.0")
21
+ s.add_runtime_dependency('activesupport', ">= 4.0.0")
22
+ s.add_runtime_dependency('activerecord', ">= 4.0.0")
24
23
 
25
- spec.add_development_dependency "bundler", "~> 1.9"
26
- spec.add_development_dependency "rake", "~> 10.0"
27
- spec.add_development_dependency "rspec"
24
+ s.add_development_dependency "bundler"
25
+ s.add_development_dependency "rake"
26
+ s.add_development_dependency "rspec"
28
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_comments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - akm
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-15 00:00:00.000000000 Z
11
+ date: 2016-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -42,30 +42,30 @@ dependencies:
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1.9'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '1.9'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '10.0'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -92,34 +92,27 @@ files:
92
92
  - ".rspec"
93
93
  - ".travis.yml"
94
94
  - Gemfile
95
- - LICENSE.txt
95
+ - MIT-LICENSE
96
96
  - README.md
97
97
  - Rakefile
98
- - VERSION
99
- - bin/console
100
- - bin/setup
101
98
  - gemfiles/.gitignore
102
99
  - gemfiles/Gemfile.rails-4.0
103
100
  - gemfiles/Gemfile.rails-4.1
104
101
  - gemfiles/Gemfile.rails-4.2
105
- - init.rb
106
- - lib/annotate_models.rb
107
102
  - lib/hash_key_orderable.rb
108
103
  - lib/schema_comments.rb
109
- - lib/schema_comments/base.rb
110
104
  - lib/schema_comments/connection_adapters.rb
111
- - lib/schema_comments/migration.rb
112
- - lib/schema_comments/migrator.rb
105
+ - lib/schema_comments/dummy_migration.rb
113
106
  - lib/schema_comments/railtie.rb
114
107
  - lib/schema_comments/schema.rb
115
108
  - lib/schema_comments/schema_comment.rb
116
109
  - lib/schema_comments/schema_dumper.rb
117
110
  - lib/schema_comments/schema_dumper/mysql.rb
118
111
  - lib/schema_comments/task.rb
119
- - lib/schema_comments/tasks/alter_comments.rake
120
- - lib/schema_comments/tasks/annotate_models_tasks.rake
121
112
  - lib/schema_comments/tasks/schema_comments.rake
122
113
  - lib/schema_comments/version.rb
114
+ - lib/tasks/schema_comments_tasks.rake
115
+ - log/.keep
123
116
  - schema_comments.gemspec
124
117
  homepage: http://github.com/akm/schema_comments
125
118
  licenses:
@@ -1,60 +0,0 @@
1
- Ruby License
2
- http://www.ruby-lang.org/en/LICENSE.txt
3
-
4
- Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.co.jp>.
5
- You can redistribute it and/or modify it under either the terms of the GPL
6
- (see COPYING.txt file), or the conditions below:
7
-
8
- 1. You may make and give away verbatim copies of the source form of the
9
- software without restriction, provided that you duplicate all of the
10
- original copyright notices and associated disclaimers.
11
-
12
- 2. You may modify your copy of the software in any way, provided that
13
- you do at least ONE of the following:
14
-
15
- a) place your modifications in the Public Domain or otherwise
16
- make them Freely Available, such as by posting said
17
- modifications to Usenet or an equivalent medium, or by allowing
18
- the author to include your modifications in the software.
19
-
20
- b) use the modified software only within your corporation or
21
- organization.
22
-
23
- c) rename any non-standard executables so the names do not conflict
24
- with standard executables, which must also be provided.
25
-
26
- d) make other distribution arrangements with the author.
27
-
28
- 3. You may distribute the software in object code or executable
29
- form, provided that you do at least ONE of the following:
30
-
31
- a) distribute the executables and library files of the software,
32
- together with instructions (in the manual page or equivalent)
33
- on where to get the original distribution.
34
-
35
- b) accompany the distribution with the machine-readable source of
36
- the software.
37
-
38
- c) give non-standard executables non-standard names, with
39
- instructions on where to get the original software distribution.
40
-
41
- d) make other distribution arrangements with the author.
42
-
43
- 4. You may modify and include the part of the software into any other
44
- software (possibly commercial). But some files in the distribution
45
- are not written by the author, so that they are not under this terms.
46
-
47
- They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
48
- files under the ./missing directory. See each file for the copying
49
- condition.
50
-
51
- 5. The scripts and library files supplied as input to or produced as
52
- output from the software do not automatically fall under the
53
- copyright of the software, but belong to whomever generated them,
54
- and may be sold commercially, and may be aggregated with this
55
- software.
56
-
57
- 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
58
- IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
59
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
60
- PURPOSE.