aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib/active_storage/attached/model.rb
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2019-07-20 06:33:11 -0400
committerGitHub <noreply@github.com>2019-07-20 06:33:11 -0400
commit400b2103540e1dfb370eab29a891687d778ad357 (patch)
treef5b85f2bca64559e9b75edfd1e176db03ecaa7f8 /activestorage/lib/active_storage/attached/model.rb
parent876548a7e7307fc7d4c22fb6560df1474353d0cf (diff)
downloadrails-400b2103540e1dfb370eab29a891687d778ad357.tar.gz
rails-400b2103540e1dfb370eab29a891687d778ad357.tar.bz2
rails-400b2103540e1dfb370eab29a891687d778ad357.zip
Preserve existing attachment assignment behavior for upgraded apps
Assigning to a collection of attachments appends rather than replacing, as in 5.2. Existing 5.2 apps that rely on this behavior will no longer break when they're upgraded to 6.0. For apps generated on 6.0 or newer, assigning replaces the existing attachments in the collection. #attach should be used to add new attachments to the collection without removing existing ones. I expect that we'll deprecate the old behavior in 6.1. Closes #36374.
Diffstat (limited to 'activestorage/lib/active_storage/attached/model.rb')
-rw-r--r--activestorage/lib/active_storage/attached/model.rb17
1 files changed, 12 insertions, 5 deletions
diff --git a/activestorage/lib/active_storage/attached/model.rb b/activestorage/lib/active_storage/attached/model.rb
index ae7f0685f2..06864a846f 100644
--- a/activestorage/lib/active_storage/attached/model.rb
+++ b/activestorage/lib/active_storage/attached/model.rb
@@ -93,12 +93,19 @@ module ActiveStorage
end
def #{name}=(attachables)
- attachment_changes["#{name}"] =
- if attachables.nil? || Array(attachables).none?
- ActiveStorage::Attached::Changes::DeleteMany.new("#{name}", self)
- else
- ActiveStorage::Attached::Changes::CreateMany.new("#{name}", self, attachables)
+ if ActiveStorage.replace_on_assign_to_many
+ attachment_changes["#{name}"] =
+ if attachables.nil? || Array(attachables).none?
+ ActiveStorage::Attached::Changes::DeleteMany.new("#{name}", self)
+ else
+ ActiveStorage::Attached::Changes::CreateMany.new("#{name}", self, attachables)
+ end
+ else
+ if !attachables.nil? || Array(attachables).any?
+ attachment_changes["#{name}"] =
+ ActiveStorage::Attached::Changes::CreateMany.new("#{name}", self, #{name}.blobs + attachables)
end
+ end
end
CODE