From 4be6544cbc4e7e9e4e0a4b9712d34a2744ce9c16 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 12 Jan 2012 20:06:34 -0200 Subject: Split tag options helper in smaller methods --- actionpack/lib/action_view/helpers/tag_helper.rb | 30 +++++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index 93a3c40683..4b5d97efe8 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -134,23 +134,35 @@ module ActionView options.each_pair do |key, value| if key.to_s == 'data' && value.is_a?(Hash) value.each do |k, v| - if !v.is_a?(String) && !v.is_a?(Symbol) - v = v.to_json - end - v = ERB::Util.html_escape(v) if escape - attrs << %(data-#{k.to_s.dasherize}="#{v}") + attrs << data_tag_option(k, v, escape) end elsif BOOLEAN_ATTRIBUTES.include?(key) - attrs << %(#{key}="#{key}") if value + attrs << boolean_tag_option(key, value) if value elsif !value.nil? - final_value = value.is_a?(Array) ? value.join(" ") : value - final_value = ERB::Util.html_escape(final_value) if escape - attrs << %(#{key}="#{final_value}") + attrs << tag_option(key, value, escape) end end " #{attrs.sort * ' '}".html_safe unless attrs.empty? end end + + def data_tag_option(k, v, escape) + if !v.is_a?(String) && !v.is_a?(Symbol) + v = v.to_json + end + v = ERB::Util.html_escape(v) if escape + %(data-#{k.to_s.dasherize}="#{v}") + end + + def boolean_tag_option(key, value) + %(#{key}="#{key}") + end + + def tag_option(key, value, escape) + final_value = value.is_a?(Array) ? value.join(" ") : value + final_value = ERB::Util.html_escape(final_value) if escape + %(#{key}="#{final_value}") + end end end end -- cgit v1.2.3 From 8bc065ee654b3917b9e058c92b649672cec5c2dd Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 12 Jan 2012 20:09:16 -0200 Subject: Refactor and improve readability --- actionpack/lib/action_view/helpers/tag_helper.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index 4b5d97efe8..523a7f74c9 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -125,7 +125,8 @@ module ActionView def content_tag_string(name, content, options, escape = true) tag_options = tag_options(options, escape) if options - "<#{name}#{tag_options}>#{escape ? ERB::Util.h(content) : content}".html_safe + content = ERB::Util.h(content) if escape + "<#{name}#{tag_options}>#{content}".html_safe end def tag_options(options, escape = true) @@ -147,10 +148,8 @@ module ActionView end def data_tag_option(k, v, escape) - if !v.is_a?(String) && !v.is_a?(Symbol) - v = v.to_json - end - v = ERB::Util.html_escape(v) if escape + v = v.to_json if !v.is_a?(String) && !v.is_a?(Symbol) + v = ERB::Util.h(v) if escape %(data-#{k.to_s.dasherize}="#{v}") end @@ -159,9 +158,9 @@ module ActionView end def tag_option(key, value, escape) - final_value = value.is_a?(Array) ? value.join(" ") : value - final_value = ERB::Util.html_escape(final_value) if escape - %(#{key}="#{final_value}") + value = value.join(" ") if value.is_a?(Array) + value = ERB::Util.h(value) if escape + %(#{key}="#{value}") end end end -- cgit v1.2.3 From b01fd08a8c51b2efea242b93619508707ac0725d Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 12 Jan 2012 20:10:59 -0200 Subject: Return faster when no option is given --- actionpack/lib/action_view/helpers/tag_helper.rb | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index 523a7f74c9..37a3fa290e 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -130,21 +130,20 @@ module ActionView end def tag_options(options, escape = true) - unless options.blank? - attrs = [] - options.each_pair do |key, value| - if key.to_s == 'data' && value.is_a?(Hash) - value.each do |k, v| - attrs << data_tag_option(k, v, escape) - end - elsif BOOLEAN_ATTRIBUTES.include?(key) - attrs << boolean_tag_option(key, value) if value - elsif !value.nil? - attrs << tag_option(key, value, escape) + return if options.blank? + attrs = [] + options.each_pair do |key, value| + if key.to_s == 'data' && value.is_a?(Hash) + value.each do |k, v| + attrs << data_tag_option(k, v, escape) end + elsif BOOLEAN_ATTRIBUTES.include?(key) + attrs << boolean_tag_option(key, value) if value + elsif !value.nil? + attrs << tag_option(key, value, escape) end - " #{attrs.sort * ' '}".html_safe unless attrs.empty? end + " #{attrs.sort * ' '}".html_safe unless attrs.empty? end def data_tag_option(k, v, escape) -- cgit v1.2.3 From c4c1c252e27f18daf4e26d2003077ae005489909 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 12 Jan 2012 20:11:31 -0200 Subject: Remove value argument from boolean option --- actionpack/lib/action_view/helpers/tag_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index 37a3fa290e..e5fd11e021 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -138,7 +138,7 @@ module ActionView attrs << data_tag_option(k, v, escape) end elsif BOOLEAN_ATTRIBUTES.include?(key) - attrs << boolean_tag_option(key, value) if value + attrs << boolean_tag_option(key) if value elsif !value.nil? attrs << tag_option(key, value, escape) end @@ -152,7 +152,7 @@ module ActionView %(data-#{k.to_s.dasherize}="#{v}") end - def boolean_tag_option(key, value) + def boolean_tag_option(key) %(#{key}="#{key}") end -- cgit v1.2.3 From cfee068b627a2aaf066bf003f678e50c8695e62c Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 12 Jan 2012 20:15:02 -0200 Subject: Delegate data tag option to tag option for escaping --- actionpack/lib/action_view/helpers/tag_helper.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index e5fd11e021..d7a2651bad 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -134,7 +134,7 @@ module ActionView attrs = [] options.each_pair do |key, value| if key.to_s == 'data' && value.is_a?(Hash) - value.each do |k, v| + value.each_pair do |k, v| attrs << data_tag_option(k, v, escape) end elsif BOOLEAN_ATTRIBUTES.include?(key) @@ -146,10 +146,11 @@ module ActionView " #{attrs.sort * ' '}".html_safe unless attrs.empty? end - def data_tag_option(k, v, escape) - v = v.to_json if !v.is_a?(String) && !v.is_a?(Symbol) - v = ERB::Util.h(v) if escape - %(data-#{k.to_s.dasherize}="#{v}") + def data_tag_option(key, value, escape) + key = "data-#{key.to_s.dasherize}" + value = value.to_json if !value.is_a?(String) && !value.is_a?(Symbol) + + tag_option(key, value, escape) end def boolean_tag_option(key) -- cgit v1.2.3 From 95545bfd75912e11d24ce9439bf9255ff3adea6f Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 12 Jan 2012 20:41:03 -0200 Subject: Refactor url_for helper to check for nil in the case statement only --- actionpack/lib/action_view/helpers/url_helper.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index 0c2e1aa3a9..5af5965c36 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -97,12 +97,12 @@ module ActionView # <%= url_for(:back) %> # # if request.env["HTTP_REFERER"] is not set or is blank # # => javascript:history.back() - def url_for(options = {}) - options ||= {} + def url_for(options = nil) case options when String options - when Hash + when nil, Hash + options ||= {} options = options.symbolize_keys.reverse_merge!(:only_path => options[:host].nil?) super when :back @@ -301,7 +301,7 @@ module ActionView # #
# # " # - # + # # <%= button_to "Delete Image", { :action => "delete", :id => @image.id }, # :confirm => "Are you sure?", :method => :delete %> # # => "
@@ -333,9 +333,9 @@ module ActionView form_method = method.to_s == 'get' ? 'get' : 'post' form_options = html_options.delete('form') || {} form_options[:class] ||= html_options.delete('form_class') || 'button_to' - + remote = html_options.delete('remote') - + request_token_tag = '' if form_method == 'post' && protect_against_forgery? request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) @@ -350,7 +350,7 @@ module ActionView form_options.merge!(:method => form_method, :action => url) form_options.merge!("data-remote" => "true") if remote - + "#{tag(:form, form_options, true)}
#{method_tag}#{tag("input", html_options)}#{request_token_tag}
".html_safe end @@ -624,7 +624,7 @@ module ActionView html_options["data-disable-with"] = disable_with if disable_with html_options["data-confirm"] = confirm if confirm - add_method_to_attributes!(html_options, method) if method + add_method_to_attributes!(html_options, method) if method html_options else -- cgit v1.2.3