activestorage 5.2.4.4 → 6.1.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activestorage might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +180 -69
- data/MIT-LICENSE +1 -1
- data/README.md +43 -8
- data/app/assets/javascripts/activestorage.js +5 -2
- data/app/controllers/active_storage/base_controller.rb +13 -4
- data/app/controllers/active_storage/blobs/proxy_controller.rb +14 -0
- data/app/controllers/active_storage/{blobs_controller.rb → blobs/redirect_controller.rb} +3 -3
- data/app/controllers/active_storage/direct_uploads_controller.rb +2 -2
- data/app/controllers/active_storage/disk_controller.rb +13 -22
- data/app/controllers/active_storage/representations/proxy_controller.rb +19 -0
- data/app/controllers/active_storage/{representations_controller.rb → representations/redirect_controller.rb} +3 -3
- data/app/controllers/concerns/active_storage/file_server.rb +18 -0
- data/app/controllers/concerns/active_storage/set_blob.rb +1 -1
- data/app/controllers/concerns/active_storage/set_current.rb +15 -0
- data/app/controllers/concerns/active_storage/set_headers.rb +12 -0
- data/app/javascript/activestorage/blob_record.js +7 -2
- data/app/jobs/active_storage/analyze_job.rb +5 -0
- data/app/jobs/active_storage/base_job.rb +0 -1
- data/app/jobs/active_storage/mirror_job.rb +15 -0
- data/app/jobs/active_storage/purge_job.rb +3 -0
- data/app/models/active_storage/attachment.rb +35 -16
- data/app/models/active_storage/blob.rb +178 -68
- data/app/models/active_storage/blob/analyzable.rb +6 -2
- data/app/models/active_storage/blob/identifiable.rb +7 -6
- data/app/models/active_storage/blob/representable.rb +36 -6
- data/app/models/active_storage/filename.rb +0 -6
- data/app/models/active_storage/preview.rb +37 -12
- data/app/models/active_storage/record.rb +7 -0
- data/app/models/active_storage/variant.rb +53 -67
- data/app/models/active_storage/variant_record.rb +8 -0
- data/app/models/active_storage/variant_with_record.rb +54 -0
- data/app/models/active_storage/variation.rb +30 -34
- data/config/routes.rb +66 -15
- data/db/migrate/20170806125915_create_active_storage_tables.rb +14 -5
- data/db/update_migrate/20190112182829_add_service_name_to_active_storage_blobs.rb +17 -0
- data/db/update_migrate/20191206030411_create_active_storage_variant_records.rb +11 -0
- data/lib/active_storage.rb +29 -6
- data/lib/active_storage/analyzer.rb +15 -4
- data/lib/active_storage/analyzer/image_analyzer.rb +14 -4
- data/lib/active_storage/analyzer/null_analyzer.rb +4 -0
- data/lib/active_storage/analyzer/video_analyzer.rb +17 -8
- data/lib/active_storage/attached.rb +7 -22
- data/lib/active_storage/attached/changes.rb +16 -0
- data/lib/active_storage/attached/changes/create_many.rb +47 -0
- data/lib/active_storage/attached/changes/create_one.rb +82 -0
- data/lib/active_storage/attached/changes/create_one_of_many.rb +10 -0
- data/lib/active_storage/attached/changes/delete_many.rb +27 -0
- data/lib/active_storage/attached/changes/delete_one.rb +19 -0
- data/lib/active_storage/attached/many.rb +19 -12
- data/lib/active_storage/attached/model.rb +212 -0
- data/lib/active_storage/attached/one.rb +19 -21
- data/lib/active_storage/downloader.rb +43 -0
- data/lib/active_storage/engine.rb +58 -23
- data/lib/active_storage/errors.rb +22 -3
- data/lib/active_storage/gem_version.rb +4 -4
- data/lib/active_storage/log_subscriber.rb +6 -0
- data/lib/active_storage/previewer.rb +24 -13
- data/lib/active_storage/previewer/mupdf_previewer.rb +3 -3
- data/lib/active_storage/previewer/poppler_pdf_previewer.rb +5 -5
- data/lib/active_storage/previewer/video_previewer.rb +17 -10
- data/lib/active_storage/reflection.rb +64 -0
- data/lib/active_storage/service.rb +44 -12
- data/lib/active_storage/service/azure_storage_service.rb +65 -44
- data/lib/active_storage/service/configurator.rb +6 -2
- data/lib/active_storage/service/disk_service.rb +57 -44
- data/lib/active_storage/service/gcs_service.rb +68 -64
- data/lib/active_storage/service/mirror_service.rb +31 -7
- data/lib/active_storage/service/registry.rb +32 -0
- data/lib/active_storage/service/s3_service.rb +58 -24
- data/lib/active_storage/transformers/image_processing_transformer.rb +45 -0
- data/lib/active_storage/transformers/transformer.rb +39 -0
- data/lib/tasks/activestorage.rake +7 -0
- metadata +84 -19
- data/app/models/active_storage/filename/parameters.rb +0 -36
- data/lib/active_storage/attached/macros.rb +0 -110
- data/lib/active_storage/downloading.rb +0 -39
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveStorage
|
4
|
+
module Transformers
|
5
|
+
# A Transformer applies a set of transformations to an image.
|
6
|
+
#
|
7
|
+
# The following concrete subclasses are included in Active Storage:
|
8
|
+
#
|
9
|
+
# * ActiveStorage::Transformers::ImageProcessingTransformer:
|
10
|
+
# backed by ImageProcessing, a common interface for MiniMagick and ruby-vips
|
11
|
+
class Transformer
|
12
|
+
attr_reader :transformations
|
13
|
+
|
14
|
+
def initialize(transformations)
|
15
|
+
@transformations = transformations
|
16
|
+
end
|
17
|
+
|
18
|
+
# Applies the transformations to the source image in +file+, producing a target image in the
|
19
|
+
# specified +format+. Yields an open Tempfile containing the target image. Closes and unlinks
|
20
|
+
# the output tempfile after yielding to the given block. Returns the result of the block.
|
21
|
+
def transform(file, format:)
|
22
|
+
output = process(file, format: format)
|
23
|
+
|
24
|
+
begin
|
25
|
+
yield output
|
26
|
+
ensure
|
27
|
+
output.close!
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
# Returns an open Tempfile containing a transformed image in the given +format+.
|
33
|
+
# All subclasses implement this method.
|
34
|
+
def process(file, format:) #:doc:
|
35
|
+
raise NotImplementedError
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -12,4 +12,11 @@ namespace :active_storage do
|
|
12
12
|
Rake::Task["app:active_storage:install:migrations"].invoke
|
13
13
|
end
|
14
14
|
end
|
15
|
+
|
16
|
+
# desc "Copy over the migrations needed to the application upgrading"
|
17
|
+
task update: :environment do
|
18
|
+
ENV["MIGRATIONS_PATH"] = "db/update_migrate"
|
19
|
+
|
20
|
+
Rake::Task["active_storage:install"].invoke
|
21
|
+
end
|
15
22
|
end
|
metadata
CHANGED
@@ -1,43 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activestorage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 6.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heinemeier Hansson
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 6.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 6.1.1
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: actionpack
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - '='
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
33
|
+
version: 6.1.1
|
20
34
|
type: :runtime
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - '='
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
40
|
+
version: 6.1.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activejob
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 6.1.1
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 6.1.1
|
27
55
|
- !ruby/object:Gem::Dependency
|
28
56
|
name: activerecord
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|
30
58
|
requirements:
|
31
59
|
- - '='
|
32
60
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
61
|
+
version: 6.1.1
|
34
62
|
type: :runtime
|
35
63
|
prerelease: false
|
36
64
|
version_requirements: !ruby/object:Gem::Requirement
|
37
65
|
requirements:
|
38
66
|
- - '='
|
39
67
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
68
|
+
version: 6.1.1
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
70
|
name: marcel
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +80,20 @@ dependencies:
|
|
52
80
|
- - "~>"
|
53
81
|
- !ruby/object:Gem::Version
|
54
82
|
version: 0.3.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: mimemagic
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.3.2
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.3.2
|
55
97
|
description: Attach cloud and local files in Rails applications.
|
56
98
|
email: david@loudthinking.com
|
57
99
|
executables: []
|
@@ -63,11 +105,16 @@ files:
|
|
63
105
|
- README.md
|
64
106
|
- app/assets/javascripts/activestorage.js
|
65
107
|
- app/controllers/active_storage/base_controller.rb
|
66
|
-
- app/controllers/active_storage/
|
108
|
+
- app/controllers/active_storage/blobs/proxy_controller.rb
|
109
|
+
- app/controllers/active_storage/blobs/redirect_controller.rb
|
67
110
|
- app/controllers/active_storage/direct_uploads_controller.rb
|
68
111
|
- app/controllers/active_storage/disk_controller.rb
|
69
|
-
- app/controllers/active_storage/
|
112
|
+
- app/controllers/active_storage/representations/proxy_controller.rb
|
113
|
+
- app/controllers/active_storage/representations/redirect_controller.rb
|
114
|
+
- app/controllers/concerns/active_storage/file_server.rb
|
70
115
|
- app/controllers/concerns/active_storage/set_blob.rb
|
116
|
+
- app/controllers/concerns/active_storage/set_current.rb
|
117
|
+
- app/controllers/concerns/active_storage/set_headers.rb
|
71
118
|
- app/javascript/activestorage/blob_record.js
|
72
119
|
- app/javascript/activestorage/blob_upload.js
|
73
120
|
- app/javascript/activestorage/direct_upload.js
|
@@ -79,6 +126,7 @@ files:
|
|
79
126
|
- app/javascript/activestorage/ujs.js
|
80
127
|
- app/jobs/active_storage/analyze_job.rb
|
81
128
|
- app/jobs/active_storage/base_job.rb
|
129
|
+
- app/jobs/active_storage/mirror_job.rb
|
82
130
|
- app/jobs/active_storage/purge_job.rb
|
83
131
|
- app/models/active_storage/attachment.rb
|
84
132
|
- app/models/active_storage/blob.rb
|
@@ -87,22 +135,32 @@ files:
|
|
87
135
|
- app/models/active_storage/blob/representable.rb
|
88
136
|
- app/models/active_storage/current.rb
|
89
137
|
- app/models/active_storage/filename.rb
|
90
|
-
- app/models/active_storage/filename/parameters.rb
|
91
138
|
- app/models/active_storage/preview.rb
|
139
|
+
- app/models/active_storage/record.rb
|
92
140
|
- app/models/active_storage/variant.rb
|
141
|
+
- app/models/active_storage/variant_record.rb
|
142
|
+
- app/models/active_storage/variant_with_record.rb
|
93
143
|
- app/models/active_storage/variation.rb
|
94
144
|
- config/routes.rb
|
95
145
|
- db/migrate/20170806125915_create_active_storage_tables.rb
|
146
|
+
- db/update_migrate/20190112182829_add_service_name_to_active_storage_blobs.rb
|
147
|
+
- db/update_migrate/20191206030411_create_active_storage_variant_records.rb
|
96
148
|
- lib/active_storage.rb
|
97
149
|
- lib/active_storage/analyzer.rb
|
98
150
|
- lib/active_storage/analyzer/image_analyzer.rb
|
99
151
|
- lib/active_storage/analyzer/null_analyzer.rb
|
100
152
|
- lib/active_storage/analyzer/video_analyzer.rb
|
101
153
|
- lib/active_storage/attached.rb
|
102
|
-
- lib/active_storage/attached/
|
154
|
+
- lib/active_storage/attached/changes.rb
|
155
|
+
- lib/active_storage/attached/changes/create_many.rb
|
156
|
+
- lib/active_storage/attached/changes/create_one.rb
|
157
|
+
- lib/active_storage/attached/changes/create_one_of_many.rb
|
158
|
+
- lib/active_storage/attached/changes/delete_many.rb
|
159
|
+
- lib/active_storage/attached/changes/delete_one.rb
|
103
160
|
- lib/active_storage/attached/many.rb
|
161
|
+
- lib/active_storage/attached/model.rb
|
104
162
|
- lib/active_storage/attached/one.rb
|
105
|
-
- lib/active_storage/
|
163
|
+
- lib/active_storage/downloader.rb
|
106
164
|
- lib/active_storage/engine.rb
|
107
165
|
- lib/active_storage/errors.rb
|
108
166
|
- lib/active_storage/gem_version.rb
|
@@ -111,22 +169,29 @@ files:
|
|
111
169
|
- lib/active_storage/previewer/mupdf_previewer.rb
|
112
170
|
- lib/active_storage/previewer/poppler_pdf_previewer.rb
|
113
171
|
- lib/active_storage/previewer/video_previewer.rb
|
172
|
+
- lib/active_storage/reflection.rb
|
114
173
|
- lib/active_storage/service.rb
|
115
174
|
- lib/active_storage/service/azure_storage_service.rb
|
116
175
|
- lib/active_storage/service/configurator.rb
|
117
176
|
- lib/active_storage/service/disk_service.rb
|
118
177
|
- lib/active_storage/service/gcs_service.rb
|
119
178
|
- lib/active_storage/service/mirror_service.rb
|
179
|
+
- lib/active_storage/service/registry.rb
|
120
180
|
- lib/active_storage/service/s3_service.rb
|
181
|
+
- lib/active_storage/transformers/image_processing_transformer.rb
|
182
|
+
- lib/active_storage/transformers/transformer.rb
|
121
183
|
- lib/active_storage/version.rb
|
122
184
|
- lib/tasks/activestorage.rake
|
123
|
-
homepage:
|
185
|
+
homepage: https://rubyonrails.org
|
124
186
|
licenses:
|
125
187
|
- MIT
|
126
188
|
metadata:
|
127
|
-
|
128
|
-
changelog_uri: https://github.com/rails/rails/blob/
|
129
|
-
|
189
|
+
bug_tracker_uri: https://github.com/rails/rails/issues
|
190
|
+
changelog_uri: https://github.com/rails/rails/blob/v6.1.1/activestorage/CHANGELOG.md
|
191
|
+
documentation_uri: https://api.rubyonrails.org/v6.1.1/
|
192
|
+
mailing_list_uri: https://discuss.rubyonrails.org/c/rubyonrails-talk
|
193
|
+
source_code_uri: https://github.com/rails/rails/tree/v6.1.1/activestorage
|
194
|
+
post_install_message:
|
130
195
|
rdoc_options: []
|
131
196
|
require_paths:
|
132
197
|
- lib
|
@@ -134,15 +199,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
134
199
|
requirements:
|
135
200
|
- - ">="
|
136
201
|
- !ruby/object:Gem::Version
|
137
|
-
version: 2.
|
202
|
+
version: 2.5.0
|
138
203
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
204
|
requirements:
|
140
205
|
- - ">="
|
141
206
|
- !ruby/object:Gem::Version
|
142
207
|
version: '0'
|
143
208
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
145
|
-
signing_key:
|
209
|
+
rubygems_version: 3.2.3
|
210
|
+
signing_key:
|
146
211
|
specification_version: 4
|
147
212
|
summary: Local and cloud file storage framework.
|
148
213
|
test_files: []
|
@@ -1,36 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class ActiveStorage::Filename::Parameters #:nodoc:
|
4
|
-
attr_reader :filename
|
5
|
-
|
6
|
-
def initialize(filename)
|
7
|
-
@filename = filename
|
8
|
-
end
|
9
|
-
|
10
|
-
def combined
|
11
|
-
"#{ascii}; #{utf8}"
|
12
|
-
end
|
13
|
-
|
14
|
-
TRADITIONAL_ESCAPED_CHAR = /[^ A-Za-z0-9!#$+.^_`|~-]/
|
15
|
-
|
16
|
-
def ascii
|
17
|
-
'filename="' + percent_escape(I18n.transliterate(filename.sanitized), TRADITIONAL_ESCAPED_CHAR) + '"'
|
18
|
-
end
|
19
|
-
|
20
|
-
RFC_5987_ESCAPED_CHAR = /[^A-Za-z0-9!#$&+.^_`|~-]/
|
21
|
-
|
22
|
-
def utf8
|
23
|
-
"filename*=UTF-8''" + percent_escape(filename.sanitized, RFC_5987_ESCAPED_CHAR)
|
24
|
-
end
|
25
|
-
|
26
|
-
def to_s
|
27
|
-
combined
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
def percent_escape(string, pattern)
|
32
|
-
string.gsub(pattern) do |char|
|
33
|
-
char.bytes.map { |byte| "%%%02X" % byte }.join
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,110 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveStorage
|
4
|
-
# Provides the class-level DSL for declaring that an Active Record model has attached blobs.
|
5
|
-
module Attached::Macros
|
6
|
-
# Specifies the relation between a single attachment and the model.
|
7
|
-
#
|
8
|
-
# class User < ActiveRecord::Base
|
9
|
-
# has_one_attached :avatar
|
10
|
-
# end
|
11
|
-
#
|
12
|
-
# There is no column defined on the model side, Active Storage takes
|
13
|
-
# care of the mapping between your records and the attachment.
|
14
|
-
#
|
15
|
-
# To avoid N+1 queries, you can include the attached blobs in your query like so:
|
16
|
-
#
|
17
|
-
# User.with_attached_avatar
|
18
|
-
#
|
19
|
-
# Under the covers, this relationship is implemented as a +has_one+ association to a
|
20
|
-
# ActiveStorage::Attachment record and a +has_one-through+ association to a
|
21
|
-
# ActiveStorage::Blob record. These associations are available as +avatar_attachment+
|
22
|
-
# and +avatar_blob+. But you shouldn't need to work with these associations directly in
|
23
|
-
# most circumstances.
|
24
|
-
#
|
25
|
-
# The system has been designed to having you go through the ActiveStorage::Attached::One
|
26
|
-
# proxy that provides the dynamic proxy to the associations and factory methods, like +attach+.
|
27
|
-
#
|
28
|
-
# If the +:dependent+ option isn't set, the attachment will be purged
|
29
|
-
# (i.e. destroyed) whenever the record is destroyed.
|
30
|
-
def has_one_attached(name, dependent: :purge_later)
|
31
|
-
class_eval <<-CODE, __FILE__, __LINE__ + 1
|
32
|
-
def #{name}
|
33
|
-
@active_storage_attached_#{name} ||= ActiveStorage::Attached::One.new("#{name}", self, dependent: #{dependent == :purge_later ? ":purge_later" : "false"})
|
34
|
-
end
|
35
|
-
|
36
|
-
def #{name}=(attachable)
|
37
|
-
#{name}.attach(attachable)
|
38
|
-
end
|
39
|
-
CODE
|
40
|
-
|
41
|
-
has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record, inverse_of: :record, dependent: false
|
42
|
-
has_one :"#{name}_blob", through: :"#{name}_attachment", class_name: "ActiveStorage::Blob", source: :blob
|
43
|
-
|
44
|
-
scope :"with_attached_#{name}", -> { includes("#{name}_attachment": :blob) }
|
45
|
-
|
46
|
-
if dependent == :purge_later
|
47
|
-
after_destroy_commit { public_send(name).purge_later }
|
48
|
-
else
|
49
|
-
before_destroy { public_send(name).detach }
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
# Specifies the relation between multiple attachments and the model.
|
54
|
-
#
|
55
|
-
# class Gallery < ActiveRecord::Base
|
56
|
-
# has_many_attached :photos
|
57
|
-
# end
|
58
|
-
#
|
59
|
-
# There are no columns defined on the model side, Active Storage takes
|
60
|
-
# care of the mapping between your records and the attachments.
|
61
|
-
#
|
62
|
-
# To avoid N+1 queries, you can include the attached blobs in your query like so:
|
63
|
-
#
|
64
|
-
# Gallery.where(user: Current.user).with_attached_photos
|
65
|
-
#
|
66
|
-
# Under the covers, this relationship is implemented as a +has_many+ association to a
|
67
|
-
# ActiveStorage::Attachment record and a +has_many-through+ association to a
|
68
|
-
# ActiveStorage::Blob record. These associations are available as +photos_attachments+
|
69
|
-
# and +photos_blobs+. But you shouldn't need to work with these associations directly in
|
70
|
-
# most circumstances.
|
71
|
-
#
|
72
|
-
# The system has been designed to having you go through the ActiveStorage::Attached::Many
|
73
|
-
# proxy that provides the dynamic proxy to the associations and factory methods, like +#attach+.
|
74
|
-
#
|
75
|
-
# If the +:dependent+ option isn't set, all the attachments will be purged
|
76
|
-
# (i.e. destroyed) whenever the record is destroyed.
|
77
|
-
def has_many_attached(name, dependent: :purge_later)
|
78
|
-
class_eval <<-CODE, __FILE__, __LINE__ + 1
|
79
|
-
def #{name}
|
80
|
-
@active_storage_attached_#{name} ||= ActiveStorage::Attached::Many.new("#{name}", self, dependent: #{dependent == :purge_later ? ":purge_later" : "false"})
|
81
|
-
end
|
82
|
-
|
83
|
-
def #{name}=(attachables)
|
84
|
-
#{name}.attach(attachables)
|
85
|
-
end
|
86
|
-
CODE
|
87
|
-
|
88
|
-
has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: false do
|
89
|
-
def purge
|
90
|
-
each(&:purge)
|
91
|
-
reset
|
92
|
-
end
|
93
|
-
|
94
|
-
def purge_later
|
95
|
-
each(&:purge_later)
|
96
|
-
reset
|
97
|
-
end
|
98
|
-
end
|
99
|
-
has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob
|
100
|
-
|
101
|
-
scope :"with_attached_#{name}", -> { includes("#{name}_attachments": :blob) }
|
102
|
-
|
103
|
-
if dependent == :purge_later
|
104
|
-
after_destroy_commit { public_send(name).purge_later }
|
105
|
-
else
|
106
|
-
before_destroy { public_send(name).detach }
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "tmpdir"
|
4
|
-
|
5
|
-
module ActiveStorage
|
6
|
-
module Downloading
|
7
|
-
private
|
8
|
-
# Opens a new tempfile in #tempdir and copies blob data into it. Yields the tempfile.
|
9
|
-
def download_blob_to_tempfile #:doc:
|
10
|
-
open_tempfile_for_blob do |file|
|
11
|
-
download_blob_to file
|
12
|
-
yield file
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def open_tempfile_for_blob
|
17
|
-
tempfile = Tempfile.open([ "ActiveStorage", blob.filename.extension_with_delimiter ], tempdir)
|
18
|
-
|
19
|
-
begin
|
20
|
-
yield tempfile
|
21
|
-
ensure
|
22
|
-
tempfile.close!
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# Efficiently downloads blob data into the given file.
|
27
|
-
def download_blob_to(file) #:doc:
|
28
|
-
file.binmode
|
29
|
-
blob.download { |chunk| file.write(chunk) }
|
30
|
-
file.flush
|
31
|
-
file.rewind
|
32
|
-
end
|
33
|
-
|
34
|
-
# Returns the directory in which tempfiles should be opened. Defaults to +Dir.tmpdir+.
|
35
|
-
def tempdir #:doc:
|
36
|
-
Dir.tmpdir
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|