Skip to content
Browse files

Refactor value sanitization logic

* Extract value sanitization from default name and id method and new
  collection helpers;
* No need to sanitize value in default name and id always;
* Improve value_before_type_cast to avoid concating the same method name
  string twice.

[Carlos Antonio da Silva + Rafael Mendonça França]
  • Loading branch information...
1 parent 4a18cbb commit 471b8554911e65bfbfc5c259f775cb7dac375c88 @carlosantoniodasilva carlosantoniodasilva committed Feb 1, 2012
View
22 actionpack/lib/action_view/helpers/tags/base.rb
@@ -32,9 +32,11 @@ def value(object)
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) :
+ object.send(@method_name)
end
end
@@ -59,13 +61,15 @@ def retrieve_autoindex(pre_match)
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
@@ -112,6 +116,10 @@ def sanitized_method_name
@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)
View
2 actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb
@@ -45,7 +45,7 @@ def default_html_options_for_collection(item, value) #:nodoc:
end
def sanitize_attribute_name(value) #:nodoc:
- "#{sanitized_method_name}_#{value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase}"
+ "#{sanitized_method_name}_#{sanitized_value(value)}"
end
def render_collection #:nodoc:

0 comments on commit 471b855

Please sign in to comment.
Something went wrong with that request. Please try again.