diff options
author | George Claghorn <george@basecamp.com> | 2017-11-19 17:34:07 -0500 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2017-11-19 17:34:07 -0500 |
commit | e05e2ae44f1ecf8e9bb5949f531305c15bc3c665 (patch) | |
tree | f02c38635cead7a99122d2eb99e7eed9efde8c92 /activestorage/lib/active_storage | |
parent | eeaf9cf61c3cd14929583878785c31dab79e2196 (diff) | |
download | rails-e05e2ae44f1ecf8e9bb5949f531305c15bc3c665.tar.gz rails-e05e2ae44f1ecf8e9bb5949f531305c15bc3c665.tar.bz2 rails-e05e2ae44f1ecf8e9bb5949f531305c15bc3c665.zip |
Permit attaching files to new records
Closes #31164.
Diffstat (limited to 'activestorage/lib/active_storage')
-rw-r--r-- | activestorage/lib/active_storage/attached/many.rb | 6 | ||||
-rw-r--r-- | activestorage/lib/active_storage/attached/one.rb | 8 |
2 files changed, 9 insertions, 5 deletions
diff --git a/activestorage/lib/active_storage/attached/many.rb b/activestorage/lib/active_storage/attached/many.rb index 0b3400bccf..6eace65b79 100644 --- a/activestorage/lib/active_storage/attached/many.rb +++ b/activestorage/lib/active_storage/attached/many.rb @@ -20,7 +20,11 @@ module ActiveStorage # document.images.attach([ first_blob, second_blob ]) def attach(*attachables) attachables.flatten.collect do |attachable| - attachments.create!(name: name, blob: create_blob_from(attachable)) + if record.new_record? + attachments.build(record: record, blob: create_blob_from(attachable)) + else + attachments.create!(record: record, blob: create_blob_from(attachable)) + end end end diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb index 7092f6b109..0244232b2c 100644 --- a/activestorage/lib/active_storage/attached/one.rb +++ b/activestorage/lib/active_storage/attached/one.rb @@ -23,7 +23,7 @@ module ActiveStorage if attached? && dependent == :purge_later replace attachable else - write_attachment create_attachment_from(attachable) + write_attachment build_attachment_from(attachable) end end @@ -67,13 +67,13 @@ module ActiveStorage blob.tap do transaction do detach - write_attachment create_attachment_from(attachable) + write_attachment build_attachment_from(attachable) end end.purge_later end - def create_attachment_from(attachable) - ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable)) + def build_attachment_from(attachable) + ActiveStorage::Attachment.new(record: record, name: name, blob: create_blob_from(attachable)) end def write_attachment(attachment) |