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"
......
...@@ -6,42 +6,43 @@ if Rails.env.development? ...@@ -6,42 +6,43 @@ if Rails.env.development?
# You can override any of these by setting an environment variable of the # You can override any of these by setting an environment variable of the
# same name. # same name.
Annotate.set_defaults( Annotate.set_defaults(
'routes' => 'false', 'routes' => 'false',
'position_in_routes' => 'before', 'position_in_routes' => 'before',
'position_in_class' => 'before', 'position_in_class' => 'before',
'position_in_test' => 'before', 'position_in_test' => 'before',
'position_in_fixture' => 'before', 'position_in_fixture' => 'before',
'position_in_factory' => 'before', 'position_in_factory' => 'before',
'position_in_serializer' => 'before', 'position_in_serializer' => 'before',
'show_foreign_keys' => 'true', 'show_foreign_keys' => 'true',
'show_indexes' => 'true', 'show_indexes' => 'true',
'simple_indexes' => 'false', 'simple_indexes' => 'false',
'model_dir' => 'app/models', 'model_dir' => 'app/models',
'root_dir' => '', 'root_dir' => '',
'include_version' => 'false', 'include_version' => 'false',
'require' => '', 'require' => '',
'exclude_tests' => 'false', 'exclude_tests' => 'false',
'exclude_fixtures' => 'false', 'exclude_fixtures' => 'false',
'exclude_factories' => 'false', 'exclude_factories' => 'false',
'exclude_serializers' => 'false', 'exclude_serializers' => 'false',
'exclude_scaffolds' => 'true', 'exclude_scaffolds' => 'true',
'exclude_controllers' => 'true', 'exclude_controllers' => 'true',
'exclude_helpers' => 'true', 'exclude_helpers' => 'true',
'exclude_sti_subclasses' => 'false', 'exclude_sti_subclasses' => 'false',
'ignore_model_sub_dir' => 'false', 'ignore_model_sub_dir' => 'false',
'ignore_columns' => nil, 'ignore_columns' => nil,
'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(",") %>',
'skip_on_db_migrate' => 'false', 'hide_default_column_types' => '<%= AnnotateModels::NO_DEFAULT_COL_TYPES.join(",") %>',
'format_bare' => 'true', 'skip_on_db_migrate' => 'false',
'format_rdoc' => 'false', 'format_bare' => 'true',
'format_markdown' => 'false', 'format_rdoc' => 'false',
'sort' => 'false', 'format_markdown' => 'false',
'force' => 'false', 'sort' => 'false',
'trace' => 'false', 'force' => 'false',
'wrapper_open' => nil, 'trace' => 'false',
'wrapper_close' => nil 'wrapper_open' => nil,
'wrapper_close' => nil
) )
end end
......
...@@ -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,61 +250,115 @@ EOS ...@@ -271,61 +250,115 @@ EOS
end end
end end
when_called_with hide_limit_column_types: '', returns: <<-EOS.strip_heredoc describe 'hide_limit_column_types option' do
# Schema Info when_called_with hide_limit_column_types: '', returns: <<-EOS.strip_heredoc
# # Schema Info
# Table name: users #
# # Table name: users
# id :integer not null, primary key #
# active :boolean not null # id :integer not null, primary key
# name :string(50) not null # active :boolean not null
# notes :text(55) not null # name :string(50) not null
# # notes :text(55) not null
EOS #
EOS
when_called_with hide_limit_column_types: 'integer,boolean', returns:
<<-EOS.strip_heredoc when_called_with hide_limit_column_types: 'integer,boolean', returns:
# Schema Info <<-EOS.strip_heredoc
# # Schema Info
# Table name: users #
# # Table name: users
# id :integer not null, primary key #
# active :boolean not null # id :integer not null, primary key
# name :string(50) not null # active :boolean not null
# notes :text(55) not null # name :string(50) not null
# # notes :text(55) not null
EOS #
EOS
when_called_with hide_limit_column_types: 'integer,boolean,string,text', returns:
<<-EOS.strip_heredoc when_called_with hide_limit_column_types: 'integer,boolean,string,text', returns:
# Schema Info <<-EOS.strip_heredoc
# # Schema Info
# Table name: users #
# # Table name: users
# id :integer not null, primary key #
# active :boolean not null # id :integer not null, primary key
# name :string not null # active :boolean not null
# notes :text not null # name :string not null
# # notes :text not null
EOS #
EOS
mocked_columns_without_id = [ end
[:active, :boolean, { :limit => 1 }],
[:name, :string, { :limit => 50 }], describe 'hide_default_column_types option' do
[:notes, :text, { :limit => 55 }] mocked_columns_without_id = [
] [:profile, :json, default: {}],
[:settings, :jsonb, default: {}],
when_called_with classified_sort: 'yes', with_columns: mocked_columns_without_id, returns: [:parameters, :hstore, default: {}]
<<-EOS.strip_heredoc ]
# Schema Info
# when_called_with hide_default_column_types: '',
# Table name: users with_columns: mocked_columns_without_id,
# returns:
# active :boolean not null <<-EOS.strip_heredoc
# name :string(50) not null # Schema Info
# notes :text(55) not null #
# # Table name: users
EOS #
# 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 = [
[:active, :boolean, { :limit => 1 }],
[:name, :string, { :limit => 50 }],
[:notes, :text, { :limit => 55 }]
]
when_called_with classified_sort: 'yes', with_columns: mocked_columns_without_id, returns:
<<-EOS.strip_heredoc
# Schema Info
#
# Table name: users
#
# active :boolean not null
# name :string(50) not null
# notes :text(55) not null
#
EOS
end
end end
describe "#get_model_class" do describe "#get_model_class" do
......
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