Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
445c682a
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,发现更多精彩内容 >>
提交
445c682a
编写于
10月 12, 2017
作者:
G
George Claghorn
提交者:
GitHub
10月 12, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce ActiveStorage::Blob#representation
上级
8bcb4fab
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
73 addition
and
0 deletion
+73
-0
activestorage/app/models/active_storage/blob.rb
activestorage/app/models/active_storage/blob.rb
+26
-0
activestorage/app/models/active_storage/variant.rb
activestorage/app/models/active_storage/variant.rb
+4
-0
activestorage/test/models/preview_test.rb
activestorage/test/models/preview_test.rb
+2
-0
activestorage/test/models/representation_test.rb
activestorage/test/models/representation_test.rb
+41
-0
未找到文件。
activestorage/app/models/active_storage/blob.rb
浏览文件 @
445c682a
...
...
@@ -15,6 +15,7 @@
# If you need to create a derivative or otherwise change the blob, simply create a new blob and purge the old one.
class
ActiveStorage::Blob
<
ActiveRecord
::
Base
class
UnpreviewableError
<
StandardError
;
end
class
UnrepresentableError
<
StandardError
;
end
self
.
table_name
=
"active_storage_blobs"
...
...
@@ -155,6 +156,31 @@ def previewable?
end
# Returns an ActiveStorage::Preview instance for a previewable blob or an ActiveStorage::Variant instance for an image blob.
#
# blob.representation(resize: "100x100").processed.service_url
#
# Raises ActiveStorage::Blob::UnrepresentableError if the receiving blob is neither an image nor previewable. Call
# ActiveStorage::Blob#representable? to determine whether a blob is representable.
#
# See ActiveStorage::Blob#preview and ActiveStorage::Blob#variant for more information.
def
representation
(
transformations
)
case
when
previewable?
preview
transformations
when
image?
variant
transformations
else
raise
UnrepresentableError
end
end
# Returns true if the blob is an image or is previewable.
def
representable?
image?
||
previewable?
end
# Returns the URL of the blob on the service. This URL is intended to be short-lived for security and not used directly
# with users. Instead, the +service_url+ should only be exposed as a redirect from a stable, possibly authenticated URL.
# Hiding the +service_url+ behind a redirect also gives you the power to change services without updating all URLs. And
...
...
activestorage/app/models/active_storage/variant.rb
浏览文件 @
445c682a
...
...
@@ -65,6 +65,10 @@ def service_url(expires_in: service.url_expires_in, disposition: :inline)
service
.
url
key
,
expires_in:
expires_in
,
disposition:
disposition
,
filename:
blob
.
filename
,
content_type:
blob
.
content_type
end
# Returns the receiving variant. Allows ActiveStorage::Variant and ActiveStorage::Preview instances to be duck-typed.
def
image
self
end
private
def
processed?
...
...
activestorage/test/models/preview_test.rb
浏览文件 @
445c682a
...
...
@@ -7,6 +7,7 @@ class ActiveStorage::PreviewTest < ActiveSupport::TestCase
test
"previewing a PDF"
do
blob
=
create_file_blob
(
filename:
"report.pdf"
,
content_type:
"application/pdf"
)
preview
=
blob
.
preview
(
resize:
"640x280"
).
processed
assert
preview
.
image
.
attached?
assert_equal
"report.png"
,
preview
.
image
.
filename
.
to_s
assert_equal
"image/png"
,
preview
.
image
.
content_type
...
...
@@ -19,6 +20,7 @@ class ActiveStorage::PreviewTest < ActiveSupport::TestCase
test
"previewing an MP4 video"
do
blob
=
create_file_blob
(
filename:
"video.mp4"
,
content_type:
"video/mp4"
)
preview
=
blob
.
preview
(
resize:
"640x280"
).
processed
assert
preview
.
image
.
attached?
assert_equal
"video.png"
,
preview
.
image
.
filename
.
to_s
assert_equal
"image/png"
,
preview
.
image
.
content_type
...
...
activestorage/test/models/representation_test.rb
0 → 100644
浏览文件 @
445c682a
# frozen_string_literal: true
require
"test_helper"
require
"database/setup"
class
ActiveStorage::RepresentationTest
<
ActiveSupport
::
TestCase
test
"representing an image"
do
blob
=
create_file_blob
representation
=
blob
.
representation
(
resize:
"100x100"
).
processed
image
=
read_image
(
representation
.
image
)
assert_equal
100
,
image
.
width
assert_equal
67
,
image
.
height
end
test
"representing a PDF"
do
blob
=
create_file_blob
(
filename:
"report.pdf"
,
content_type:
"application/pdf"
)
representation
=
blob
.
representation
(
resize:
"640x280"
).
processed
image
=
read_image
(
representation
.
image
)
assert_equal
612
,
image
.
width
assert_equal
792
,
image
.
height
end
test
"representing an MP4 video"
do
blob
=
create_file_blob
(
filename:
"video.mp4"
,
content_type:
"video/mp4"
)
representation
=
blob
.
representation
(
resize:
"640x280"
).
processed
image
=
read_image
(
representation
.
image
)
assert_equal
640
,
image
.
width
assert_equal
480
,
image
.
height
end
test
"representing an unrepresentable blob"
do
blob
=
create_blob
assert_raises
ActiveStorage
::
Blob
::
UnrepresentableError
do
blob
.
representation
resize:
"100x100"
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录