Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
bd38d9f2
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,发现更多精彩内容 >>
提交
bd38d9f2
编写于
10月 12, 2012
作者:
J
Joshua Peek
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add asset_path and asset_url helpers
上级
b62f51d5
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
72 addition
and
17 deletion
+72
-17
actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
.../lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
+2
-1
actionpack/lib/action_view/helpers/asset_url_helper.rb
actionpack/lib/action_view/helpers/asset_url_helper.rb
+52
-16
actionpack/test/template/asset_tag_helper_test.rb
actionpack/test/template/asset_tag_helper_test.rb
+18
-0
未找到文件。
actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
浏览文件 @
bd38d9f2
...
...
@@ -43,7 +43,8 @@ def rewrite_extension(source, dir, ext)
# Break out the asset path rewrite in case plugins wish to put the asset id
# someplace other than the query string.
def
rewrite_asset_path
(
source
,
dir
,
options
=
nil
)
source
=
"/
#{
dir
}
/
#{
source
}
"
unless
source
[
0
]
==
?/
dir
=
"/
#{
dir
}
"
if
dir
&&
dir
[
0
]
!=
?/
source
=
File
.
join
(
dir
.
to_s
,
source
)
unless
source
[
0
]
==
?/
path
=
config
.
asset_path
if
path
&&
path
.
respond_to?
(
:call
)
...
...
actionpack/lib/action_view/helpers/asset_url_helper.rb
浏览文件 @
bd38d9f2
...
...
@@ -190,6 +190,46 @@ module Helpers #:nodoc:
# RewriteRule ^/release-\d+/(images|javascripts|stylesheets)/(.*)$ /$1/$2 [L]
#
module
AssetUrlHelper
ASSET_EXTENSIONS
=
{
javascript:
'js'
,
stylesheet:
'css'
}
ASSET_PUBLIC_DIRECTORIES
=
{
audio:
'/audios'
,
font:
'/fonts'
,
image:
'/images'
,
javascript:
'/javascripts'
,
stylesheet:
'/stylesheets'
,
video:
'/videos'
}
# Computes the path to asset in public directory. If :type
# options is set, a file extension will be appended and scoped
# to the corresponding public directory.
#
# All other asset *_path helpers delegate through this method.
#
# asset_path "application.js" # => /application.js
# asset_path "application", type: :javascript # => /javascripts/application.js
# asset_path "application", type: :stylesheet # => /stylesheets/application.css
# asset_path "http://www.example.com/js/xmlhr.js" # => http://www.example.com/js/xmlhr.js
def
asset_path
(
source
,
options
=
{})
return
""
unless
source
.
present?
options
[
:ext
]
||=
ASSET_EXTENSIONS
[
options
[
:type
]]
if
options
[
:type
]
asset_paths
.
compute_public_path
(
source
,
ASSET_PUBLIC_DIRECTORIES
[
options
[
:type
]],
options
)
end
alias_method
:path_to_asset
,
:asset_path
# aliased to avoid conflicts with a asset_path named route
# Computes the full URL to a asset in the public directory. This
# will use +asset_path+ internally, so most of their behaviors
# will be the same.
def
asset_url
(
source
,
options
=
{})
host
=
url_for
(
:only_path
=>
false
)
URI
.
join
(
host
,
path_to_asset
(
source
,
options
)).
to_s
end
alias_method
:url_to_asset
,
:asset_url
# aliased to avoid conflicts with an asset_url named route
# Computes the path to a javascript asset in the public javascripts directory.
# If the +source+ filename has no extension, .js will be appended (except for explicit URIs)
# Full paths from the document root will be passed through.
...
...
@@ -201,14 +241,14 @@ module AssetUrlHelper
# javascript_path "http://www.example.com/js/xmlhr" # => http://www.example.com/js/xmlhr
# javascript_path "http://www.example.com/js/xmlhr.js" # => http://www.example.com/js/xmlhr.js
def
javascript_path
(
source
)
asset_paths
.
compute_public_path
(
source
,
'javascripts'
,
:ext
=>
'js'
)
path_to_asset
(
source
,
type: :javascript
)
end
alias_method
:path_to_javascript
,
:javascript_path
# aliased to avoid conflicts with a javascript_path named route
# Computes the full URL to a javascript asset in the public javascripts directory.
# This will use +javascript_path+ internally, so most of their behaviors will be the same.
def
javascript_url
(
source
)
URI
.
join
(
current_host
,
path_to_javascript
(
source
)).
to_s
url_to_asset
(
source
,
type: :javascript
)
end
alias_method
:url_to_javascript
,
:javascript_url
# aliased to avoid conflicts with a javascript_url named route
...
...
@@ -223,14 +263,14 @@ def javascript_url(source)
# stylesheet_path "http://www.example.com/css/style" # => http://www.example.com/css/style
# stylesheet_path "http://www.example.com/css/style.css" # => http://www.example.com/css/style.css
def
stylesheet_path
(
source
)
asset_paths
.
compute_public_path
(
source
,
'stylesheets'
,
:ext
=>
'css'
,
:protocol
=>
:reques
t
)
path_to_asset
(
source
,
type: :styleshee
t
)
end
alias_method
:path_to_stylesheet
,
:stylesheet_path
# aliased to avoid conflicts with a stylesheet_path named route
# Computes the full URL to a stylesheet asset in the public stylesheets directory.
# This will use +stylesheet_path+ internally, so most of their behaviors will be the same.
def
stylesheet_url
(
source
)
URI
.
join
(
current_host
,
path_to_stylesheet
(
source
)).
to_s
url_to_asset
(
source
,
type: :stylesheet
)
end
alias_method
:url_to_stylesheet
,
:stylesheet_url
# aliased to avoid conflicts with a stylesheet_url named route
...
...
@@ -248,14 +288,14 @@ def stylesheet_url(source)
# The alias +path_to_image+ is provided to avoid that. Rails uses the alias internally, and
# plugin authors are encouraged to do so.
def
image_path
(
source
)
source
.
present?
?
asset_paths
.
compute_public_path
(
source
,
'images'
)
:
""
path_to_asset
(
source
,
type: :image
)
end
alias_method
:path_to_image
,
:image_path
# aliased to avoid conflicts with an image_path named route
# Computes the full URL to an image asset.
# This will use +image_path+ internally, so most of their behaviors will be the same.
def
image_url
(
source
)
URI
.
join
(
current_host
,
path_to_image
(
source
)).
to_s
url_to_asset
(
source
,
type: :image
)
end
alias_method
:url_to_image
,
:image_url
# aliased to avoid conflicts with an image_url named route
...
...
@@ -269,14 +309,14 @@ def image_url(source)
# video_path("/trailers/hd.avi") # => /trailers/hd.avi
# video_path("http://www.example.com/vid/hd.avi") # => http://www.example.com/vid/hd.avi
def
video_path
(
source
)
asset_paths
.
compute_public_path
(
source
,
'videos'
)
path_to_asset
(
source
,
type: :video
)
end
alias_method
:path_to_video
,
:video_path
# aliased to avoid conflicts with a video_path named route
# Computes the full URL to a video asset in the public videos directory.
# This will use +video_path+ internally, so most of their behaviors will be the same.
def
video_url
(
source
)
URI
.
join
(
current_host
,
path_to_video
(
source
)).
to_s
url_to_asset
(
source
,
type: :video
)
end
alias_method
:url_to_video
,
:video_url
# aliased to avoid conflicts with an video_url named route
...
...
@@ -290,14 +330,14 @@ def video_url(source)
# audio_path("/sounds/horse.wav") # => /sounds/horse.wav
# audio_path("http://www.example.com/sounds/horse.wav") # => http://www.example.com/sounds/horse.wav
def
audio_path
(
source
)
asset_paths
.
compute_public_path
(
source
,
'audios'
)
path_to_asset
(
source
,
type: :audio
)
end
alias_method
:path_to_audio
,
:audio_path
# aliased to avoid conflicts with an audio_path named route
# Computes the full URL to an audio asset in the public audios directory.
# This will use +audio_path+ internally, so most of their behaviors will be the same.
def
audio_url
(
source
)
URI
.
join
(
current_host
,
path_to_audio
(
source
)).
to_s
url_to_asset
(
source
,
type: :audio
)
end
alias_method
:url_to_audio
,
:audio_url
# aliased to avoid conflicts with an audio_url named route
...
...
@@ -310,14 +350,14 @@ def audio_url(source)
# font_path("/dir/font.ttf") # => /dir/font.ttf
# font_path("http://www.example.com/dir/font.ttf") # => http://www.example.com/dir/font.ttf
def
font_path
(
source
)
asset_paths
.
compute_public_path
(
source
,
'fonts'
)
path_to_asset
(
source
,
type: :font
)
end
alias_method
:path_to_font
,
:font_path
# aliased to avoid conflicts with an font_path named route
# Computes the full URL to a font asset.
# This will use +font_path+ internally, so most of their behaviors will be the same.
def
font_url
(
source
)
URI
.
join
(
current_host
,
path_to_font
(
source
)).
to_s
url_to_asset
(
source
,
type: :font
)
end
alias_method
:url_to_font
,
:font_url
# aliased to avoid conflicts with an font_url named route
...
...
@@ -325,10 +365,6 @@ def font_url(source)
def
asset_paths
@asset_paths
||=
AssetTagHelper
::
AssetPaths
.
new
(
config
,
controller
)
end
def
current_host
url_for
(
:only_path
=>
false
)
end
end
end
end
actionpack/test/template/asset_tag_helper_test.rb
浏览文件 @
bd38d9f2
...
...
@@ -55,6 +55,19 @@ def teardown
ENV
.
delete
(
'RAILS_ASSET_ID'
)
end
AssetPathToTag
=
{
%(asset_path("foo"))
=>
%(/foo)
,
%(asset_path("style.css"))
=>
%(/style.css)
,
%(asset_path("xmlhr.js"))
=>
%(/xmlhr.js)
,
%(asset_path("xml.png"))
=>
%(/xml.png)
,
%(asset_path("dir/xml.png"))
=>
%(/dir/xml.png)
,
%(asset_path("/dir/xml.png"))
=>
%(/dir/xml.png)
,
%(asset_path("style", type: :stylesheet))
=>
%(/stylesheets/style.css)
,
%(asset_path("xmlhr", type: :javascript))
=>
%(/javascripts/xmlhr.js)
,
%(asset_path("xml.png", type: :image))
=>
%(/images/xml.png)
}
AutoDiscoveryToTag
=
{
%(auto_discovery_link_tag)
=>
%(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />)
,
%(auto_discovery_link_tag(:rss))
=>
%(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />)
,
...
...
@@ -293,6 +306,11 @@ def test_autodiscovery_link_tag_deprecated_types
assert_equal
expected
,
result
end
def
test_asset_path_tag
ENV
[
"RAILS_ASSET_ID"
]
=
""
AssetPathToTag
.
each
{
|
method
,
tag
|
assert_dom_equal
(
tag
,
eval
(
method
))
}
end
def
test_auto_discovery_link_tag
AutoDiscoveryToTag
.
each
{
|
method
,
tag
|
assert_dom_equal
(
tag
,
eval
(
method
))
}
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录