From 68b870f6be0eff3de6b3dfb6ba64b253419260ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 16 Jan 2012 20:14:56 -0300 Subject: Extract FileField --- actionpack/lib/action_view/helpers/form_helper.rb | 2 +- actionpack/lib/action_view/helpers/tags.rb | 1 + actionpack/lib/action_view/helpers/tags/file_field.rb | 12 ++++++++++++ actionpack/lib/action_view/helpers/tags/text_field.rb | 4 ++-- 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 actionpack/lib/action_view/helpers/tags/file_field.rb (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 1fbd3f80e6..9c8966e528 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -738,7 +738,7 @@ module ActionView # # => # def file_field(object_name, method, options = {}) - InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("file", options.update({:size => nil})) + ActionView::Helpers::Tags::FileField.new(object_name, method, self, options).render end # Returns a textarea opening and closing tag set tailored for accessing a specified attribute (identified by +method+) diff --git a/actionpack/lib/action_view/helpers/tags.rb b/actionpack/lib/action_view/helpers/tags.rb index b663f8b68f..5b43f43fea 100644 --- a/actionpack/lib/action_view/helpers/tags.rb +++ b/actionpack/lib/action_view/helpers/tags.rb @@ -6,6 +6,7 @@ module ActionView autoload :TextField, 'action_view/helpers/tags/text_field' autoload :PasswordField, 'action_view/helpers/tags/password_field' autoload :HiddenField, 'action_view/helpers/tags/hidden_field' + autoload :FileField, 'action_view/helpers/tags/file_field' end end end diff --git a/actionpack/lib/action_view/helpers/tags/file_field.rb b/actionpack/lib/action_view/helpers/tags/file_field.rb new file mode 100644 index 0000000000..56442e1c14 --- /dev/null +++ b/actionpack/lib/action_view/helpers/tags/file_field.rb @@ -0,0 +1,12 @@ +module ActionView + module Helpers + module Tags + class FileField < TextField #:nodoc: + def render + @options.update(:size => nil) + super + end + end + end + end +end diff --git a/actionpack/lib/action_view/helpers/tags/text_field.rb b/actionpack/lib/action_view/helpers/tags/text_field.rb index 758fd636f2..0f81726eb4 100644 --- a/actionpack/lib/action_view/helpers/tags/text_field.rb +++ b/actionpack/lib/action_view/helpers/tags/text_field.rb @@ -7,7 +7,7 @@ module ActionView options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size") options = DEFAULT_FIELD_OPTIONS.merge(options) options["type"] ||= field_type - options["value"] = options.fetch("value"){ value_before_type_cast(object) } + options["value"] = options.fetch("value"){ value_before_type_cast(object) } unless field_type == "file" options["value"] &&= ERB::Util.html_escape(options["value"]) add_default_name_and_id(options) tag("input", options) @@ -16,7 +16,7 @@ module ActionView private def field_type - self.class.name.split("::").last.sub("Field", "").downcase + @field_type ||= self.class.name.split("::").last.sub("Field", "").downcase end end end -- cgit v1.2.3