Skip to content

Commit bd910b0

Browse files
committed
Provide :hide_default_column_types option to make hiding of default configurable
1 parent 65af43e commit bd910b0

File tree

6 files changed

+146
-115
lines changed

6 files changed

+146
-115
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Metrics/MethodLength:
105105
# Offense count: 2
106106
# Configuration parameters: CountComments.
107107
Metrics/ModuleLength:
108-
Max: 522
108+
Max: 527
109109

110110
# Offense count: 7
111111
Metrics/PerceivedComplexity:

bin/annotate

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ OptionParser.new do |opts|
185185
ENV['hide_limit_column_types'] = "#{values}"
186186
end
187187

188+
opts.on('--hide_default-column-types VALUES', "don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)") do |values|
189+
ENV['hide_hide_default_column_types'] = "#{values}"
190+
end
191+
188192
opts.on('--ignore-unknown-models', "don't display warnings for bad model files") do |values|
189193
ENV['ignore_unknown_models'] = 'true'
190194
end

lib/annotate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module Annotate
3434
].freeze
3535
OTHER_OPTIONS = [
3636
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper, :routes,
37-
:hide_limit_column_types, :ignore_routes, :active_admin
37+
:hide_limit_column_types, :hide_default_column_types, :ignore_routes, :active_admin
3838
].freeze
3939
PATH_OPTIONS = [
4040
:require, :model_dir, :root_dir

lib/annotate/annotate_models.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def get_schema_info(klass, header, options = {})
220220
col_type = (col.type || col.sql_type).to_s
221221

222222
attrs = []
223-
attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || NO_DEFAULT_COL_TYPES.include?(col_type)
223+
attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || hide_default?(col_type, options)
224224
attrs << 'not null' unless col.null
225225
attrs << 'primary key' if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect(&:to_sym).include?(col.name.to_sym) : col.name.to_sym == klass.primary_key.to_sym)
226226

@@ -320,6 +320,13 @@ def hide_limit?(col_type, options)
320320
excludes.include?(col_type)
321321
end
322322

323+
def hide_default?(col_type, options)
324+
return false if options[:hide_default_column_types].blank?
325+
326+
excludes = options[:hide_default_column_types].split(',')
327+
excludes.include?(col_type)
328+
end
329+
323330
def get_foreign_key_info(klass, options={})
324331
if options[:format_markdown]
325332
fk_info = "#\n# ### Foreign Keys\n#\n"

lib/generators/annotate/templates/auto_annotate_models.rake

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,43 @@ if Rails.env.development?
66
# You can override any of these by setting an environment variable of the
77
# same name.
88
Annotate.set_defaults(
9-
'routes' => 'false',
10-
'position_in_routes' => 'before',
11-
'position_in_class' => 'before',
12-
'position_in_test' => 'before',
13-
'position_in_fixture' => 'before',
14-
'position_in_factory' => 'before',
15-
'position_in_serializer' => 'before',
16-
'show_foreign_keys' => 'true',
17-
'show_indexes' => 'true',
18-
'simple_indexes' => 'false',
19-
'model_dir' => 'app/models',
20-
'root_dir' => '',
21-
'include_version' => 'false',
22-
'require' => '',
23-
'exclude_tests' => 'false',
24-
'exclude_fixtures' => 'false',
25-
'exclude_factories' => 'false',
26-
'exclude_serializers' => 'false',
27-
'exclude_scaffolds' => 'true',
28-
'exclude_controllers' => 'true',
29-
'exclude_helpers' => 'true',
30-
'exclude_sti_subclasses' => 'false',
31-
'ignore_model_sub_dir' => 'false',
32-
'ignore_columns' => nil,
33-
'ignore_routes' => nil,
34-
'ignore_unknown_models' => 'false',
35-
'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(",") %>',
36-
'skip_on_db_migrate' => 'false',
37-
'format_bare' => 'true',
38-
'format_rdoc' => 'false',
39-
'format_markdown' => 'false',
40-
'sort' => 'false',
41-
'force' => 'false',
42-
'trace' => 'false',
43-
'wrapper_open' => nil,
44-
'wrapper_close' => nil
9+
'routes' => 'false',
10+
'position_in_routes' => 'before',
11+
'position_in_class' => 'before',
12+
'position_in_test' => 'before',
13+
'position_in_fixture' => 'before',
14+
'position_in_factory' => 'before',
15+
'position_in_serializer' => 'before',
16+
'show_foreign_keys' => 'true',
17+
'show_indexes' => 'true',
18+
'simple_indexes' => 'false',
19+
'model_dir' => 'app/models',
20+
'root_dir' => '',
21+
'include_version' => 'false',
22+
'require' => '',
23+
'exclude_tests' => 'false',
24+
'exclude_fixtures' => 'false',
25+
'exclude_factories' => 'false',
26+
'exclude_serializers' => 'false',
27+
'exclude_scaffolds' => 'true',
28+
'exclude_controllers' => 'true',
29+
'exclude_helpers' => 'true',
30+
'exclude_sti_subclasses' => 'false',
31+
'ignore_model_sub_dir' => 'false',
32+
'ignore_columns' => nil,
33+
'ignore_routes' => nil,
34+
'ignore_unknown_models' => 'false',
35+
'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(",") %>',
36+
'hide_default_column_types' => '<%= AnnotateModels::NO_DEFAULT_COL_TYPES.join(",") %>',
37+
'skip_on_db_migrate' => 'false',
38+
'format_bare' => 'true',
39+
'format_rdoc' => 'false',
40+
'format_markdown' => 'false',
41+
'sort' => 'false',
42+
'force' => 'false',
43+
'trace' => 'false',
44+
'wrapper_open' => nil,
45+
'wrapper_close' => nil
4546
)
4647
end
4748

