Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
499a4164
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
499a4164
编写于
11月 14, 2017
作者:
G
George Claghorn
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce ActiveStorage::Attached::{One,Many}#detach
上级
82022bd2
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
40 addition
and
7 deletion
+40
-7
activestorage/lib/active_storage/attached/many.rb
activestorage/lib/active_storage/attached/many.rb
+5
-1
activestorage/lib/active_storage/attached/one.rb
activestorage/lib/active_storage/attached/one.rb
+9
-6
activestorage/test/models/attachments_test.rb
activestorage/test/models/attachments_test.rb
+26
-0
未找到文件。
activestorage/lib/active_storage/attached/many.rb
浏览文件 @
499a4164
...
...
@@ -13,7 +13,6 @@ def attachments
end
# Associates one or several attachments with the current record, saving them to the database.
# Examples:
#
# 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
...
...
@@ -36,6 +35,11 @@ def attached?
attachments
.
any?
end
# Deletes associated attachments without purging them, leaving their respective blobs in place.
def
detach
attachments
.
destroy_all
if
attached?
end
# Directly purges each associated attachment (i.e. destroys the blobs and
# attachments and deletes the files on the service).
def
purge
...
...
activestorage/lib/active_storage/attached/one.rb
浏览文件 @
499a4164
...
...
@@ -14,7 +14,6 @@ def attachment
end
# Associates a given attachment with the current record, saving it to the database.
# Examples:
#
# person.avatar.attach(params[:avatar]) # ActionDispatch::Http::UploadedFile object
# person.avatar.attach(params[:signed_blob_id]) # Signed reference to blob from direct upload
...
...
@@ -39,6 +38,14 @@ def attached?
attachment
.
present?
end
# Deletes the attachment without purging it, leaving its blob in place.
def
detach
if
attached?
attachment
.
destroy
write_attachment
nil
end
end
# Directly purges the attachment (i.e. destroys the blob and
# attachment and deletes the file on the service).
def
purge
...
...
@@ -59,16 +66,12 @@ def purge_later
def
replace
(
attachable
)
blob
.
tap
do
transaction
do
de
stroy_attachment
de
tach
write_attachment
create_attachment_from
(
attachable
)
end
end
.
purge_later
end
def
destroy_attachment
attachment
.
destroy
end
def
create_attachment_from
(
attachable
)
ActiveStorage
::
Attachment
.
create!
(
record:
record
,
name:
name
,
blob:
create_blob_from
(
attachable
))
end
...
...
activestorage/test/models/attachments_test.rb
浏览文件 @
499a4164
...
...
@@ -98,6 +98,17 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
assert_equal
"bar"
,
blob
.
reload
.
metadata
[
:foo
]
end
test
"detach blob"
do
@user
.
avatar
.
attach
create_blob
(
filename:
"funky.jpg"
)
avatar_blob_id
=
@user
.
avatar
.
blob
.
id
avatar_key
=
@user
.
avatar
.
key
@user
.
avatar
.
detach
assert_not
@user
.
avatar
.
attached?
assert
ActiveStorage
::
Blob
.
exists?
(
avatar_blob_id
)
assert
ActiveStorage
::
Blob
.
service
.
exist?
(
avatar_key
)
end
test
"purge attached blob"
do
@user
.
avatar
.
attach
create_blob
(
filename:
"funky.jpg"
)
avatar_key
=
@user
.
avatar
.
key
...
...
@@ -218,6 +229,21 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
end
end
test
"detach blobs"
do
@user
.
highlights
.
attach
create_blob
(
filename:
"funky.jpg"
),
create_blob
(
filename:
"wonky.jpg"
)
highlight_blob_ids
=
@user
.
highlights
.
collect
{
|
highlight
|
highlight
.
blob
.
id
}
highlight_keys
=
@user
.
highlights
.
collect
(
&
:key
)
@user
.
highlights
.
detach
assert_not
@user
.
highlights
.
attached?
assert
ActiveStorage
::
Blob
.
exists?
(
highlight_blob_ids
.
first
)
assert
ActiveStorage
::
Blob
.
exists?
(
highlight_blob_ids
.
second
)
assert
ActiveStorage
::
Blob
.
service
.
exist?
(
highlight_keys
.
first
)
assert
ActiveStorage
::
Blob
.
service
.
exist?
(
highlight_keys
.
second
)
end
test
"purge attached blobs"
do
@user
.
highlights
.
attach
create_blob
(
filename:
"funky.jpg"
),
create_blob
(
filename:
"wonky.jpg"
)
highlight_keys
=
@user
.
highlights
.
collect
(
&
:key
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录