diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-01-24 22:12:31 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-01-24 22:12:31 +0000 |
commit | 35433859bd9995a9802abc38b1aba379c0019dff (patch) | |
tree | 2a0e2f3dfd44e9aa6347fb47060d05b632a7e0cb /activesupport/lib | |
parent | 5544231caf768d217bcb85842f6d243f3481bd04 (diff) | |
download | rails-35433859bd9995a9802abc38b1aba379c0019dff.tar.gz rails-35433859bd9995a9802abc38b1aba379c0019dff.tar.bz2 rails-35433859bd9995a9802abc38b1aba379c0019dff.zip |
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
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/conversions.rb | 24 |
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]), |