spec/annotate/annotate_models_spec.rb

Lines changed: 95 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,6 @@ def mock_column(name, type, options={})
153153
EOS
154154
end
155155

156-
it "should ignore default value of json and hstore columns " do
157-
klass = mock_class(:users, nil, [
158-
mock_column(:id, :integer),
159-
mock_column(:profile, :json, :default => '{}'),
160-
mock_column(:settings, :jsonb, :default => '{}'),
161-
mock_column(:parameters, :hstore, :default => '{}'),
162-
])
163-
164-
expect(AnnotateModels.get_schema_info(klass, "Schema Info")).to eql(<<-EOS)
165-
# Schema Info
166-
#
167-
# Table name: users
168-
#
169-
# id :integer not null
170-
# profile :json not null
171-
# settings :jsonb not null
172-
# parameters :hstore not null
173-
#
174-
EOS
175-
end
176-
177156
it "should get foreign key info" do
178157
klass = mock_class(:users, :id, [
179158
mock_column(:id, :integer),
@@ -271,61 +250,101 @@ def self.when_called_with(options = {})
271250
end
272251
end
273252

274-
when_called_with hide_limit_column_types: '', returns: <<-EOS.strip_heredoc
275-
# Schema Info
276-
#
277-
# Table name: users
278-
#
279-
# id :integer not null, primary key
280-
# active :boolean not null
281-
# name :string(50) not null
282-
# notes :text(55) not null
283-
#
284-
EOS
285-
286-
when_called_with hide_limit_column_types: 'integer,boolean', returns:
287-
<<-EOS.strip_heredoc
288-
# Schema Info
289-
#
290-
# Table name: users
291-
#
292-
# id :integer not null, primary key
293-
# active :boolean not null
294-
# name :string(50) not null
295-
# notes :text(55) not null
296-
#
297-
EOS
298-
299-
when_called_with hide_limit_column_types: 'integer,boolean,string,text', returns:
300-
<<-EOS.strip_heredoc
301-
# Schema Info
302-
#
303-
# Table name: users
304-
#
305-
# id :integer not null, primary key
306-
# active :boolean not null
307-
# name :string not null
308-
# notes :text not null
309-
#
310-
EOS
311-
312-
mocked_columns_without_id = [
313-
[:active, :boolean, { :limit => 1 }],
314-
[:name, :string, { :limit => 50 }],
315-
[:notes, :text, { :limit => 55 }]
316-
]
317-
318-
when_called_with classified_sort: 'yes', with_columns: mocked_columns_without_id, returns:
319-
<<-EOS.strip_heredoc
320-
# Schema Info
321-
#
322-
# Table name: users
323-
#
324-
# active :boolean not null
325-
# name :string(50) not null
326-
# notes :text(55) not null
327-
#
328-
EOS
253+
describe 'hide_limit_column_types option' do
254+
when_called_with hide_limit_column_types: '', returns: <<-EOS.strip_heredoc
255+
# Schema Info
256+
#
257+
# Table name: users
258+
#
259+
# id :integer not null, primary key
260+
# active :boolean not null
261+
# name :string(50) not null
262+
# notes :text(55) not null
263+
#
264+
EOS
265+
266+
when_called_with hide_limit_column_types: 'integer,boolean', returns:
267+
<<-EOS.strip_heredoc
268+
# Schema Info
269+
#
270+
# Table name: users
271+
#
272+
# id :integer not null, primary key
273+
# active :boolean not null
274+
# name :string(50) not null
275+
# notes :text(55) not null
276+
#
277+
EOS
278+
279+
when_called_with hide_limit_column_types: 'integer,boolean,string,text', returns:
280+
<<-EOS.strip_heredoc
281+
# Schema Info
282+
#
283+
# Table name: users
284+
#
285+
# id :integer not null, primary key
286+
# active :boolean not null
287+
# name :string not null
288+
# notes :text not null
289+
#
290+
EOS
291+
end
292+
293+
describe 'hide_default_column_types option' do
294+
mocked_columns_without_id = [
295+
[:profile, :json, default: {}],
296+
[:settings, :jsonb, default: {}],
297+
[:parameters, :hstore, default: {}]
298+
]
299+
300+
when_called_with hide_default_column_types: '',
301+
with_columns: mocked_columns_without_id,
302+
returns:
303+
<<-EOS.strip_heredoc
304+
# Schema Info
305+
#
306+
# Table name: users
307+
#
308+
# profile :json default({}), not null
309+
# settings :jsonb default({}), not null
310+
# parameters :hstore default({}), not null
311+
#
312+
EOS
313+
314+
when_called_with hide_default_column_types: 'json',
315+
with_columns: mocked_columns_without_id,
316+
returns:
317+
<<-EOS.strip_heredoc
318+
# Schema Info
319+
#
320+
# Table name: users
321+
#
322+
# profile :json not null
323+
# settings :jsonb default({}), not null
324+
# parameters :hstore default({}), not null
325+
#
326+
EOS
327+
end
328+
329+
describe 'classified_sort option' do
330+
mocked_columns_without_id = [
331+
[:active, :boolean, { :limit => 1 }],
332+
[:name, :string, { :limit => 50 }],
333+
[:notes, :text, { :limit => 55 }]
334+
]
335+
336+
when_called_with classified_sort: 'yes', with_columns: mocked_columns_without_id, returns:
337+
<<-EOS.strip_heredoc
338+
# Schema Info
339+
#
340+
# Table name: users
341+
#
342+
# active :boolean not null
343+
# name :string(50) not null
344+
# notes :text(55) not null
345+
#
346+
EOS
347+
end
329348
end
330349

331350
describe "#get_model_class" do

0 commit comments

Comments
 (0)