Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
d93b45e8
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,发现更多精彩内容 >>
提交
d93b45e8
编写于
5月 17, 2010
作者:
J
Jeff Kreeftmeijer
提交者:
José Valim
6月 10, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rails::Generators::GeneratedAttribute: tests, cleanups and a bugfix [#4631 state:resolved]
Signed-off-by:
N
José Valim
<
jose.valim@gmail.com
>
上级
566967ea
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
105 addition
and
19 deletion
+105
-19
railties/lib/rails/generators/generated_attribute.rb
railties/lib/rails/generators/generated_attribute.rb
+2
-1
railties/lib/rails/generators/test_case.rb
railties/lib/rails/generators/test_case.rb
+22
-8
railties/test/generators/generated_attribute_test.rb
railties/test/generators/generated_attribute_test.rb
+81
-10
未找到文件。
railties/lib/rails/generators/generated_attribute.rb
浏览文件 @
d93b45e8
require
'active_support/time'
module
Rails
module
Generators
class
GeneratedAttribute
...
...
@@ -13,7 +15,6 @@ def field_type
when
:time
then
:time_select
when
:datetime
,
:timestamp
then
:datetime_select
when
:date
then
:date_select
when
:string
then
:text_field
when
:text
then
:text_area
when
:boolean
then
:check_box
else
...
...
railties/lib/rails/generators/test_case.rb
浏览文件 @
d93b45e8
...
...
@@ -189,18 +189,23 @@ def assert_instance_method(method, content)
end
alias
:assert_method
:assert_instance_method
# Asserts the given
field name gets translated to an attribute type
# properly
.
# Asserts the given
attribute type gets translated to a field type
# properly
:
#
# assert_field_type :date, :date_select
#
def
assert_field_type
(
name
,
attribute_type
)
assert_equal
(
Rails
::
Generators
::
GeneratedAttribute
.
new
(
'test'
,
name
.
to_s
).
field_type
,
attribute_type
)
def
assert_field_type
(
attribute_type
,
field_type
)
assert_equal
(
field_type
,
create_generated_attribute
(
attribute_type
).
field_type
)
end
# Asserts the given attribute type gets a proper default value:
#
# assert_field_type :string, "MyString"
#
def
assert_field_default_value
(
attribute_type
,
value
)
assert_equal
(
value
,
create_generated_attribute
(
attribute_type
).
default
)
end
# Runs the generator configured for this class. The first argument is an array like
# command line arguments:
#
...
...
@@ -226,6 +231,15 @@ def generator(args=self.default_arguments, options={}, config={})
@generator
||=
self
.
generator_class
.
new
(
args
,
options
,
config
.
reverse_merge
(
:destination_root
=>
destination_root
))
end
# Create a Rails::Generators::GeneratedAttribute by supplying the
# attribute type and, optionally, the attribute name:
#
# create_generated_attribute(:string, 'name')
#
def
create_generated_attribute
(
attribute_type
,
name
=
'test'
)
Rails
::
Generators
::
GeneratedAttribute
.
new
(
name
,
attribute_type
.
to_s
)
end
protected
def
destination_root_is_set?
#:nodoc:
...
...
railties/test/generators/generated_attribute_test.rb
浏览文件 @
d93b45e8
...
...
@@ -5,36 +5,107 @@ class GeneratedAttributeTest < Rails::Generators::TestCase
include
GeneratorsTestHelper
def
test_field_type_returns_text_field
%w(integer float decimal string)
.
each
do
|
nam
e
|
assert_field_type
nam
e
,
:text_field
%w(integer float decimal string)
.
each
do
|
attribute_typ
e
|
assert_field_type
attribute_typ
e
,
:text_field
end
end
def
test_field_type_returns_datetime_select
%w(datetime timestamp)
.
each
do
|
nam
e
|
assert_field_type
nam
e
,
:datetime_select
%w(datetime timestamp)
.
each
do
|
attribute_typ
e
|
assert_field_type
attribute_typ
e
,
:datetime_select
end
end
def
test_field_type_returns_time_select
assert_field_type
'time'
,
:time_select
assert_field_type
:time
,
:time_select
end
def
test_field_type_returns_date_select
assert_field_type
'date'
,
:date_select
assert_field_type
:date
,
:date_select
end
def
test_field_type_returns_text_area
assert_field_type
'text'
,
:text_area
assert_field_type
:text
,
:text_area
end
def
test_field_type_returns_check_box
assert_field_type
'boolean'
,
:check_box
assert_field_type
:boolean
,
:check_box
end
def
test_field_type_with_unknown_type_returns_text_field
%w(foo bar baz)
.
each
do
|
name
|
assert_field_type
name
,
:text_field
%w(foo bar baz)
.
each
do
|
attribute_type
|
assert_field_type
attribute_type
,
:text_field
end
end
def
test_default_value_is_integer
assert_field_default_value
:integer
,
1
end
def
test_default_value_is_float
assert_field_default_value
:float
,
1.5
end
def
test_default_value_is_decimal
assert_field_default_value
:decimal
,
'9.99'
end
def
test_default_value_is_datetime
%w(datetime timestamp time)
.
each
do
|
attribute_type
|
assert_field_default_value
attribute_type
,
Time
.
now
.
to_s
(
:db
)
end
end
def
test_default_value_is_date
assert_field_default_value
:date
,
Date
.
today
.
to_s
(
:db
)
end
def
test_default_value_is_string
assert_field_default_value
:string
,
'MyString'
end
def
test_default_value_is_text
assert_field_default_value
:text
,
'MyText'
end
def
test_default_value_is_boolean
assert_field_default_value
:boolean
,
false
end
def
test_default_value_is_nil
%w(references belongs_to)
.
each
do
|
attribute_type
|
assert_field_default_value
attribute_type
,
nil
end
end
def
test_default_value_is_empty_string
%w(foo bar baz)
.
each
do
|
attribute_type
|
assert_field_default_value
attribute_type
,
''
end
end
def
test_human_name
assert_equal
(
'Full name'
,
create_generated_attribute
(
:string
,
'full_name'
).
human_name
)
end
def
test_reference_is_true
%w(references belongs_to)
.
each
do
|
attribute_type
|
assert_equal
(
true
,
create_generated_attribute
(
attribute_type
).
reference?
)
end
end
def
test_reference_is_false
%w(foo bar baz)
.
each
do
|
attribute_type
|
assert_equal
(
false
,
create_generated_attribute
(
attribute_type
).
reference?
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录