Skip to content

Commit 6523dd7

Browse files
committed
Provide :hide_limit_column_types option to skip column size, #278
1 parent 513ce35 commit 6523dd7

File tree

5 files changed

+82
-7
lines changed

5 files changed

+82
-7
lines changed

bin/annotate

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ OptionParser.new do |opts|
178178
ENV['ignore_columns'] = regex
179179
end
180180

181+
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|
182+
ENV['hide_limit_column_types'] = "#{values}"
183+
end
184+
181185
end.parse!
182186

183187
options = Annotate.setup_options({ :is_rake => ENV['is_rake'] && !ENV['is_rake'].empty? })

lib/annotate.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ module Annotate
3030
:exclude_scaffolds, :exclude_controllers, :exclude_helpers
3131
]
3232
OTHER_OPTIONS=[
33-
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper, :routes
33+
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper, :routes,
34+
:hide_limit_column_types,
3435
]
3536
PATH_OPTIONS=[
3637
:require, :model_dir, :root_dir

lib/annotate/annotate_models.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def get_schema_info(klass, header, options = {})
202202
if col.limit.is_a? Array
203203
attrs << "(#{col.limit.join(', ')})"
204204
else
205-
col_type << "(#{col.limit})" unless NO_LIMIT_COL_TYPES.include?(col_type)
205+
col_type << "(#{col.limit})" unless hide_limit?(col_type, options)
206206
end
207207
end
208208
end
@@ -281,6 +281,17 @@ def get_index_info(klass, options={})
281281
return index_info
282282
end
283283

284+
def hide_limit?(col_type, options)
285+
excludes =
286+
if options[:hide_limit_column_types].blank?
287+
NO_LIMIT_COL_TYPES
288+
else
289+
options[:hide_limit_column_types].split(',')
290+
end
291+
292+
excludes.include?(col_type)
293+
end
294+
284295
def get_foreign_key_info(klass, options={})
285296
if(options[:format_markdown])
286297
fk_info = "#\n# ### Foreign Keys\n#\n"

lib/generators/annotate/templates/auto_annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ if Rails.env.development?
2929
'exclude_helpers' => 'false',
3030
'ignore_model_sub_dir' => 'false',
3131
'ignore_columns' => nil,
32+
'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(',') %>',
3233
'skip_on_db_migrate' => 'false',
3334
'format_bare' => 'true',
3435
'format_rdoc' => 'false',

spec/annotate/annotate_models_spec.rb

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require File.dirname(__FILE__) + '/../spec_helper.rb'
33
require 'annotate/annotate_models'
44
require 'annotate/active_record_patch'
5+
require 'active_support/core_ext/string'
56

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

63-
it "should get schema info" do
64+
it "should get schema info with default options" do
6465
klass = mock_class(:users, :id, [
65-
mock_column(:id, :integer),
66-
mock_column(:name, :string, :limit => 50)
66+
mock_column(:id, :integer, :limit => 8),
67+
mock_column(:name, :string, :limit => 50),
68+
mock_column(:notes, :text, :limit => 55),
6769
])
6870

6971
expect(AnnotateModels.get_schema_info(klass, "Schema Info")).to eql(<<-EOS)
7072
# Schema Info
7173
#
7274
# Table name: users
7375
#
74-
# id :integer not null, primary key
75-
# name :string(50) not null
76+
# id :integer not null, primary key
77+
# name :string(50) not null
78+
# notes :text(55) not null
7679
#
7780
EOS
7881
end
@@ -192,6 +195,61 @@ def mock_column(name, type, options={})
192195
EOS
193196
end
194197

198+
describe "#get_schema_info with custom options" do
199+
def self.when_called_with(options = {})
200+
expected = options.delete(:returns)
201+
202+
it "should work with options = #{options}" do
203+
klass = mock_class(:users, :id, [
204+
mock_column(:id, :integer, :limit => 8),
205+
mock_column(:active, :boolean, :limit => 1),
206+
mock_column(:name, :string, :limit => 50),
207+
mock_column(:notes, :text, :limit => 55),
208+
])
209+
schema_info = AnnotateModels.get_schema_info(klass, "Schema Info", options)
210+
expect(schema_info).to eql(expected)
211+
end
212+
end
213+
214+
when_called_with hide_limit_column_types: '', returns: <<-EOS.strip_heredoc
215+
# Schema Info
216+
#
217+
# Table name: users
218+
#
219+
# id :integer not null, primary key
220+
# active :boolean not null
221+
# name :string(50) not null
222+
# notes :text(55) not null
223+
#
224+
EOS
225+
226+
when_called_with hide_limit_column_types: 'integer,boolean', returns:
227+
<<-EOS.strip_heredoc
228+
# Schema Info
229+
#
230+
# Table name: users
231+
#
232+
# id :integer not null, primary key
233+
# active :boolean not null
234+
# name :string(50) not null
235+
# notes :text(55) not null
236+
#
237+
EOS
238+
239+
when_called_with hide_limit_column_types: 'integer,boolean,string,text', returns:
240+
<<-EOS.strip_heredoc
241+
# Schema Info
242+
#
243+
# Table name: users
244+
#
245+
# id :integer not null, primary key
246+
# active :boolean not null
247+
# name :string not null
248+
# notes :text not null
249+
#
250+
EOS
251+
end
252+
195253
describe "#get_model_class" do
196254
require "tmpdir"
197255

0 commit comments

Comments
 (0)