Skip to content

Commit

Permalink
Provide :hide_limit_column_types option to skip column size, #278
Browse files Browse the repository at this point in the history
  • Loading branch information
ctran committed Dec 30, 2015
1 parent 513ce35 commit 6523dd7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
4 changes: 4 additions & 0 deletions bin/annotate
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ OptionParser.new do |opts|
ENV['ignore_columns'] = regex
end

opts.on('--hide-limit-column-types VALUES', "don't show limit for given column types, separated by comas (i.e., `integer,boolean,text`)" ) do |values|
ENV['hide_limit_column_types'] = "#{values}"
end

end.parse!

options = Annotate.setup_options({ :is_rake => ENV['is_rake'] && !ENV['is_rake'].empty? })
Expand Down
3 changes: 2 additions & 1 deletion lib/annotate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ module Annotate
:exclude_scaffolds, :exclude_controllers, :exclude_helpers
]
OTHER_OPTIONS=[
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper, :routes
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper, :routes,
:hide_limit_column_types,
]
PATH_OPTIONS=[
:require, :model_dir, :root_dir
Expand Down
13 changes: 12 additions & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def get_schema_info(klass, header, options = {})
if col.limit.is_a? Array
attrs << "(#{col.limit.join(', ')})"
else
col_type << "(#{col.limit})" unless NO_LIMIT_COL_TYPES.include?(col_type)
col_type << "(#{col.limit})" unless hide_limit?(col_type, options)
end
end
end
Expand Down Expand Up @@ -281,6 +281,17 @@ def get_index_info(klass, options={})
return index_info
end

def hide_limit?(col_type, options)
excludes =
if options[:hide_limit_column_types].blank?
NO_LIMIT_COL_TYPES
else
options[:hide_limit_column_types].split(',')
end

excludes.include?(col_type)
end

def get_foreign_key_info(klass, options={})
if(options[:format_markdown])
fk_info = "#\n# ### Foreign Keys\n#\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ if Rails.env.development?
'exclude_helpers' => 'false',
'ignore_model_sub_dir' => 'false',
'ignore_columns' => nil,
'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(',') %>',
'skip_on_db_migrate' => 'false',
'format_bare' => 'true',
'format_rdoc' => 'false',
Expand Down
68 changes: 63 additions & 5 deletions spec/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require File.dirname(__FILE__) + '/../spec_helper.rb'
require 'annotate/annotate_models'
require 'annotate/active_record_patch'
require 'active_support/core_ext/string'

describe AnnotateModels do
def mock_foreign_key(name, from_column, to_table, to_column = 'id')
Expand Down Expand Up @@ -60,19 +61,21 @@ def mock_column(name, type, options={})
it { expect(AnnotateModels.quote(BigDecimal.new("1.2"))).to eql("1.2") }
it { expect(AnnotateModels.quote([BigDecimal.new("1.2")])).to eql(["1.2"]) }

it "should get schema info" do
it "should get schema info with default options" do
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
mock_column(:name, :string, :limit => 50)
mock_column(:id, :integer, :limit => 8),
mock_column(:name, :string, :limit => 50),
mock_column(:notes, :text, :limit => 55),
])

expect(AnnotateModels.get_schema_info(klass, "Schema Info")).to eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
# id :integer not null, primary key
# name :string(50) not null
# notes :text(55) not null
#
EOS
end
Expand Down Expand Up @@ -192,6 +195,61 @@ def mock_column(name, type, options={})
EOS
end

describe "#get_schema_info with custom options" do
def self.when_called_with(options = {})
expected = options.delete(:returns)

it "should work with options = #{options}" do
klass = mock_class(:users, :id, [
mock_column(:id, :integer, :limit => 8),
mock_column(:active, :boolean, :limit => 1),
mock_column(:name, :string, :limit => 50),
mock_column(:notes, :text, :limit => 55),
])
schema_info = AnnotateModels.get_schema_info(klass, "Schema Info", options)
expect(schema_info).to eql(expected)
end
end

when_called_with hide_limit_column_types: '', returns: <<-EOS.strip_heredoc
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# active :boolean not null
# name :string(50) not null
# notes :text(55) not null
#
EOS

when_called_with hide_limit_column_types: 'integer,boolean', returns:
<<-EOS.strip_heredoc
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# active :boolean not null
# name :string(50) not null
# notes :text(55) not null
#
EOS

when_called_with hide_limit_column_types: 'integer,boolean,string,text', returns:
<<-EOS.strip_heredoc
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# active :boolean not null
# name :string not null
# notes :text not null
#
EOS
end

describe "#get_model_class" do
require "tmpdir"

Expand Down

0 comments on commit 6523dd7

Please sign in to comment.