Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
5963766d
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,发现更多精彩内容 >>
未验证
提交
5963766d
编写于
7月 09, 2017
作者:
D
David Heinemeier Hansson
提交者:
Kasper Timm Hansen
7月 23, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Explore regular polymorphic associations rather than record_gid
上级
4efbeaea
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
24 addition
and
17 deletion
+24
-17
app/models/active_storage/attachment.rb
app/models/active_storage/attachment.rb
+1
-9
lib/active_storage/attached/macros.rb
lib/active_storage/attached/macros.rb
+6
-0
lib/active_storage/attached/many.rb
lib/active_storage/attached/many.rb
+2
-2
lib/active_storage/attached/one.rb
lib/active_storage/attached/one.rb
+2
-2
lib/active_storage/migration.rb
lib/active_storage/migration.rb
+5
-4
test/models/attachments_test.rb
test/models/attachments_test.rb
+8
-0
未找到文件。
app/models/active_storage/attachment.rb
浏览文件 @
5963766d
...
...
@@ -6,19 +6,11 @@
class
ActiveStorage::Attachment
<
ActiveRecord
::
Base
self
.
table_name
=
"active_storage_attachments"
belongs_to
:record
,
polymorphic:
true
belongs_to
:blob
,
class_name:
"ActiveStorage::Blob"
delegate_missing_to
:blob
def
record
@record
||=
GlobalID
::
Locator
.
locate
(
record_gid
)
end
def
record
=
(
record
)
@record
=
record
self
.
record_gid
=
record
&
.
to_gid
end
def
purge
blob
.
purge
destroy
...
...
lib/active_storage/attached/macros.rb
浏览文件 @
5963766d
...
...
@@ -16,6 +16,9 @@ def has_one_attached(name, dependent: :purge_later)
instance_variable_set
(
"@active_storage_attached_
#{
name
}
"
,
ActiveStorage
::
Attached
::
One
.
new
(
name
,
self
))
end
has_one
:"
#{
name
}
_attachment"
,
->
{
where
(
name:
name
)
},
class_name:
"ActiveStorage::Attachment"
,
as: :record
has_one
:"
#{
name
}
_blob"
,
through: :"
#{
name
}
_attachment"
if
dependent
==
:purge_later
before_destroy
{
public_send
(
name
).
purge_later
}
end
...
...
@@ -38,6 +41,9 @@ def has_many_attached(name, dependent: :purge_later)
instance_variable_set
(
"@active_storage_attached_
#{
name
}
"
,
ActiveStorage
::
Attached
::
Many
.
new
(
name
,
self
))
end
has_many
:"
#{
name
}
_attachments"
,
->
{
where
(
name:
name
)
},
as: :record
,
class_name:
"ActiveStorage::Attachment"
has_many
:"
#{
name
}
_blobs"
,
through: :"
#{
name
}
_attachments"
if
dependent
==
:purge_later
before_destroy
{
public_send
(
name
).
purge_later
}
end
...
...
lib/active_storage/attached/many.rb
浏览文件 @
5963766d
...
...
@@ -7,14 +7,14 @@ class ActiveStorage::Attached::Many < ActiveStorage::Attached
# You don't have to call this method to access the attachments' methods as
# they are all available at the model level.
def
attachments
@attachments
||=
ActiveStorage
::
Attachment
.
where
(
record_gid:
record
.
to_gid
.
to_s
,
name:
name
)
@attachments
||=
record
.
public_send
(
"
#{
name
}
_attachments"
)
end
# Associates one or several attachments with the current record, saving
# them to the database.
def
attach
(
*
attachables
)
@attachments
=
attachments
|
Array
(
attachables
).
flatten
.
collect
do
|
attachable
|
ActiveStorage
::
Attachment
.
create!
(
record
_gid:
record
.
to_gid
.
to_s
,
name:
name
,
blob:
create_blob_from
(
attachable
))
ActiveStorage
::
Attachment
.
create!
(
record
:
record
,
name:
name
,
blob:
create_blob_from
(
attachable
))
end
end
...
...
lib/active_storage/attached/one.rb
浏览文件 @
5963766d
...
...
@@ -7,13 +7,13 @@ class ActiveStorage::Attached::One < ActiveStorage::Attached
# You don't have to call this method to access the attachment's methods as
# they are all available at the model level.
def
attachment
@attachment
||=
ActiveStorage
::
Attachment
.
find_by
(
record_gid:
record
.
to_gid
.
to_s
,
name:
name
)
@attachment
||=
record
.
public_send
(
"
#{
name
}
_attachment"
)
end
# Associates a given attachment with the current record, saving it to the
# database.
def
attach
(
attachable
)
@attachment
=
ActiveStorage
::
Attachment
.
create!
(
record
_gid:
record
.
to_gid
.
to_s
,
name:
name
,
blob:
create_blob_from
(
attachable
))
@attachment
=
ActiveStorage
::
Attachment
.
create!
(
record
:
record
,
name:
name
,
blob:
create_blob_from
(
attachable
))
end
# Checks the presence of the attachment.
...
...
lib/active_storage/migration.rb
浏览文件 @
5963766d
...
...
@@ -14,15 +14,16 @@ def change
create_table
:active_storage_attachments
do
|
t
|
t
.
string
:name
t
.
string
:record_gid
t
.
string
:record_type
t
.
integer
:record_id
t
.
integer
:blob_id
t
.
datetime
:created_at
t
.
index
:record_gid
t
.
index
:blob_id
t
.
index
[
:record_gid
,
:name
]
t
.
index
[
:record_gid
,
:blob_id
],
unique:
true
t
.
index
[
:record_type
,
:record_id
]
t
.
index
[
:record_type
,
:record_id
,
:name
],
name:
"index_active_storage_attachments_record_and_name"
t
.
index
[
:record_type
,
:record_id
,
:blob_id
],
name:
"index_active_storage_attachments_uniqueness"
,
unique:
true
end
end
end
test/models/attachments_test.rb
浏览文件 @
5963766d
...
...
@@ -68,6 +68,14 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
assert_equal
"country.jpg"
,
@user
.
highlights
.
second
.
filename
.
to_s
end
test
"find attached blobs"
do
@user
.
highlights
.
attach
(
{
io:
StringIO
.
new
(
"STUFF"
),
filename:
"town.jpg"
,
content_type:
"image/jpg"
},
{
io:
StringIO
.
new
(
"IT"
),
filename:
"country.jpg"
,
content_type:
"image/jpg"
})
User
.
where
(
id:
@user
.
id
).
includes
(
highlights_attachments: :blob
).
first
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录