Commit 54bdf7e2 by Cuong Tran

Merge pull request #142 from YayConnolly/master

add option to ignore columns matching a regex with -I or --ignore-columns
parents aaab884f d093e995
...@@ -134,6 +134,11 @@ OptionParser.new do |opts| ...@@ -134,6 +134,11 @@ OptionParser.new do |opts|
opts.on('--trace', 'If unable to annotate a file, print the full stack trace, not just the exception message.') do |value| opts.on('--trace', 'If unable to annotate a file, print the full stack trace, not just the exception message.') do |value|
ENV['trace'] = 'yes' ENV['trace'] = 'yes'
end end
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|
ENV['ignore_columns'] = regex
end
end.parse! end.parse!
......
...@@ -25,7 +25,7 @@ module Annotate ...@@ -25,7 +25,7 @@ module Annotate
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace, :format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
] ]
OTHER_OPTIONS=[ OTHER_OPTIONS=[
:model_dir, :model_dir, :ignore_columns
] ]
PATH_OPTIONS=[ PATH_OPTIONS=[
:require, :require,
......
...@@ -108,6 +108,9 @@ module AnnotateModels ...@@ -108,6 +108,9 @@ module AnnotateModels
end end
cols = klass.columns cols = klass.columns
if options[:ignore_columns]
cols.reject! { |col| col.name.match(/#{options[:ignore_columns]}/) }
end
cols = cols.sort_by(&:name) if(options[:sort]) cols = cols.sort_by(&:name) if(options[:sort])
cols.each do |col| cols.each do |col|
attrs = [] attrs = []
......
...@@ -396,6 +396,12 @@ end ...@@ -396,6 +396,12 @@ end
File.read(@model_file_name).should == "#{@file_content}\n#{another_schema_info}" File.read(@model_file_name).should == "#{@file_content}\n#{another_schema_info}"
end end
it 'should skip columns with option[:ignore_columns] set' do
output = AnnotateModels.get_schema_info(@klass, "== Schema Info",
:ignore_columns => '(id|updated_at|created_at)')
expect(output.match(/id/)).to be_nil
end
it "works with namespaced models (i.e. models inside modules/subdirectories)" do it "works with namespaced models (i.e. models inside modules/subdirectories)" do
(model_file_name, file_content) = write_model "foo/user.rb", <<-EOS (model_file_name, file_content) = write_model "foo/user.rb", <<-EOS
class Foo::User < ActiveRecord::Base class Foo::User < ActiveRecord::Base
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment