Skip to content

Commit 0958f39

Browse files
ctranmathieujobin
authored andcommitted
Ignore default value for json type since that can break indentation, ctran#320
1 parent a996f35 commit 0958f39

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/annotate/annotate_models.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ module AnnotateModels
5959
# Example: show "integer" instead of "integer(4)"
6060
NO_LIMIT_COL_TYPES = ["integer", "boolean"]
6161

62+
# Don't show default value for these column types
63+
NO_DEFAULT_COL_TYPES = ["json", "jsonb"]
64+
6265
class << self
6366
def model_dir
6467
@model_dir.is_a?(Array) ? @model_dir : [@model_dir || "app/models"]
@@ -198,7 +201,7 @@ def get_schema_info(klass, header, options = {})
198201
col_type = (col.type || col.sql_type).to_s
199202

200203
attrs = []
201-
attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || col_type == "jsonb"
204+
attrs << "default(#{schema_default(klass, col)})" unless col.default.nil? || NO_DEFAULT_COL_TYPES.include?(col_type)
202205
attrs << "not null" unless col.null
203206
attrs << "primary key" if klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect{|c|c.to_sym}.include?(col.name.to_sym) : col.name.to_sym == klass.primary_key.to_sym)
204207

spec/annotate/annotate_models_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ def mock_column(name, type, options={})
150150
EOS
151151
end
152152

153+
it "should ignore default value of json columns " do
154+
klass = mock_class(:users, nil, [
155+
mock_column(:id, :integer),
156+
mock_column(:profile, :json, :default => '{}'),
157+
mock_column(:settings, :jsonb, :default => '{}')
158+
])
159+
160+
expect(AnnotateModels.get_schema_info(klass, "Schema Info")).to eql(<<-EOS)
161+
# Schema Info
162+
#
163+
# Table name: users
164+
#
165+
# id :integer not null
166+
# profile :json not null
167+
# settings :jsonb not null
168+
#
169+
EOS
170+
end
171+
153172
it "should get foreign key info" do
154173
klass = mock_class(:users, :id, [
155174
mock_column(:id, :integer),

0 commit comments

Comments
 (0)