From 35433859bd9995a9802abc38b1aba379c0019dff Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 24 Jan 2007 22:12:31 +0000 Subject: Added Hash#to_query to turn a hash of values into a form-encoded query string [Nicholas Seckar] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6038 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../active_support/core_ext/hash/conversions.rb | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'activesupport/lib/active_support/core_ext/hash/conversions.rb') 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]), -- cgit v1.2.3