@rails/activestorage 6.0.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -9
- package/app/assets/javascripts/activestorage.esm.js +856 -0
- package/app/assets/javascripts/activestorage.js +271 -378
- package/package.json +9 -12
- package/src/blob_record.js +11 -4
- package/src/direct_upload.js +4 -2
- package/src/direct_upload_controller.js +9 -1
- package/src/ujs.js +1 -1
- package/CHANGELOG.md +0 -198
package/README.md
CHANGED
|
@@ -10,13 +10,13 @@ You can read more about Active Storage in the [Active Storage Overview](https://
|
|
|
10
10
|
|
|
11
11
|
## Compared to other storage solutions
|
|
12
12
|
|
|
13
|
-
A key difference to how Active Storage works compared to other attachment solutions in Rails is through the use of built-in [Blob](https://github.com/rails/rails/blob/
|
|
13
|
+
A key difference to how Active Storage works compared to other attachment solutions in Rails is through the use of built-in [Blob](https://github.com/rails/rails/blob/main/activestorage/app/models/active_storage/blob.rb) and [Attachment](https://github.com/rails/rails/blob/main/activestorage/app/models/active_storage/attachment.rb) models (backed by Active Record). This means existing application models do not need to be modified with additional columns to associate with files. Active Storage uses polymorphic associations via the `Attachment` join model, which then connects to the actual `Blob`.
|
|
14
14
|
|
|
15
15
|
`Blob` models store attachment metadata (filename, content-type, etc.), and their identifier key in the storage service. Blob models do not store the actual binary data. They are intended to be immutable in spirit. One file, one blob. You can associate the same blob with multiple application models as well. And if you want to do transformations of a given `Blob`, the idea is that you'll simply create a new one, rather than attempt to mutate the existing one (though of course you can delete the previous version later if you don't need it).
|
|
16
16
|
|
|
17
17
|
## Installation
|
|
18
18
|
|
|
19
|
-
Run `rails active_storage:install` to copy over active_storage migrations.
|
|
19
|
+
Run `bin/rails active_storage:install` to copy over active_storage migrations.
|
|
20
20
|
|
|
21
21
|
NOTE: If the task cannot be found, verify that `require "active_storage/engine"` is present in `config/application.rb`.
|
|
22
22
|
|
|
@@ -32,7 +32,7 @@ class User < ApplicationRecord
|
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
# Attach an avatar to the user.
|
|
35
|
-
user.avatar.attach(io: File.open("/path/to/face.jpg"), filename: "face.jpg", content_type: "image/
|
|
35
|
+
user.avatar.attach(io: File.open("/path/to/face.jpg"), filename: "face.jpg", content_type: "image/jpeg")
|
|
36
36
|
|
|
37
37
|
# Does the user have an avatar?
|
|
38
38
|
user.avatar.attached? # => true
|
|
@@ -55,7 +55,7 @@ url_for(user.avatar)
|
|
|
55
55
|
|
|
56
56
|
class AvatarsController < ApplicationController
|
|
57
57
|
def update
|
|
58
|
-
# params[:avatar] contains
|
|
58
|
+
# params[:avatar] contains an ActionDispatch::Http::UploadedFile object
|
|
59
59
|
Current.user.avatar.attach(params.require(:avatar))
|
|
60
60
|
redirect_to Current.user
|
|
61
61
|
end
|
|
@@ -88,8 +88,7 @@ class MessagesController < ApplicationController
|
|
|
88
88
|
end
|
|
89
89
|
|
|
90
90
|
def create
|
|
91
|
-
message = Message.create! params.require(:message).permit(:title, :content)
|
|
92
|
-
message.images.attach(params[:message][:images])
|
|
91
|
+
message = Message.create! params.require(:message).permit(:title, :content, images: [])
|
|
93
92
|
redirect_to message
|
|
94
93
|
end
|
|
95
94
|
|
|
@@ -106,21 +105,68 @@ Variation of image attachment:
|
|
|
106
105
|
<%= image_tag user.avatar.variant(resize_to_limit: [100, 100]) %>
|
|
107
106
|
```
|
|
108
107
|
|
|
108
|
+
## File serving strategies
|
|
109
|
+
|
|
110
|
+
Active Storage supports two ways to serve files: redirecting and proxying.
|
|
111
|
+
|
|
112
|
+
### Redirecting
|
|
113
|
+
|
|
114
|
+
Active Storage generates stable application URLs for files which, when accessed, redirect to signed, short-lived service URLs. This relieves application servers of the burden of serving file data. It is the default file serving strategy.
|
|
115
|
+
|
|
116
|
+
When the application is configured to proxy files by default, use the `rails_storage_redirect_path` and `_url` route helpers to redirect instead:
|
|
117
|
+
|
|
118
|
+
```erb
|
|
119
|
+
<%= image_tag rails_storage_redirect_path(@user.avatar) %>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Proxying
|
|
123
|
+
|
|
124
|
+
Optionally, files can be proxied instead. This means that your application servers will download file data from the storage service in response to requests. This can be useful for serving files from a CDN.
|
|
125
|
+
|
|
126
|
+
You can configure Active Storage to use proxying by default:
|
|
127
|
+
|
|
128
|
+
```ruby
|
|
129
|
+
# config/initializers/active_storage.rb
|
|
130
|
+
Rails.application.config.active_storage.resolve_model_to_route = :rails_storage_proxy
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Or if you want to explicitly proxy specific attachments there are URL helpers you can use in the form of `rails_storage_proxy_path` and `rails_storage_proxy_url`.
|
|
134
|
+
|
|
135
|
+
```erb
|
|
136
|
+
<%= image_tag rails_storage_proxy_path(@user.avatar) %>
|
|
137
|
+
```
|
|
138
|
+
|
|
109
139
|
## Direct uploads
|
|
110
140
|
|
|
111
141
|
Active Storage, with its included JavaScript library, supports uploading directly from the client to the cloud.
|
|
112
142
|
|
|
113
143
|
### Direct upload installation
|
|
114
144
|
|
|
115
|
-
1. Include
|
|
145
|
+
1. Include the Active Storage JavaScript in your application's JavaScript bundle or reference it directly.
|
|
116
146
|
|
|
147
|
+
Requiring directly without bundling through the asset pipeline in the application html with autostart:
|
|
148
|
+
```html
|
|
149
|
+
<%= javascript_include_tag "activestorage" %>
|
|
150
|
+
```
|
|
151
|
+
Requiring via importmap-rails without bundling through the asset pipeline in the application html without autostart as ESM:
|
|
152
|
+
```ruby
|
|
153
|
+
# config/importmap.rb
|
|
154
|
+
pin "@rails/activestorage", to: "activestorage.esm.js"
|
|
155
|
+
```
|
|
156
|
+
```html
|
|
157
|
+
<script type="module-shim">
|
|
158
|
+
import * as ActiveStorage from "@rails/activestorage"
|
|
159
|
+
ActiveStorage.start()
|
|
160
|
+
</script>
|
|
161
|
+
```
|
|
117
162
|
Using the asset pipeline:
|
|
118
163
|
```js
|
|
119
164
|
//= require activestorage
|
|
120
165
|
```
|
|
121
166
|
Using the npm package:
|
|
122
167
|
```js
|
|
123
|
-
|
|
168
|
+
import * as ActiveStorage from "@rails/activestorage"
|
|
169
|
+
ActiveStorage.start()
|
|
124
170
|
```
|
|
125
171
|
2. Annotate file inputs with the direct upload URL.
|
|
126
172
|
|
|
@@ -159,4 +205,4 @@ Bug reports for the Ruby on Rails project can be filed here:
|
|
|
159
205
|
|
|
160
206
|
Feature requests should be discussed on the rails-core mailing list here:
|
|
161
207
|
|
|
162
|
-
* https://
|
|
208
|
+
* https://discuss.rubyonrails.org/c/rubyonrails-core
|