From abca431765000911aec0316c242a7f661892eefc Mon Sep 17 00:00:00 2001 From: Adam Hallett Date: Sat, 17 Dec 2022 12:44:47 -0500 Subject: [PATCH 1/4] add support for default procs (combines default + extra options) --- lib/schema_plus/core/schema_dump.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/schema_plus/core/schema_dump.rb b/lib/schema_plus/core/schema_dump.rb index e4c0db2..dda7a8c 100644 --- a/lib/schema_plus/core/schema_dump.rb +++ b/lib/schema_plus/core/schema_dump.rb @@ -105,6 +105,11 @@ def assemble(stream, typelen, namelen) pr += ',' unless options.blank? stream.write "%-#{namelen+3}s " % pr end + if options[:default].is_a?(Proc) + default = options.delete(:default) + stream.write "default: -> { \"#{default.call}\" }, " + stream.write options.to_s.sub(/^{(.*)}$/, '\1') unless options.blank? + end stream.write options.to_s.sub(/^{(.*)}$/, '\1') unless options.blank? stream.write ' ' unless options.blank? or comments.blank? stream.write '# ' + comments.join('; ') unless comments.blank? From ef3d9d32f1b608e4f09c1e4e5b0a6be72d3802da Mon Sep 17 00:00:00 2001 From: Adam Hallett Date: Sat, 17 Dec 2022 12:53:04 -0500 Subject: [PATCH 2/4] fix trailing comma --- lib/schema_plus/core/schema_dump.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/schema_plus/core/schema_dump.rb b/lib/schema_plus/core/schema_dump.rb index dda7a8c..3a31217 100644 --- a/lib/schema_plus/core/schema_dump.rb +++ b/lib/schema_plus/core/schema_dump.rb @@ -107,8 +107,8 @@ def assemble(stream, typelen, namelen) end if options[:default].is_a?(Proc) default = options.delete(:default) - stream.write "default: -> { \"#{default.call}\" }, " - stream.write options.to_s.sub(/^{(.*)}$/, '\1') unless options.blank? + stream.write "default: -> { \"#{default.call}\" }" + stream.write ", " unless options.blank? end stream.write options.to_s.sub(/^{(.*)}$/, '\1') unless options.blank? stream.write ' ' unless options.blank? or comments.blank? From b852cc6ae60d956bfaae7e8370d146b48cf1c820 Mon Sep 17 00:00:00 2001 From: Adam Hallett Date: Mon, 17 Apr 2023 11:44:26 -0500 Subject: [PATCH 3/4] add a version check --- lib/schema_plus/core/schema_dump.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/schema_plus/core/schema_dump.rb b/lib/schema_plus/core/schema_dump.rb index 3a31217..e8410e7 100644 --- a/lib/schema_plus/core/schema_dump.rb +++ b/lib/schema_plus/core/schema_dump.rb @@ -105,10 +105,12 @@ def assemble(stream, typelen, namelen) pr += ',' unless options.blank? stream.write "%-#{namelen+3}s " % pr end - if options[:default].is_a?(Proc) - default = options.delete(:default) - stream.write "default: -> { \"#{default.call}\" }" - stream.write ", " unless options.blank? + if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.0.0') + if options[:default].is_a?(Proc) && + default = options.delete(:default) + stream.write "default: -> { \"#{default.call}\" }" + stream.write ", " unless options.blank? + end end stream.write options.to_s.sub(/^{(.*)}$/, '\1') unless options.blank? stream.write ' ' unless options.blank? or comments.blank? From 3a5356a4b50f1b04358703ebe876931e99c717d1 Mon Sep 17 00:00:00 2001 From: Adam Hallett Date: Mon, 17 Apr 2023 11:48:52 -0500 Subject: [PATCH 4/4] fix --- lib/schema_plus/core/schema_dump.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/schema_plus/core/schema_dump.rb b/lib/schema_plus/core/schema_dump.rb index e8410e7..65f5889 100644 --- a/lib/schema_plus/core/schema_dump.rb +++ b/lib/schema_plus/core/schema_dump.rb @@ -105,7 +105,7 @@ def assemble(stream, typelen, namelen) pr += ',' unless options.blank? stream.write "%-#{namelen+3}s " % pr end - if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.0.0') + if Gem::Version.new(::ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.0.0') if options[:default].is_a?(Proc) && default = options.delete(:default) stream.write "default: -> { \"#{default.call}\" }"