File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed
lib/active_storage/attached/changes Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ * Allow attaching File and Pathname when assigning attributes, e.g.
2
+
3
+ ``` ruby
4
+ User .create!(avatar: File .open (" image.jpg" ))
5
+ User .create!(avatar: file_fixture(" image.jpg" ))
6
+ ```
7
+
8
+ * Dorian Mari é*
9
+
1
10
# # Rails 7.1.0.beta1 (September 13, 2023) ##
2
11
3
12
* Disables the session in ` ActiveStorage::Blobs::ProxyController`
Original file line number Diff line number Diff line change @@ -82,6 +82,20 @@ def find_or_build_blob
82
82
)
83
83
when String
84
84
ActiveStorage ::Blob . find_signed! ( attachable , record : record )
85
+ when File
86
+ ActiveStorage ::Blob . build_after_unfurling (
87
+ io : attachable ,
88
+ filename : File . basename ( attachable ) ,
89
+ record : record ,
90
+ service_name : attachment_service_name
91
+ )
92
+ when Pathname
93
+ ActiveStorage ::Blob . build_after_unfurling (
94
+ io : attachable . open ,
95
+ filename : File . basename ( attachable ) ,
96
+ record : record ,
97
+ service_name : attachment_service_name
98
+ )
85
99
else
86
100
raise ArgumentError , "Could not find or build blob: expected attachable, got #{ attachable . inspect } "
87
101
end
Original file line number Diff line number Diff line change @@ -16,6 +16,22 @@ class ActiveStorage::OneAttachedTest < ActiveSupport::TestCase
16
16
ActiveStorage ::Blob . all . each ( &:delete )
17
17
end
18
18
19
+ test "creating a record with a File as attachable attribute" do
20
+ @user = User . create! ( name : "Dorian" , avatar : file_fixture ( "image.gif" ) . open )
21
+
22
+ assert_equal "image.gif" , @user . avatar . filename . to_s
23
+ assert_not_nil @user . avatar_attachment
24
+ assert_not_nil @user . avatar_blob
25
+ end
26
+
27
+ test "creating a record with a Pathname as attachable attribute" do
28
+ @user = User . create! ( name : "Dorian" , avatar : file_fixture ( "image.gif" ) )
29
+
30
+ assert_equal "image.gif" , @user . avatar . filename . to_s
31
+ assert_not_nil @user . avatar_attachment
32
+ assert_not_nil @user . avatar_blob
33
+ end
34
+
19
35
test "attaching an existing blob to an existing record" do
20
36
@user . avatar . attach create_blob ( filename : "funky.jpg" )
21
37
assert_equal "funky.jpg" , @user . avatar . filename . to_s
You can’t perform that action at this time.
0 commit comments