annotate 2.6.8 → 2.6.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ == 2.6.9
2
+ * Support foreigh key (#241)
3
+ * Check if model has skip tag in annotate_model_file (#167)
4
+ * Fix issue where serializer-related flags weren't being honored (#246)
5
+ * Prefer SQL column type over normalized AR type (#231)
6
+
1
7
  == 2.6.8
2
8
  * Nothing annotated unless options[:model_dir] is specified, #234
3
9
 
@@ -175,6 +175,7 @@ you can do so with a simple environment variable, instead of editing the
175
175
  -v, --version Show the current version of this gem
176
176
  -m, --show-migration Include the migration version number in the annotation
177
177
  -i, --show-indexes List the table's database indexes in the annotation
178
+ -k, --show-foreign-keys List the table's foreign key constraints in the annotation
178
179
  -s, --simple-indexes Concat the column's related indexes in the annotation
179
180
  --model-dir dir Annotate model files stored in dir rather than app/models, separate multiple dirs with comas
180
181
  --ignore-model-subdirects Ignore subdirectories of the models directory
@@ -33,7 +33,7 @@ OptionParser.new do |opts|
33
33
  end
34
34
 
35
35
  opts.on('-p', '--position [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
36
- "Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s)") do |p|
36
+ "Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)") do |p|
37
37
  ENV['position'] = p
38
38
  [
39
39
  'position_in_class','position_in_factory','position_in_fixture','position_in_test', 'position_in_routes', 'position_in_serializer'
@@ -109,6 +109,11 @@ OptionParser.new do |opts|
109
109
  ENV['include_version'] = "yes"
110
110
  end
111
111
 
112
+ opts.on('-k', '--show-foreign-keys',
113
+ "List the table's foreign key constraints in the annotation") do
114
+ ENV['show_foreign_keys'] = "yes"
115
+ end
116
+
112
117
  opts.on('-i', '--show-indexes',
113
118
  "List the table's database indexes in the annotation") do
114
119
  ENV['show_indexes'] = "yes"
@@ -20,13 +20,13 @@ module Annotate
20
20
  POSITION_OPTIONS=[
21
21
  :position_in_routes, :position_in_class, :position_in_test,
22
22
  :position_in_fixture, :position_in_factory, :position,
23
- :position_in_serializer,
23
+ :position_in_serializer
24
24
  ]
25
25
  FLAG_OPTIONS=[
26
26
  :show_indexes, :simple_indexes, :include_version, :exclude_tests,
27
27
  :exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
28
28
  :format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
29
- :timestamp, :exclude_serializers, :classified_sort
29
+ :timestamp, :exclude_serializers, :classified_sort, :show_foreign_keys,
30
30
  ]
31
31
  OTHER_OPTIONS=[
32
32
  :ignore_columns
@@ -7,7 +7,7 @@ module AnnotateModels
7
7
  PREFIX = "== Schema Information"
8
8
  PREFIX_MD = "## Schema Information"
9
9
  END_MARK = "== Schema Information End"
10
- PATTERN = /^\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\n(#.*\n)*\n*/
10
+ PATTERN = /^\r?\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\r?\n(#.*\r?\n)*(\r?\n)*/
11
11
 
12
12
  # File.join for windows reverse bar compat?
13
13
  # I dont use windows, can`t test
@@ -140,7 +140,7 @@ module AnnotateModels
140
140
  attrs << "not null" unless col.null
141
141
  attrs << "primary key" if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect{|c|c.to_sym}.include?(col.name.to_sym) : col.name.to_sym == klass.primary_key.to_sym)
142
142
 
143
- col_type = (col.type || col.sql_type).to_s
143
+ col_type = (col.sql_type || col.type).to_s
144
144
  if col_type == "decimal"
145
145
  col_type << "(#{col.precision}, #{col.scale})"
146
146
  elsif col_type != "spatial"
@@ -193,6 +193,10 @@ module AnnotateModels
193
193
  info << get_index_info(klass, options)
194
194
  end
195
195
 
196
+ if options[:show_foreign_keys] && klass.table_exists?
197
+ info << get_foreign_key_info(klass, options)
198
+ end
199
+
196
200
  if options[:format_rdoc]
197
201
  info << "#--\n"
198
202
  info << "# #{END_MARK}\n"
@@ -223,6 +227,28 @@ module AnnotateModels
223
227
  return index_info
224
228
  end
225
229
 
230
+ def get_foreign_key_info(klass, options={})
231
+ if(options[:format_markdown])
232
+ fk_info = "#\n# ### Foreign Keys\n#\n"
233
+ else
234
+ fk_info = "#\n# Foreign Keys\n#\n"
235
+ end
236
+
237
+ foreign_keys = klass.connection.respond_to?(:foreign_keys) ? klass.connection.foreign_keys(klass.table_name) : []
238
+ return "" if foreign_keys.empty?
239
+
240
+ max_size = foreign_keys.collect{|fk| fk.name.size}.max + 1
241
+ foreign_keys.sort_by{|fk| fk.name}.each do |fk|
242
+ ref_info = "#{fk.column} => #{fk.to_table}.#{fk.primary_key}"
243
+ if(options[:format_markdown])
244
+ fk_info << sprintf("# * `%s`:\n# * **`%s`**\n", fk.name, ref_info)
245
+ else
246
+ fk_info << sprintf("# %-#{max_size}.#{max_size}s %s", fk.name, "(#{ref_info})").rstrip + "\n"
247
+ end
248
+ end
249
+ return fk_info
250
+ end
251
+
226
252
  # Add a schema block to a file. If the file already contains
227
253
  # a schema info block (a comment starting with "== Schema Information"), check if it
228
254
  # matches the block that is already there. If so, leave it be. If not, remove the old
@@ -309,9 +335,11 @@ module AnnotateModels
309
335
  # :position_in_test<Symbol>:: where to place the annotated section in test/spec file(s)
310
336
  # :position_in_fixture<Symbol>:: where to place the annotated section in fixture file
311
337
  # :position_in_factory<Symbol>:: where to place the annotated section in factory file
338
+ # :position_in_serializer<Symbol>:: where to place the annotated section in serializer file
312
339
  # :exclude_tests<Symbol>:: whether to skip modification of test/spec files
313
340
  # :exclude_fixtures<Symbol>:: whether to skip modification of fixture files
314
341
  # :exclude_factories<Symbol>:: whether to skip modification of factory files
342
+ # :exclude_serializers<Symbol>:: whether to skip modification of serializer files
315
343
  #
316
344
  def annotate(klass, file, header, options={})
317
345
  begin
@@ -350,9 +378,9 @@ module AnnotateModels
350
378
  options.merge(:position=>(options[position_in] || options[:position]))
351
379
  end
352
380
 
353
- # Return a list of the model files to annotate.
381
+ # Return a list of the model files to annotate.
354
382
  # If we have command line arguments, they're assumed to the path
355
- # of model files from root dir. Otherwise we take all the model files
383
+ # of model files from root dir. Otherwise we take all the model files
356
384
  # in the model_dir directory.
357
385
  def get_model_files(options)
358
386
  models = []
@@ -364,7 +392,7 @@ module AnnotateModels
364
392
  begin
365
393
  model_dir.each do |dir|
366
394
  Dir.chdir(dir) do
367
- lst =
395
+ lst =
368
396
  if options[:ignore_model_sub_dir]
369
397
  Dir["*.rb"].map{ |f| [dir, f] }
370
398
  else
@@ -451,6 +479,7 @@ module AnnotateModels
451
479
 
452
480
  def annotate_model_file(annotated, file, header, options)
453
481
  begin
482
+ return false if (/# -\*- SkipSchemaAnnotations.*/ =~ (File.exist?(file) ? File.read(file) : '') )
454
483
  klass = get_model_class(file)
455
484
  if klass && klass < ActiveRecord::Base && !klass.abstract_class? && klass.table_exists?
456
485
  if annotate(klass, file, header, options)
@@ -477,7 +506,7 @@ module AnnotateModels
477
506
  model_file_name = file
478
507
  deannotated_klass = true if(remove_annotation_of_file(model_file_name))
479
508
 
480
- (TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS).
509
+ (TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS + SERIALIZER_PATTERNS).
481
510
  map { |file| resolve_filename(file, model_name, table_name) }.
482
511
  each do |file|
483
512
  if File.exist?(file)
@@ -1,5 +1,5 @@
1
1
  module Annotate
2
2
  def self.version
3
- '2.6.8'
3
+ '2.6.9'
4
4
  end
5
5
  end
@@ -6,27 +6,30 @@ if Rails.env.development?
6
6
  # You can override any of these by setting an environment variable of the
7
7
  # same name.
8
8
  Annotate.set_defaults({
9
- 'position_in_routes' => "before",
10
- 'position_in_class' => "before",
11
- 'position_in_test' => "before",
12
- 'position_in_fixture' => "before",
13
- 'position_in_factory' => "before",
14
- 'show_indexes' => "true",
15
- 'simple_indexes' => "false",
16
- 'model_dir' => "app/models",
17
- 'include_version' => "false",
18
- 'require' => "",
19
- 'exclude_tests' => "false",
20
- 'exclude_fixtures' => "false",
21
- 'exclude_factories' => "false",
22
- 'ignore_model_sub_dir' => "false",
23
- 'skip_on_db_migrate' => "false",
24
- 'format_bare' => "true",
25
- 'format_rdoc' => "false",
26
- 'format_markdown' => "false",
27
- 'sort' => "false",
28
- 'force' => "false",
29
- 'trace' => "false",
9
+ 'position_in_routes' => "before",
10
+ 'position_in_class' => "before",
11
+ 'position_in_test' => "before",
12
+ 'position_in_fixture' => "before",
13
+ 'position_in_factory' => "before",
14
+ 'position_in_serializer' => "before",
15
+ 'show_foreign_keys' => "true",
16
+ 'show_indexes' => "true",
17
+ 'simple_indexes' => "false",
18
+ 'model_dir' => "app/models",
19
+ 'include_version' => "false",
20
+ 'require' => "",
21
+ 'exclude_tests' => "false",
22
+ 'exclude_fixtures' => "false",
23
+ 'exclude_factories' => "false",
24
+ 'exclude_serializers' => "false",
25
+ 'ignore_model_sub_dir' => "false",
26
+ 'skip_on_db_migrate' => "false",
27
+ 'format_bare' => "true",
28
+ 'format_rdoc' => "false",
29
+ 'format_markdown' => "false",
30
+ 'sort' => "false",
31
+ 'force' => "false",
32
+ 'trace' => "false",
30
33
  })
31
34
  end
32
35
 
@@ -16,6 +16,7 @@ task :annotate_models => :environment do
16
16
  options[:position_in_fixture] = Annotate.fallback(ENV['position_in_fixture'], ENV['position'])
17
17
  options[:position_in_factory] = Annotate.fallback(ENV['position_in_factory'], ENV['position'])
18
18
  options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position'])
19
+ options[:position_in_serializer] = Annotate.fallback(ENV['position_in_serializer'], ENV['position'])
19
20
  options[:show_indexes] = Annotate.true?(ENV['show_indexes'])
20
21
  options[:simple_indexes] = Annotate.true?(ENV['simple_indexes'])
21
22
  options[:model_dir] = ENV['model_dir'] ? ENV['model_dir'].split(',') : []
@@ -24,6 +25,7 @@ task :annotate_models => :environment do
24
25
  options[:exclude_tests] = Annotate.true?(ENV['exclude_tests'])
25
26
  options[:exclude_factories] = Annotate.true?(ENV['exclude_factories'])
26
27
  options[:exclude_fixtures] = Annotate.true?(ENV['exclude_fixtures'])
28
+ options[:exclude_serializers] = Annotate.true?(ENV['exclude_serializers'])
27
29
  options[:ignore_model_sub_dir] = Annotate.true?(ENV['ignore_model_sub_dir'])
28
30
  options[:format_bare] = Annotate.true?(ENV['format_bare'])
29
31
  options[:format_rdoc] = Annotate.true?(ENV['format_rdoc'])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: annotate
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.8
4
+ version: 2.6.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2015-03-12 00:00:00.000000000 Z
16
+ date: 2015-05-22 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: rake