aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib
diff options
context:
space:
mode:
Diffstat (limited to 'activestorage/lib')
-rw-r--r--activestorage/lib/active_storage/attached.rb6
-rw-r--r--activestorage/lib/active_storage/attached/macros.rb4
-rw-r--r--activestorage/lib/active_storage/attached/one.rb22
-rw-r--r--activestorage/lib/active_storage/service.rb2
-rw-r--r--activestorage/lib/active_storage/service/disk_service.rb2
-rw-r--r--activestorage/lib/active_storage/service/gcs_service.rb10
6 files changed, 33 insertions, 13 deletions
diff --git a/activestorage/lib/active_storage/attached.rb b/activestorage/lib/active_storage/attached.rb
index 9c4b6d5d1d..c08fd56652 100644
--- a/activestorage/lib/active_storage/attached.rb
+++ b/activestorage/lib/active_storage/attached.rb
@@ -8,10 +8,10 @@ module ActiveStorage
# Abstract base class for the concrete ActiveStorage::Attached::One and ActiveStorage::Attached::Many
# classes that both provide proxy access to the blob association for a record.
class Attached
- attr_reader :name, :record
+ attr_reader :name, :record, :dependent
- def initialize(name, record)
- @name, @record = name, record
+ def initialize(name, record, dependent:)
+ @name, @record, @dependent = name, record, dependent
end
private
diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb
index f3879ee2e3..35a081adc4 100644
--- a/activestorage/lib/active_storage/attached/macros.rb
+++ b/activestorage/lib/active_storage/attached/macros.rb
@@ -26,7 +26,7 @@ module ActiveStorage
def has_one_attached(name, dependent: :purge_later)
class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name}
- @active_storage_attached_#{name} ||= ActiveStorage::Attached::One.new("#{name}", self)
+ @active_storage_attached_#{name} ||= ActiveStorage::Attached::One.new("#{name}", self, dependent: #{dependent == :purge_later ? ":purge_later" : "false"})
end
CODE
@@ -65,7 +65,7 @@ module ActiveStorage
def has_many_attached(name, dependent: :purge_later)
class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name}
- @active_storage_attached_#{name} ||= ActiveStorage::Attached::Many.new("#{name}", self)
+ @active_storage_attached_#{name} ||= ActiveStorage::Attached::Many.new("#{name}", self, dependent: #{dependent == :purge_later ? ":purge_later" : "false"})
end
CODE
diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb
index 2e5831348e..ac90f32d95 100644
--- a/activestorage/lib/active_storage/attached/one.rb
+++ b/activestorage/lib/active_storage/attached/one.rb
@@ -21,11 +21,14 @@ module ActiveStorage
# person.avatar.attach(io: File.open("~/face.jpg"), filename: "face.jpg", content_type: "image/jpg")
# person.avatar.attach(avatar_blob) # ActiveStorage::Blob object
def attach(attachable)
- write_attachment \
- ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable))
+ if attached? && dependent == :purge_later
+ replace attachable
+ else
+ write_attachment create_attachment_from(attachable)
+ end
end
- # Returns true if an attachment has been made.
+ # Returns +true+ if an attachment has been made.
#
# class User < ActiveRecord::Base
# has_one_attached :avatar
@@ -53,6 +56,19 @@ module ActiveStorage
end
private
+ def replace(attachable)
+ blob.tap do
+ transaction do
+ destroy
+ write_attachment create_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))
+ end
+
def write_attachment(attachment)
record.public_send("#{name}_attachment=", attachment)
end
diff --git a/activestorage/lib/active_storage/service.rb b/activestorage/lib/active_storage/service.rb
index ce736b8728..b80fdea1ab 100644
--- a/activestorage/lib/active_storage/service.rb
+++ b/activestorage/lib/active_storage/service.rb
@@ -77,7 +77,7 @@ module ActiveStorage
raise NotImplementedError
end
- # Return true if a file exists at the +key+.
+ # Return +true+ if a file exists at the +key+.
def exist?(key)
raise NotImplementedError
end
diff --git a/activestorage/lib/active_storage/service/disk_service.rb b/activestorage/lib/active_storage/service/disk_service.rb
index 5aa8d74a5a..f600753a08 100644
--- a/activestorage/lib/active_storage/service/disk_service.rb
+++ b/activestorage/lib/active_storage/service/disk_service.rb
@@ -66,7 +66,7 @@ module ActiveStorage
verified_key_with_expiration,
filename: filename, disposition: disposition, content_type: content_type
else
- "/rails/active_storage/disk/#{verified_key_with_expiration}/#{filename}?disposition=#{disposition}&content_type=#{content_type}"
+ "/rails/active_storage/disk/#{verified_key_with_expiration}/#{filename}?content_type=#{content_type}&disposition=#{disposition}"
end
payload[:url] = generated_url
diff --git a/activestorage/lib/active_storage/service/gcs_service.rb b/activestorage/lib/active_storage/service/gcs_service.rb
index ebf24a36d7..a0ba5654a1 100644
--- a/activestorage/lib/active_storage/service/gcs_service.rb
+++ b/activestorage/lib/active_storage/service/gcs_service.rb
@@ -9,8 +9,8 @@ module ActiveStorage
class Service::GCSService < Service
attr_reader :client, :bucket
- def initialize(project:, keyfile:, bucket:)
- @client = Google::Cloud::Storage.new(project: project, keyfile: keyfile)
+ def initialize(project:, keyfile:, bucket:, **options)
+ @client = Google::Cloud::Storage.new(project: project, keyfile: keyfile, **options)
@bucket = @client.bucket(bucket)
end
@@ -35,7 +35,11 @@ module ActiveStorage
def delete(key)
instrument :delete, key do
- file_for(key).try(:delete)
+ begin
+ file_for(key).try(:delete)
+ rescue Google::Cloud::NotFoundError
+ # Ignore files already deleted
+ end
end
end