aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/tags/base.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/action_view/helpers/tags/base.rb')
-rw-r--r--actionpack/lib/action_view/helpers/tags/base.rb43
1 files changed, 27 insertions, 16 deletions
diff --git a/actionpack/lib/action_view/helpers/tags/base.rb b/actionpack/lib/action_view/helpers/tags/base.rb
index 449f94d347..7c2f12d8e7 100644
--- a/actionpack/lib/action_view/helpers/tags/base.rb
+++ b/actionpack/lib/action_view/helpers/tags/base.rb
@@ -5,8 +5,6 @@ module ActionView
include Helpers::ActiveModelInstanceTag, Helpers::TagHelper, Helpers::FormTagHelper
include FormOptionsHelper
- DEFAULT_FIELD_OPTIONS = { "size" => 30 }
-
attr_reader :object
def initialize(object_name, method_name, template_object, options = {})
@@ -32,9 +30,11 @@ module ActionView
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)
+ method_before_type_cast = @method_name + "_before_type_cast"
+
+ object.respond_to?(method_before_type_cast) ?
+ object.send(method_before_type_cast) :
+ value(object)
end
end
@@ -59,26 +59,28 @@ module ActionView
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"]
+ if tag_value.nil?
add_default_name_and_id(options)
- options["id"] += "_#{pretty_tag_value}" if specified_id.blank? && options["id"].present?
else
+ specified_id = options["id"]
add_default_name_and_id(options)
+
+ if specified_id.blank? && options["id"].present?
+ options["id"] += "_#{sanitized_value(tag_value)}"
+ end
end
end
def add_default_name_and_id(options)
if options.has_key?("index")
- options["name"] ||= tag_name_with_index(options["index"])
+ options["name"] ||= options.fetch("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["name"] ||= options.fetch("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["name"] ||= options.fetch("name"){ options['multiple'] ? tag_name_multiple : tag_name }
options["id"] = options.fetch("id"){ tag_id }
end
options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence
@@ -88,6 +90,10 @@ module ActionView
"#{@object_name}[#{sanitized_method_name}]"
end
+ def tag_name_multiple
+ "#{tag_name}[]"
+ end
+
def tag_name_with_index(index)
"#{@object_name}[#{index}][#{sanitized_method_name}]"
end
@@ -108,11 +114,16 @@ module ActionView
@sanitized_method_name ||= @method_name.sub(/\?$/,"")
end
+ def sanitized_value(value)
+ value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase
+ end
+
def select_content_tag(option_tags, options, html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
select = content_tag("select", add_options(option_tags, options, value(object)), html_options)
- if html_options["multiple"]
+
+ if html_options["multiple"] && options.fetch(:include_hidden) { true }
tag("input", :disabled => html_options["disabled"], :name => html_options["name"], :type => "hidden", :value => "") + select
else
select
@@ -121,13 +132,13 @@ module ActionView
def add_options(option_tags, options, value = nil)
if options[:include_blank]
- option_tags = "<option value=\"\">#{ERB::Util.html_escape(options[:include_blank]) if options[:include_blank].kind_of?(String)}</option>\n" + option_tags
+ option_tags = content_tag('option', options[:include_blank].kind_of?(String) ? options[:include_blank] : nil, :value => '') + "\n" + option_tags
end
if value.blank? && options[:prompt]
prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('helpers.select.prompt', :default => 'Please select')
- option_tags = "<option value=\"\">#{ERB::Util.html_escape(prompt)}</option>\n" + option_tags
+ option_tags = content_tag('option', prompt, :value => '') + "\n" + option_tags
end
- option_tags.html_safe
+ option_tags
end
end
end