Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
520fe7de
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 搜索 >>
提交
520fe7de
编写于
1月 16, 2012
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Extract TextFieldTag
上级
e749fb58
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
117 addition
and
2 deletion
+117
-2
actionpack/lib/action_view/helpers/form_helper.rb
actionpack/lib/action_view/helpers/form_helper.rb
+1
-1
actionpack/lib/action_view/helpers/tags.rb
actionpack/lib/action_view/helpers/tags.rb
+2
-1
actionpack/lib/action_view/helpers/tags/text_field_tag.rb
actionpack/lib/action_view/helpers/tags/text_field_tag.rb
+114
-0
未找到文件。
actionpack/lib/action_view/helpers/form_helper.rb
浏览文件 @
520fe7de
...
...
@@ -677,7 +677,7 @@ def label(object_name, method, content_or_options = nil, options = nil, &block)
# # => <input type="text" id="snippet_code" name="snippet[code]" size="20" value="#{@snippet.code}" class="code_input" />
#
def
text_field
(
object_name
,
method
,
options
=
{})
InstanceTag
.
new
(
object_name
,
method
,
self
,
options
.
delete
(
:object
)).
to_input_field_tag
(
"text"
,
options
)
ActionView
::
Helpers
::
Tags
::
TextFieldTag
.
new
(
object_name
,
method
,
self
,
options
).
render
end
# Returns an input tag of the "password" type tailored for accessing a specified attribute (identified by +method+) on an object
...
...
actionpack/lib/action_view/helpers/tags.rb
浏览文件 @
520fe7de
module
ActionView
module
Helpers
module
Tags
autoload
:LabelTag
,
'action_view/helpers/tags/label_tag'
autoload
:LabelTag
,
'action_view/helpers/tags/label_tag'
autoload
:TextFieldTag
,
'action_view/helpers/tags/text_field_tag'
end
end
end
actionpack/lib/action_view/helpers/tags/text_field_tag.rb
0 → 100644
浏览文件 @
520fe7de
module
ActionView
module
Helpers
module
Tags
class
TextFieldTag
include
Helpers
::
ActiveModelInstanceTag
,
Helpers
::
TagHelper
,
Helpers
::
FormTagHelper
DEFAULT_FIELD_OPTIONS
=
{
"size"
=>
30
}
attr_reader
:object
def
initialize
(
object_name
,
method_name
,
template_object
,
options
=
{})
@object_name
,
@method_name
=
object_name
.
to_s
.
dup
,
method_name
.
to_s
.
dup
@template_object
=
template_object
@object_name
.
sub!
(
/\[\]$/
,
""
)
||
@object_name
.
sub!
(
/\[\]\]$/
,
"]"
)
@object
=
retrieve_object
(
options
.
delete
(
:object
))
@options
=
options
@auto_index
=
retrieve_autoindex
(
Regexp
.
last_match
.
pre_match
)
if
Regexp
.
last_match
end
def
render
(
&
block
)
options
=
@options
.
stringify_keys
options
[
"size"
]
=
options
[
"maxlength"
]
||
DEFAULT_FIELD_OPTIONS
[
"size"
]
unless
options
.
key?
(
"size"
)
options
=
DEFAULT_FIELD_OPTIONS
.
merge
(
options
)
options
[
"type"
]
||=
"text"
options
[
"value"
]
=
options
.
fetch
(
"value"
){
value_before_type_cast
(
object
)
}
options
[
"value"
]
&&=
ERB
::
Util
.
html_escape
(
options
[
"value"
])
add_default_name_and_id
(
options
)
tag
(
"input"
,
options
)
end
private
def
value_before_type_cast
(
object
)
unless
object
.
nil?
object
.
respond_to?
(
@method_name
+
"_before_type_cast"
)
?
object
.
send
(
@method_name
+
"_before_type_cast"
)
:
object
.
send
(
@method_name
)
end
end
def
retrieve_object
(
object
)
if
object
object
elsif
@template_object
.
instance_variable_defined?
(
"@
#{
@object_name
}
"
)
@template_object
.
instance_variable_get
(
"@
#{
@object_name
}
"
)
end
rescue
NameError
# As @object_name may contain the nested syntax (item[subobject]) we need to fallback to nil.
nil
end
def
retrieve_autoindex
(
pre_match
)
object
=
self
.
object
||
@template_object
.
instance_variable_get
(
"@
#{
pre_match
}
"
)
if
object
&&
object
.
respond_to?
(
:to_param
)
object
.
to_param
else
raise
ArgumentError
,
"object[] naming but object param and @object var don't exist or don't respond to to_param:
#{
object
.
inspect
}
"
end
end
def
add_default_name_and_id_for_value
(
tag_value
,
options
)
unless
tag_value
.
nil?
pretty_tag_value
=
tag_value
.
to_s
.
gsub
(
/\s/
,
"_"
).
gsub
(
/[^-\w]/
,
""
).
downcase
specified_id
=
options
[
"id"
]
add_default_name_and_id
(
options
)
options
[
"id"
]
+=
"_
#{
pretty_tag_value
}
"
if
specified_id
.
blank?
&&
options
[
"id"
].
present?
else
add_default_name_and_id
(
options
)
end
end
def
add_default_name_and_id
(
options
)
if
options
.
has_key?
(
"index"
)
options
[
"name"
]
||=
tag_name_with_index
(
options
[
"index"
])
options
[
"id"
]
=
options
.
fetch
(
"id"
){
tag_id_with_index
(
options
[
"index"
])
}
options
.
delete
(
"index"
)
elsif
defined?
(
@auto_index
)
options
[
"name"
]
||=
tag_name_with_index
(
@auto_index
)
options
[
"id"
]
=
options
.
fetch
(
"id"
){
tag_id_with_index
(
@auto_index
)
}
else
options
[
"name"
]
||=
tag_name
+
(
options
[
'multiple'
]
?
'[]'
:
''
)
options
[
"id"
]
=
options
.
fetch
(
"id"
){
tag_id
}
end
options
[
"id"
]
=
[
options
.
delete
(
'namespace'
),
options
[
"id"
]].
compact
.
join
(
"_"
).
presence
end
def
tag_name
"
#{
@object_name
}
[
#{
sanitized_method_name
}
]"
end
def
tag_name_with_index
(
index
)
"
#{
@object_name
}
[
#{
index
}
][
#{
sanitized_method_name
}
]"
end
def
tag_id
"
#{
sanitized_object_name
}
_
#{
sanitized_method_name
}
"
end
def
tag_id_with_index
(
index
)
"
#{
sanitized_object_name
}
_
#{
index
}
_
#{
sanitized_method_name
}
"
end
def
sanitized_object_name
@sanitized_object_name
||=
@object_name
.
gsub
(
/\]\[|[^-a-zA-Z0-9:.]/
,
"_"
).
sub
(
/_$/
,
""
)
end
def
sanitized_method_name
@sanitized_method_name
||=
@method_name
.
sub
(
/\?$/
,
""
)
end
end
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录