Skip to content

Commit 54bdf7e

Browse files
committed
Merge pull request #142 from YayConnolly/master
add option to ignore columns matching a regex with -I or --ignore-columns
2 parents aaab884 + d093e99 commit 54bdf7e

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

bin/annotate

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ OptionParser.new do |opts|
134134
opts.on('--trace', 'If unable to annotate a file, print the full stack trace, not just the exception message.') do |value|
135135
ENV['trace'] = 'yes'
136136
end
137+
138+
opts.on('-I', '--ignore-columns REGEX', "don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`" ) do |regex|
139+
ENV['ignore_columns'] = regex
140+
end
141+
137142
end.parse!
138143

139144

lib/annotate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Annotate
2525
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
2626
]
2727
OTHER_OPTIONS=[
28-
:model_dir,
28+
:model_dir, :ignore_columns
2929
]
3030
PATH_OPTIONS=[
3131
:require,

lib/annotate/annotate_models.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def get_schema_info(klass, header, options = {})
108108
end
109109

110110
cols = klass.columns
111+
if options[:ignore_columns]
112+
cols.reject! { |col| col.name.match(/#{options[:ignore_columns]}/) }
113+
end
111114
cols = cols.sort_by(&:name) if(options[:sort])
112115
cols.each do |col|
113116
attrs = []

spec/annotate/annotate_models_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,12 @@ def encoding_comments_list_each
396396
File.read(@model_file_name).should == "#{@file_content}\n#{another_schema_info}"
397397
end
398398

399+
it 'should skip columns with option[:ignore_columns] set' do
400+
output = AnnotateModels.get_schema_info(@klass, "== Schema Info",
401+
:ignore_columns => '(id|updated_at|created_at)')
402+
expect(output.match(/id/)).to be_nil
403+
end
404+
399405
it "works with namespaced models (i.e. models inside modules/subdirectories)" do
400406
(model_file_name, file_content) = write_model "foo/user.rb", <<-EOS
401407
class Foo::User < ActiveRecord::Base

0 commit comments

Comments
 (0)