aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/hash
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-12-09 22:12:07 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-12-09 22:12:07 +0000
commit2d2fd67b51e204e77e24d1b493cebae72d8fa33d (patch)
treef44e6e4e785def5dbd8a117a22c1ef5ed79c3fa4 /activesupport/lib/active_support/core_ext/hash
parentbafd698acb160fd241efe6f027b83676b9e80d1f (diff)
downloadrails-2d2fd67b51e204e77e24d1b493cebae72d8fa33d.tar.gz
rails-2d2fd67b51e204e77e24d1b493cebae72d8fa33d.tar.bz2
rails-2d2fd67b51e204e77e24d1b493cebae72d8fa33d.zip
Move #to_query methods where they ought to belong. Closes #10395 [Chu Yeow]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8343 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash')
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb24
1 files changed, 7 insertions, 17 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index e196c5d7b9..8021dfa87a 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -4,23 +4,6 @@ require 'base64'
require 'builder'
require 'xmlsimple'
-# Extensions needed for Hash#to_query
-class Object
- def to_param #:nodoc:
- to_s
- end
-
- def to_query(key) #:nodoc:
- "#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}"
- end
-end
-
-class Array
- def to_query(key) #:nodoc:
- collect { |value| value.to_query("#{key}[]") } * '&'
- end
-end
-
# Locked down XmlSimple#xml_in_string
class XmlSimple
# Same as xml_in but doesn't try to smartly shoot itself in the foot.
@@ -100,6 +83,13 @@ module ActiveSupport #:nodoc:
klass.extend(ClassMethods)
end
+ # Converts a hash into a string suitable for use as a URL query string. An optional <tt>namespace</tt> can be
+ # passed to enclose the param names (see example below).
+ #
+ # ==== Example:
+ # { :name => 'David', :nationality => 'Danish' }.to_query # => "name=David&nationality=Danish"
+ #
+ # { :name => 'David', :nationality => 'Danish' }.to_query('user') # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
def to_query(namespace = nil)
collect do |key, value|
value.to_query(namespace ? "#{namespace}[#{key}]" : key)