diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-02-01 00:57:21 -0200 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-02-02 09:40:24 -0200 |
commit | 471b8554911e65bfbfc5c259f775cb7dac375c88 (patch) | |
tree | bcaf84d66d0d4377cb6c4df632a84101b931920d | |
parent | 4a18cbb6dd7ba9b6c5bd3e42b43fece6644735c2 (diff) | |
download | rails-471b8554911e65bfbfc5c259f775cb7dac375c88.tar.gz rails-471b8554911e65bfbfc5c259f775cb7dac375c88.tar.bz2 rails-471b8554911e65bfbfc5c259f775cb7dac375c88.zip |
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]
-rw-r--r-- | actionpack/lib/action_view/helpers/tags/base.rb | 22 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb | 2 |
2 files changed, 16 insertions, 8 deletions
diff --git a/actionpack/lib/action_view/helpers/tags/base.rb b/actionpack/lib/action_view/helpers/tags/base.rb index ec8e3caf3a..e22612ccd0 100644 --- a/actionpack/lib/action_view/helpers/tags/base.rb +++ b/actionpack/lib/action_view/helpers/tags/base.rb @@ -32,9 +32,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) : + object.send(@method_name) end end @@ -59,13 +61,15 @@ 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 @@ -112,6 +116,10 @@ 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) diff --git a/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb b/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb index cd02408833..201eff048c 100644 --- a/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb +++ b/actionpack/lib/action_view/helpers/tags/collection_radio_buttons.rb @@ -45,7 +45,7 @@ module ActionView 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: |