aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib/active_storage/attached
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2018-07-13 13:29:33 -0400
committerGeorge Claghorn <george@basecamp.com>2018-07-13 13:29:33 -0400
commit28db8ba60e726d695cf35710cc43ea45966464e9 (patch)
treee8e0306d761f10262d60f9021d47745a831574f1 /activestorage/lib/active_storage/attached
parentd20d6c732613dcc7276cb57d451e2a3bf573df19 (diff)
downloadrails-28db8ba60e726d695cf35710cc43ea45966464e9.tar.gz
rails-28db8ba60e726d695cf35710cc43ea45966464e9.tar.bz2
rails-28db8ba60e726d695cf35710cc43ea45966464e9.zip
Implement ActiveStorage::Attached::{One,Many}#attach in terms of changes
Diffstat (limited to 'activestorage/lib/active_storage/attached')
-rw-r--r--activestorage/lib/active_storage/attached/changes/create_many.rb4
-rw-r--r--activestorage/lib/active_storage/attached/changes/create_one.rb3
-rw-r--r--activestorage/lib/active_storage/attached/many.rb21
-rw-r--r--activestorage/lib/active_storage/attached/one.rb20
4 files changed, 30 insertions, 18 deletions
diff --git a/activestorage/lib/active_storage/attached/changes/create_many.rb b/activestorage/lib/active_storage/attached/changes/create_many.rb
index 3f7ca6a25f..af19328a61 100644
--- a/activestorage/lib/active_storage/attached/changes/create_many.rb
+++ b/activestorage/lib/active_storage/attached/changes/create_many.rb
@@ -12,6 +12,10 @@ module ActiveStorage
@attachments ||= subchanges.collect(&:attachment)
end
+ def blobs
+ @blobs ||= subchanges.collect(&:blob)
+ end
+
def upload
subchanges.each(&:upload)
end
diff --git a/activestorage/lib/active_storage/attached/changes/create_one.rb b/activestorage/lib/active_storage/attached/changes/create_one.rb
index 98aea36861..5812fd2b08 100644
--- a/activestorage/lib/active_storage/attached/changes/create_one.rb
+++ b/activestorage/lib/active_storage/attached/changes/create_one.rb
@@ -1,5 +1,8 @@
# frozen_string_literal: true
+require "action_dispatch"
+require "action_dispatch/http/upload"
+
module ActiveStorage
class Attached::Changes::CreateOne #:nodoc:
attr_reader :name, :record, :attachable
diff --git a/activestorage/lib/active_storage/attached/many.rb b/activestorage/lib/active_storage/attached/many.rb
index 073cc013d8..25f88284df 100644
--- a/activestorage/lib/active_storage/attached/many.rb
+++ b/activestorage/lib/active_storage/attached/many.rb
@@ -12,19 +12,26 @@ module ActiveStorage
change.present? ? change.attachments : record.public_send("#{name}_attachments")
end
- # Associates one or several attachments with the current record, saving them to the database.
+ # Returns all attached blobs.
+ def blobs
+ change.present? ? change.blobs : record.public_send("#{name}_blobs")
+ end
+
+ # Attaches one or more +attachables+ to the record.
+ #
+ # If the record is persisted and unchanged, the attachments are saved to
+ # the database immediately. Otherwise, they'll be saved to the DB when the
+ # record is next saved.
#
# document.images.attach(params[:images]) # Array of ActionDispatch::Http::UploadedFile objects
# document.images.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload
# document.images.attach(io: File.open("/path/to/racecar.jpg"), filename: "racecar.jpg", content_type: "image/jpg")
# document.images.attach([ first_blob, second_blob ])
def attach(*attachables)
- attachables.flatten.collect do |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
+ if record.persisted? && !record.changed?
+ record.update(name => blobs + attachables.flatten)
+ else
+ record.public_send("#{name}=", blobs + attachables.flatten)
end
end
diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb
index 4a6bb1ffaa..c039226fcd 100644
--- a/activestorage/lib/active_storage/attached/one.rb
+++ b/activestorage/lib/active_storage/attached/one.rb
@@ -17,17 +17,21 @@ module ActiveStorage
!attached?
end
- # Associates a given attachment with the current record, saving it to the database.
+ # Attaches an +attachable+ to the record.
+ #
+ # If the record is persisted and unchanged, the attachment is saved to
+ # the database immediately. Otherwise, it'll be saved to the DB when the
+ # record is next saved.
#
# person.avatar.attach(params[:avatar]) # ActionDispatch::Http::UploadedFile object
# person.avatar.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload
# person.avatar.attach(io: File.open("/path/to/face.jpg"), filename: "face.jpg", content_type: "image/jpg")
# person.avatar.attach(avatar_blob) # ActiveStorage::Blob object
def attach(attachable)
- new_blob = create_blob_from(attachable)
-
- if !attached? || new_blob != blob
- write_attachment build_attachment(blob: new_blob)
+ if record.persisted? && !record.changed?
+ record.update(name => attachable)
+ else
+ record.public_send("#{name}=", attachable)
end
end
@@ -68,12 +72,6 @@ module ActiveStorage
end
private
- delegate :transaction, to: :record
-
- def build_attachment(blob:)
- Attachment.new(record: record, name: name, blob: blob)
- end
-
def write_attachment(attachment)
record.public_send("#{name}_attachment=", attachment)
end