Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6c027443
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,体验更适合开发者的 AI 搜索 >>
提交
6c027443
编写于
3月 11, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add tests for lookup context.
上级
073852df
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
203 addition
and
34 deletion
+203
-34
actionpack/lib/action_view/lookup_context.rb
actionpack/lib/action_view/lookup_context.rb
+33
-33
actionpack/test/controller/new_base/render_rjs_test.rb
actionpack/test/controller/new_base/render_rjs_test.rb
+1
-1
actionpack/test/lib/fixture_template.rb
actionpack/test/lib/fixture_template.rb
+2
-0
actionpack/test/template/lookup_context_test.rb
actionpack/test/template/lookup_context_test.rb
+167
-0
未找到文件。
actionpack/lib/action_view/lookup_context.rb
浏览文件 @
6c027443
...
@@ -11,16 +11,26 @@ class LookupContext #:nodoc:
...
@@ -11,16 +11,26 @@ class LookupContext #:nodoc:
@@fallbacks
=
[
FileSystemResolver
.
new
(
""
),
FileSystemResolver
.
new
(
"/"
)]
@@fallbacks
=
[
FileSystemResolver
.
new
(
""
),
FileSystemResolver
.
new
(
"/"
)]
mattr_accessor
:registered_details
mattr_accessor
:registered_details
self
.
registered_details
=
{}
self
.
registered_details
=
[]
def
self
.
register_detail
(
name
,
options
=
{})
def
self
.
register_detail
(
name
,
options
=
{})
registered_details
[
name
]
=
lambda
do
|
value
|
self
.
registered_details
<<
name
Setters
.
send
:define_method
,
:"
#{
name
}
="
do
|
value
|
value
=
Array
(
value
.
presence
||
yield
)
value
=
Array
(
value
.
presence
||
yield
)
value
|=
[
nil
]
unless
options
[
:allow_nil
]
==
false
value
|=
[
nil
]
unless
options
[
:allow_nil
]
==
false
value
unless
value
==
@details
[
name
]
@details_key
,
@details
=
nil
,
@details
.
merge
(
name
=>
value
)
@details
.
freeze
end
end
end
end
end
# Holds raw setters for the registered details.
module
Setters
#:nodoc:
end
register_detail
(
:formats
)
{
Mime
::
SET
.
symbols
}
register_detail
(
:formats
)
{
Mime
::
SET
.
symbols
}
register_detail
(
:locale
)
{
[
I18n
.
locale
]
}
register_detail
(
:locale
)
{
[
I18n
.
locale
]
}
...
@@ -40,7 +50,7 @@ def initialize(details)
...
@@ -40,7 +50,7 @@ def initialize(details)
end
end
def
initialize
(
view_paths
,
details
=
{})
def
initialize
(
view_paths
,
details
=
{})
@details
_key
=
nil
@details
,
@details_key
=
{},
nil
self
.
view_paths
=
view_paths
self
.
view_paths
=
view_paths
self
.
details
=
details
self
.
details
=
details
end
end
...
@@ -55,18 +65,18 @@ def view_paths=(paths)
...
@@ -55,18 +65,18 @@ def view_paths=(paths)
end
end
def
find
(
name
,
prefix
=
nil
,
partial
=
false
)
def
find
(
name
,
prefix
=
nil
,
partial
=
false
)
@view_paths
.
find
(
name
,
prefix
,
partial
||
false
,
details
,
details_key
)
@view_paths
.
find
(
name
,
prefix
,
partial
,
details
,
details_key
)
end
end
def
find_all
(
name
,
prefix
=
nil
,
partial
=
false
)
def
find_all
(
name
,
prefix
=
nil
,
partial
=
false
)
@view_paths
.
find_all
(
name
,
prefix
,
partial
||
false
,
details
,
details_key
)
@view_paths
.
find_all
(
name
,
prefix
,
partial
,
details
,
details_key
)
end
end
def
exists?
(
name
,
prefix
=
nil
,
partial
=
false
)
def
exists?
(
name
,
prefix
=
nil
,
partial
=
false
)
@view_paths
.
exists?
(
name
,
prefix
,
partial
||
false
,
details
,
details_key
)
@view_paths
.
exists?
(
name
,
prefix
,
partial
,
details
,
details_key
)
end
end
# Add fallbacks to the view paths. Useful in cases you are rendering a file.
# Add fallbacks to the view paths. Useful in cases you are rendering a
:
file.
def
with_fallbacks
def
with_fallbacks
added_resolvers
=
0
added_resolvers
=
0
self
.
class
.
fallbacks
.
each
do
|
resolver
|
self
.
class
.
fallbacks
.
each
do
|
resolver
|
...
@@ -83,9 +93,8 @@ def with_fallbacks
...
@@ -83,9 +93,8 @@ def with_fallbacks
module
Details
module
Details
attr_reader
:details
attr_reader
:details
def
details
=
(
details
)
def
details
=
(
given_details
)
@details
=
normalize_details
(
details
)
registered_details
.
each
{
|
key
|
send
(
:"
#{
key
}
="
,
given_details
[
key
])
}
@details_key
=
nil
if
@details_key
&&
@details_key
.
details
!=
@details
end
end
def
details_key
def
details_key
...
@@ -97,9 +106,10 @@ def formats
...
@@ -97,9 +106,10 @@ def formats
@details
[
:formats
].
compact
@details
[
:formats
].
compact
end
end
# Shortcut to set formats in details.
# Overload formats= to reject [:"*/*"] values.
def
formats
=
(
value
)
def
formats
=
(
value
,
freeze
=
true
)
self
.
details
=
@details
.
merge
(
:formats
=>
value
)
value
=
nil
if
value
==
[
:"*/*"
]
super
(
value
)
end
end
# Shortcut to read locale.
# Shortcut to read locale.
...
@@ -107,13 +117,14 @@ def locale
...
@@ -107,13 +117,14 @@ def locale
I18n
.
locale
I18n
.
locale
end
end
# Shortcut to set locale in details and I18n.
# Overload locale= to also set the I18n.locale. If the current I18n.config object responds
# to i18n_config, it means that it's has a copy of the original I18n configuration and it's
# acting as proxy, which we need to skip.
def
locale
=
(
value
)
def
locale
=
(
value
)
I18n
.
locale
=
value
value
=
value
.
first
if
value
.
is_a?
(
Array
)
config
=
I18n
.
config
.
respond_to?
(
:i18n_config
)
?
I18n
.
config
.
i18n_config
:
I18n
.
config
unless
I18n
.
config
.
respond_to?
(
:lookup_context
)
config
.
locale
=
value
if
value
self
.
details
=
@details
.
merge
(
:locale
=>
value
)
super
(
I18n
.
locale
)
end
end
end
# Update the details keys by merging the given hash into the current
# Update the details keys by merging the given hash into the current
...
@@ -127,24 +138,13 @@ def update_details(new_details)
...
@@ -127,24 +138,13 @@ def update_details(new_details)
begin
begin
yield
yield
ensure
ensure
self
.
details
=
old_details
@details
=
old_details
end
end
end
end
protected
def
normalize_details
(
details
)
details
=
details
.
dup
# TODO: Refactor this concern out of the resolver
details
.
delete
(
:formats
)
if
details
[
:formats
]
==
[
:"*/*"
]
self
.
class
.
registered_details
.
each
do
|
k
,
v
|
details
[
k
]
=
v
.
call
(
details
[
k
])
end
end
details
.
freeze
end
end
end
end
include
Setters
include
Details
include
Details
include
ViewPaths
include
ViewPaths
end
end
...
...
actionpack/test/controller/new_base/render_rjs_test.rb
浏览文件 @
6c027443
...
@@ -17,7 +17,7 @@ def index
...
@@ -17,7 +17,7 @@ def index
end
end
def
index_locale
def
index_locale
old_locale
,
I18n
.
locale
=
I18n
.
locale
,
:da
self
.
locale
=
:da
end
end
end
end
...
...
actionpack/test/lib/fixture_template.rb
浏览文件 @
6c027443
module
ActionView
#:nodoc:
module
ActionView
#:nodoc:
class
FixtureResolver
<
PathResolver
class
FixtureResolver
<
PathResolver
attr_reader
:hash
def
initialize
(
hash
=
{})
def
initialize
(
hash
=
{})
super
()
super
()
@hash
=
hash
@hash
=
hash
...
...
actionpack/test/template/lookup_context_test.rb
0 → 100644
浏览文件 @
6c027443
require
"abstract_unit"
require
"abstract_controller/rendering"
ActionView
::
LookupContext
::
DetailsKey
.
class_eval
do
def
self
.
details_keys
@details_keys
end
end
class
LookupContextTest
<
ActiveSupport
::
TestCase
def
setup
@lookup_context
=
ActionView
::
LookupContext
.
new
(
FIXTURE_LOAD_PATH
,
{})
end
def
teardown
I18n
.
locale
=
:en
ActionView
::
LookupContext
::
DetailsKey
.
details_keys
.
clear
end
test
"process view paths on initialization"
do
assert_kind_of
ActionView
::
PathSet
,
@lookup_context
.
view_paths
end
test
"normalizes details on initialization"
do
formats
=
Mime
::
SET
+
[
nil
]
locale
=
[
I18n
.
locale
,
nil
]
assert_equal
Hash
[
:formats
=>
formats
,
:locale
=>
locale
],
@lookup_context
.
details
end
test
"allows me to set details"
do
@lookup_context
.
details
=
{
:formats
=>
[
:html
],
:locale
=>
:pt
}
assert_equal
Hash
[
:formats
=>
[
:html
,
nil
],
:locale
=>
[
:pt
,
nil
]],
@lookup_context
.
details
end
test
"does not allow details to be modified in place"
do
assert_raise
TypeError
do
@lookup_context
.
details
.
clear
end
end
test
"allows me to update an specific detail"
do
@lookup_context
.
update_details
(
:locale
=>
:pt
)
assert_equal
:pt
,
I18n
.
locale
formats
=
Mime
::
SET
+
[
nil
]
locale
=
[
I18n
.
locale
,
nil
]
assert_equal
Hash
[
:formats
=>
formats
,
:locale
=>
locale
],
@lookup_context
.
details
end
test
"allows me to change some details to execute an specific block of code"
do
formats
=
Mime
::
SET
+
[
nil
]
@lookup_context
.
update_details
(
:locale
=>
:pt
)
do
assert_equal
Hash
[
:formats
=>
formats
,
:locale
=>
[
:pt
,
nil
]],
@lookup_context
.
details
end
assert_equal
Hash
[
:formats
=>
formats
,
:locale
=>
[
:en
,
nil
]],
@lookup_context
.
details
end
test
"provides getters and setters for formats"
do
@lookup_context
.
formats
=
:html
assert_equal
[
:html
],
@lookup_context
.
formats
end
test
"handles */* formats"
do
@lookup_context
.
formats
=
[
:"*/*"
]
assert_equal
Mime
::
SET
,
@lookup_context
.
formats
end
test
"provides getters and setters for locale"
do
@lookup_context
.
locale
=
:pt
assert_equal
:pt
,
@lookup_context
.
locale
end
test
"changing lookup_context locale, changes I18n.locale"
do
@lookup_context
.
locale
=
:pt
assert_equal
:pt
,
I18n
.
locale
end
test
"delegates changing the locale to the I18n configuration object if it contains a lookup_context object"
do
begin
I18n
.
config
=
AbstractController
::
I18nProxy
.
new
(
I18n
.
config
,
@lookup_context
)
@lookup_context
.
locale
=
:pt
assert_equal
:pt
,
I18n
.
locale
assert_equal
:pt
,
@lookup_context
.
locale
ensure
I18n
.
config
=
I18n
.
config
.
i18n_config
end
assert_equal
:pt
,
I18n
.
locale
end
test
"find templates using the given view paths and configured details"
do
template
=
@lookup_context
.
find
(
"hello_world"
,
"test"
)
assert_equal
"Hello world!"
,
template
.
source
@lookup_context
.
locale
=
:da
template
=
@lookup_context
.
find
(
"hello_world"
,
"test"
)
assert_equal
"Hey verden"
,
template
.
source
end
test
"adds fallbacks to view paths when required"
do
assert_equal
1
,
@lookup_context
.
view_paths
.
size
@lookup_context
.
with_fallbacks
do
assert_equal
3
,
@lookup_context
.
view_paths
.
size
assert
@lookup_context
.
view_paths
.
include?
(
ActionView
::
FileSystemResolver
.
new
(
""
))
assert
@lookup_context
.
view_paths
.
include?
(
ActionView
::
FileSystemResolver
.
new
(
"/"
))
end
end
test
"add fallbacks just once in nested fallbacks calls"
do
@lookup_context
.
with_fallbacks
do
@lookup_context
.
with_fallbacks
do
assert_equal
3
,
@lookup_context
.
view_paths
.
size
end
end
end
test
"generates a new details key for each details hash"
do
keys
=
[]
keys
<<
@lookup_context
.
details_key
assert_equal
1
,
keys
.
uniq
.
size
@lookup_context
.
locale
=
:da
keys
<<
@lookup_context
.
details_key
assert_equal
2
,
keys
.
uniq
.
size
@lookup_context
.
locale
=
:en
keys
<<
@lookup_context
.
details_key
assert_equal
2
,
keys
.
uniq
.
size
@lookup_context
.
formats
=
:html
keys
<<
@lookup_context
.
details_key
assert_equal
3
,
keys
.
uniq
.
size
@lookup_context
.
formats
=
nil
keys
<<
@lookup_context
.
details_key
assert_equal
3
,
keys
.
uniq
.
size
end
test
"gives the key forward to the resolver, so it can be used as cache key"
do
@lookup_context
.
view_paths
=
ActionView
::
FixtureResolver
.
new
(
"test/_foo.erb"
=>
"Foo"
)
template
=
@lookup_context
.
find
(
"foo"
,
"test"
,
true
)
assert_equal
"Foo"
,
template
.
source
# Now we are going to change the template, but it won't change the returned template
# since we will hit the cache.
@lookup_context
.
view_paths
.
first
.
hash
[
"test/_foo.erb"
]
=
"Bar"
template
=
@lookup_context
.
find
(
"foo"
,
"test"
,
true
)
assert_equal
"Foo"
,
template
.
source
# This time we will change the locale. The updated template should be picked since
# lookup_context generated a new key after we changed the locale.
@lookup_context
.
locale
=
:da
template
=
@lookup_context
.
find
(
"foo"
,
"test"
,
true
)
assert_equal
"Bar"
,
template
.
source
# Now we will change back the locale and it will still pick the old template.
# This is expected because lookup_context will reuse the previous key for :en locale.
@lookup_context
.
locale
=
:en
template
=
@lookup_context
.
find
(
"foo"
,
"test"
,
true
)
assert_equal
"Foo"
,
template
.
source
# Finally, we can expire the cache. And the expected template will be used.
@lookup_context
.
view_paths
.
first
.
clear_cache
template
=
@lookup_context
.
find
(
"foo"
,
"test"
,
true
)
assert_equal
"Bar"
,
template
.
source
end
end
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录