Commit 89d513ca by Andrey Krivko Committed by Cuong Tran

Provide :hide_default_column_types option to make hiding of default configurable (#389)

* Provide :hide_default_column_types option to make hiding of default configurable * Use `NO_DEFAULT_COL_TYPES` as default when the `hide_default_column_types` option is not provided
parent 65af43e6
...@@ -105,7 +105,7 @@ Metrics/MethodLength: ...@@ -105,7 +105,7 @@ Metrics/MethodLength:
# Offense count: 2 # Offense count: 2
# Configuration parameters: CountComments. # Configuration parameters: CountComments.
Metrics/ModuleLength: Metrics/ModuleLength:
Max: 522 Max: 531
# Offense count: 7 # Offense count: 7
Metrics/PerceivedComplexity: Metrics/PerceivedComplexity:
......
...@@ -185,6 +185,10 @@ OptionParser.new do |opts| ...@@ -185,6 +185,10 @@ OptionParser.new do |opts|
ENV['hide_limit_column_types'] = "#{values}" ENV['hide_limit_column_types'] = "#{values}"
end end
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|
ENV['hide_default_column_types'] = "#{values}"
end
opts.on('--ignore-unknown-models', "don't display warnings for bad model files") do |values| opts.on('--ignore-unknown-models', "don't display warnings for bad model files") do |values|
ENV['ignore_unknown_models'] = 'true' ENV['ignore_unknown_models'] = 'true'
end end
......
...@@ -34,7 +34,7 @@ module Annotate ...@@ -34,7 +34,7 @@ module Annotate
].freeze ].freeze
OTHER_OPTIONS = [ 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, :ignore_routes, :active_admin :hide_limit_column_types, :hide_default_column_types, :ignore_routes, :active_admin
].freeze ].freeze
PATH_OPTIONS = [ PATH_OPTIONS = [
:require, :model_dir, :root_dir :require, :model_dir, :root_dir
......
...@@ -220,7 +220,7 @@ module AnnotateModels ...@@ -220,7 +220,7 @@ module AnnotateModels
col_type = (col.type || col.sql_type).to_s col_type = (col.type || col.sql_type).to_s
attrs = [] attrs = []
attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || NO_DEFAULT_COL_TYPES.include?(col_type) attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || hide_default?(col_type, options)
attrs << 'not null' unless col.null attrs << 'not null' unless col.null
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) 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)
...@@ -320,6 +320,17 @@ module AnnotateModels ...@@ -320,6 +320,17 @@ module AnnotateModels
excludes.include?(col_type) excludes.include?(col_type)
end end
def hide_default?(col_type, options)
excludes =
if options[:hide_default_column_types].blank?
NO_DEFAULT_COL_TYPES
else
options[:hide_default_column_types].split(',')
end
excludes.include?(col_type)
end
def get_foreign_key_info(klass, options={}) def get_foreign_key_info(klass, options={})
if options[:format_markdown] if options[:format_markdown]
fk_info = "#\n# ### Foreign Keys\n#\n" fk_info = "#\n# ### Foreign Keys\n#\n"
......
...@@ -33,6 +33,7 @@ if Rails.env.development? ...@@ -33,6 +33,7 @@ if Rails.env.development?
'ignore_routes' => nil, 'ignore_routes' => nil,
'ignore_unknown_models' => 'false', 'ignore_unknown_models' => 'false',
'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(",") %>', 'hide_limit_column_types' => '<%= AnnotateModels::NO_LIMIT_COL_TYPES.join(",") %>',
'hide_default_column_types' => '<%= AnnotateModels::NO_DEFAULT_COL_TYPES.join(",") %>',
'skip_on_db_migrate' => 'false', 'skip_on_db_migrate' => 'false',
'format_bare' => 'true', 'format_bare' => 'true',
'format_rdoc' => 'false', 'format_rdoc' => 'false',
......
...@@ -153,27 +153,6 @@ EOS ...@@ -153,27 +153,6 @@ EOS
EOS EOS
end end
it "should ignore default value of json and hstore columns " do
klass = mock_class(:users, nil, [
mock_column(:id, :integer),
mock_column(:profile, :json, :default => '{}'),
mock_column(:settings, :jsonb, :default => '{}'),
mock_column(:parameters, :hstore, :default => '{}'),
])
expect(AnnotateModels.get_schema_info(klass, "Schema Info")).to eql(<<-EOS)
# Schema Info
#
# Table name: users
#
# id :integer not null
# profile :json not null
# settings :jsonb not null
# parameters :hstore not null
#
EOS
end
it "should get foreign key info" do it "should get foreign key info" do
klass = mock_class(:users, :id, [ klass = mock_class(:users, :id, [
mock_column(:id, :integer), mock_column(:id, :integer),
...@@ -271,6 +250,7 @@ EOS ...@@ -271,6 +250,7 @@ EOS
end end
end end
describe 'hide_limit_column_types option' do
when_called_with hide_limit_column_types: '', returns: <<-EOS.strip_heredoc when_called_with hide_limit_column_types: '', returns: <<-EOS.strip_heredoc
# Schema Info # Schema Info
# #
...@@ -308,7 +288,59 @@ EOS ...@@ -308,7 +288,59 @@ EOS
# notes :text not null # notes :text not null
# #
EOS EOS
end
describe 'hide_default_column_types option' do
mocked_columns_without_id = [
[:profile, :json, default: {}],
[:settings, :jsonb, default: {}],
[:parameters, :hstore, default: {}]
]
when_called_with hide_default_column_types: '',
with_columns: mocked_columns_without_id,
returns:
<<-EOS.strip_heredoc
# Schema Info
#
# Table name: users
#
# profile :json not null
# settings :jsonb not null
# parameters :hstore not null
#
EOS
when_called_with hide_default_column_types: 'skip',
with_columns: mocked_columns_without_id,
returns:
<<-EOS.strip_heredoc
# Schema Info
#
# Table name: users
#
# profile :json default({}), not null
# settings :jsonb default({}), not null
# parameters :hstore default({}), not null
#
EOS
when_called_with hide_default_column_types: 'json',
with_columns: mocked_columns_without_id,
returns:
<<-EOS.strip_heredoc
# Schema Info
#
# Table name: users
#
# profile :json not null
# settings :jsonb default({}), not null
# parameters :hstore default({}), not null
#
EOS
end
describe 'classified_sort option' do
mocked_columns_without_id = [ mocked_columns_without_id = [
[:active, :boolean, { :limit => 1 }], [:active, :boolean, { :limit => 1 }],
[:name, :string, { :limit => 50 }], [:name, :string, { :limit => 50 }],
...@@ -327,6 +359,7 @@ EOS ...@@ -327,6 +359,7 @@ EOS
# #
EOS EOS
end end
end
describe "#get_model_class" do describe "#get_model_class" do
require "tmpdir" require "tmpdir"
......
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