From e13ce5d390691c25b472749c4fae4a58fecc17bd Mon Sep 17 00:00:00 2001 From: Justin Louie <224840+nitsujri@users.noreply.github.com> Date: Mon, 9 Dec 2019 08:31:47 +0800 Subject: [PATCH] Duplicate #892 - Rails 6 save stack too deep on File upload (#899) * fix rails 6 save loop callback stack * update to support backward compatibility * use proc * add same change to fragment * code review * Upgrading sqlite3 to 1.4 --- Gemfile | 2 +- app/models/comfy/cms/file.rb | 7 ++++++- app/models/comfy/cms/fragment.rb | 8 ++++++-- test/gemfiles/5.2.gemfile | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index ebba18473..02e35c33c 100644 --- a/Gemfile +++ b/Gemfile @@ -12,7 +12,7 @@ group :development, :test do gem "puma", "~> 3.11.2" gem "rubocop", "~> 0.55.0", require: false gem "selenium-webdriver", "~> 3.9.0" - gem "sqlite3", "~> 1.3.13" + gem "sqlite3", "~> 1.4" end group :development do diff --git a/app/models/comfy/cms/file.rb b/app/models/comfy/cms/file.rb index e8bb3a673..19b05ce45 100644 --- a/app/models/comfy/cms/file.rb +++ b/app/models/comfy/cms/file.rb @@ -23,7 +23,12 @@ class Comfy::Cms::File < ActiveRecord::Base # -- Callbacks --------------------------------------------------------------- before_validation :assign_label, on: :create before_create :assign_position - after_save :process_attachment + # active_storage attachment behavior changed in rails 6 - see PR#892 for details + if Rails::VERSION::MAJOR >= 6 + before_save :process_attachment + else + after_save :process_attachment + end # -- Validations ------------------------------------------------------------- validates :label, presence: true diff --git a/app/models/comfy/cms/fragment.rb b/app/models/comfy/cms/fragment.rb index 3d4d034dc..5115b6704 100644 --- a/app/models/comfy/cms/fragment.rb +++ b/app/models/comfy/cms/fragment.rb @@ -11,8 +11,12 @@ class Comfy::Cms::Fragment < ActiveRecord::Base attr_reader :files # -- Callbacks --------------------------------------------------------------- - after_save :remove_attachments, - :add_attachments + # active_storage attachment behavior changed in rails 6 - see PR#892 for details + if Rails::VERSION::MAJOR >= 6 + before_save :remove_attachments, :add_attachments + else + after_save :remove_attachments, :add_attachments + end # -- Relationships ----------------------------------------------------------- belongs_to :record, polymorphic: true, touch: true diff --git a/test/gemfiles/5.2.gemfile b/test/gemfiles/5.2.gemfile index 25777ee45..58fb11013 100644 --- a/test/gemfiles/5.2.gemfile +++ b/test/gemfiles/5.2.gemfile @@ -13,7 +13,7 @@ group :development, :test do gem "puma", "~> 3.11.2" gem "rubocop", "~> 0.55.0", require: false gem "selenium-webdriver", "~> 3.9.0" - gem "sqlite3", "~> 1.3.13" + gem "sqlite3", "~> 1.4" end group :test do