aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/hash/conversions.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash/conversions.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index 83054cde93..74e23bd884 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -1,5 +1,23 @@
require 'date'
require 'xml_simple'
+require 'cgi'
+
+# Extensions needed for Hash#to_query
+class Object
+ def to_param #:nodoc:
+ to_s
+ end
+
+ def to_query(key) #:nodoc:
+ "#{key}=#{CGI.escape(to_param)}"
+ end
+end
+
+class Array
+ def to_query(key) #:nodoc:
+ collect { |value| value.to_query("#{key}[]") } * '&'
+ end
+end
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
@@ -27,6 +45,12 @@ module ActiveSupport #:nodoc:
klass.extend(ClassMethods)
end
+ def to_query(namespace = nil)
+ collect do |key, value|
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
+ end * '&'
+ end
+
def to_xml(options = {})
options[:indent] ||= 2
options.reverse_merge!({ :builder => Builder::XmlMarkup.new(:indent => options[:indent]),