Skip to content

Commit 56e6930

Browse files
committed
Merge remote-tracking branch 'appsinyourpants/restart' into development
Conflicts: lib/annotate/annotate_models.rb
2 parents bb88aa6 + 2293d1e commit 56e6930

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

History.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
* Accept String or Symbol for :position (et al) options.
2929
* Rename "annotate" bin to "annotate_models" to avoid conflicting with
3030
ImageMagick.
31-
* Add rdoc output formatting as an option.
31+
* Add RDoc output formatting as an option.
32+
* Add Markdown output formatting as an option.
33+
* Add option to force annotation regeneration.
3234
* Add new configuration option for controlling where info is placed in
3335
fixtures/factories.
3436
* Fix for models without tables.

bin/annotate_models

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ OptionParser.new do |opts|
7373
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" }
7474
end
7575

76-
opts.on('-f', '--format [bare|rdoc]', ['bare', 'rdoc'], 'rdoc: render Schema Infomation as RDoc') do |fmt|
77-
ENV['format_#{fmt}'] = 'yes'
76+
opts.on('-f', '--format [bare|rdoc|markdown]', ['bare', 'rdoc', 'markdown'], 'rdoc: render Schema Infomation as RDoc') do |fmt|
77+
ENV["format_#{fmt}"] = 'yes'
78+
end
79+
80+
opts.on('--force', 'Force new annotations even if there are no changes.') do |force|
81+
ENV['force'] = 'yes'
7882
end
7983

8084
end.parse!

lib/annotate/annotate_models.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
module AnnotateModels
22
# Annotate Models plugin use this header
3-
COMPAT_PREFIX = "== Schema Info"
4-
PREFIX = "== Schema Information"
5-
END_MARK = "== Schema Information End"
6-
PATTERN = /\n?# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n*/
3+
COMPAT_PREFIX = "== Schema Info"
4+
COMPAT_PREFIX_MD = "## Schema Info"
5+
PREFIX = "== Schema Information"
6+
PREFIX_MD = "## Schema Information"
7+
END_MARK = "== Schema Information End"
8+
PATTERN = /^\n?# (?:#{COMPAT_PREFIX}|#{COMPAT_PREFIX_MD}).*?\n(#.*\n)*\n/
9+
710
# File.join for windows reverse bar compat?
811
# I dont use windows, can`t test
912
UNIT_TEST_DIR = File.join("test", "unit" )
@@ -62,6 +65,12 @@ def get_schema_info(klass, header, options = {})
6265

6366
max_size = klass.column_names.map{|name| name.size}.max || 0
6467
max_size += options[:format_rdoc] ? 5 : 1
68+
69+
if(options[:format_markdown])
70+
info<< sprintf( "# %-#{max_size + 4}.#{max_size + 4}s | %-17.17s | %s \n", 'Field', 'Type', 'Attributes' )
71+
info<< "# #{ '-' * ( max_size + 4 ) } | #{'-' * 17} | #{ '-' * 25 } \n"
72+
end
73+
6574
cols = klass.columns
6675
cols = cols.sort_by(&:name) unless(options[:no_sort])
6776
cols.each do |col|
@@ -99,6 +108,8 @@ def get_schema_info(klass, header, options = {})
99108

100109
if options[:format_rdoc]
101110
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col.name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
111+
elsif options[:format_markdown]
112+
info << sprintf("# **%-#{max_size}.#{max_size}s** | `%-16.16s` | `%s `", col.name, col_type, attrs.join(", ")).rstrip + "\n"
102113
else
103114
info << sprintf("# %-#{max_size}.#{max_size}s:%-16.16s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
104115
end
@@ -162,7 +173,7 @@ def annotate_one_file(file_name, info_block, options={})
162173
encoding = Regexp.new(/(^#\s*encoding:.*\n)|(^# coding:.*\n)|(^# -\*- coding:.*\n)/)
163174
encoding_header = old_content.match(encoding).to_s
164175

165-
if old_columns == new_columns
176+
if old_columns == new_columns && !options[:force]
166177
false
167178
else
168179
# Strip the old schema info, and insert new schema info.
@@ -304,7 +315,7 @@ def do_annotations(options={})
304315
end
305316
end
306317

307-
header = PREFIX.dup
318+
header = options[:format_markdown] ? PREFIX_MD.dup : PREFIX.dup
308319

309320
if options[:include_version]
310321
version = ActiveRecord::Migrator.current_version rescue 0

lib/generators/annotate_models/templates/auto_annotate_models.rake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ if(Rails.env.development?)
1414
ENV['ignore_model_sub_dir'] = "false"
1515
ENV['skip_on_db_migrate'] = "false"
1616
ENV['format_rdoc'] = "false"
17+
ENV['format_markdown'] = "false"
1718
ENV['no_sort'] = "false"
19+
ENV['force'] = "false"
1820
end

lib/tasks/annotate_models.rake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ task :annotate_models => :environment do
1818
options[:exclude_fixtures] = ENV['exclude_fixtures'] =~ true_re
1919
options[:ignore_model_sub_dir] = ENV['ignore_model_sub_dir'] =~ true_re
2020
options[:format_rdoc] = ENV['format_rdoc'] =~ true_re
21+
options[:format_markdown] = ENV['format_markdown'] =~ true_re
2122
options[:no_sort] = ENV['no_sort'] =~ true_re
23+
options[:force] = ENV['force'] =~ true_re
2224
AnnotateModels.do_annotations(options)
2325
end
2426

0 commit comments

Comments
 (0)