Permalink
Browse files

Consolidate Object#to_param and #to_query core extensions

  • Loading branch information...
jeremy committed Nov 1, 2009
1 parent 8935854 commit b540eca5889d7a28fac39c9ec0df715aa89487ce
@@ -1,6 +1,4 @@
-require 'active_support/core_ext/object/conversions'
-require 'active_support/core_ext/boolean/conversions'
-require 'active_support/core_ext/nil/conversions'
+require 'active_support/core_ext/object/to_param'
require 'active_support/core_ext/regexp'
module ActionDispatch
@@ -8,6 +8,7 @@
require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/object/misc'
+require 'active_support/core_ext/object/to_query'
require 'set'
require 'uri'
@@ -2,12 +2,7 @@
require 'active_support/core_ext/benchmark'
require 'active_support/core_ext/exception'
require 'active_support/core_ext/class/attribute_accessors'
-
-%w(hash nil string time date date_time array big_decimal range object boolean).each do |library|
- require "active_support/core_ext/#{library}/conversions"
-end
-
-# require 'active_support/core_ext' # FIXME: pulling in all to_param extensions
+require 'active_support/core_ext/object/to_param'
module ActiveSupport
# See ActiveSupport::Cache::Store for documentation.
@@ -40,22 +40,6 @@ def to_sentence(options = {})
end
end
-
- # Calls <tt>to_param</tt> on all its elements and joins the result with
- # slashes. This is used by <tt>url_for</tt> in Action Pack.
- def to_param
- collect { |e| e.to_param }.join '/'
- end
-
- # Converts an array into a string suitable for use as a URL query string,
- # using the given +key+ as the param name.
- #
- # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
- def to_query(key)
- prefix = "#{key}[]"
- collect { |value| value.to_query(prefix) }.join '&'
- end
-
# Converts a collection of elements into a formatted string by calling
# <tt>to_s</tt> on all elements and joining them:
#
@@ -1 +0,0 @@
-require 'active_support/core_ext/boolean/conversions'
@@ -1,11 +0,0 @@
-class TrueClass
- def to_param
- self
- end
-end
-
-class FalseClass
- def to_param
- self
- end
-end
@@ -1,6 +1,4 @@
require 'active_support/time'
-require 'active_support/core_ext/object/conversions'
-require 'active_support/core_ext/array/conversions'
require 'active_support/core_ext/hash/reverse_merge'
class Hash
@@ -68,21 +66,6 @@ def content_type
)
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).
- #
- # ==== Examples
- # { :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)
- end.sort * '&'
- end
-
- alias_method :to_param, :to_query
-
def to_xml(options = {})
require 'builder' unless defined?(Builder)
@@ -1 +0,0 @@
-require 'active_support/core_ext/nil/conversions'
@@ -1,5 +0,0 @@
-class NilClass
- def to_param
- self
- end
-end
@@ -1,18 +1,4 @@
+require 'active_support/core_ext/object/to_param'
+require 'active_support/core_ext/object/to_query'
require 'active_support/core_ext/array/conversions'
require 'active_support/core_ext/hash/conversions'
-
-class Object
- # Alias of <tt>to_s</tt>.
- def to_param
- to_s
- end
-
- # Converts an object into a string suitable for use as a URL query string, using the given <tt>key</tt> as the
- # param name.
- #
- # Note: This method is defined as a default implementation for all Objects for Hash#to_query to work.
- def to_query(key)
- require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
- "#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}"
- end
-end
@@ -0,0 +1,47 @@
+class Object
+ # Alias of <tt>to_s</tt>.
+ def to_param
+ to_s
+ end
+end
+
+class NilClass
+ def to_param
+ self
+ end
+end
+
+class TrueClass
+ def to_param
+ self
+ end
+end
+
+class FalseClass
+ def to_param
+ self
+ end
+end
+
+class Array
+ # Calls <tt>to_param</tt> on all its elements and joins the result with
+ # slashes. This is used by <tt>url_for</tt> in Action Pack.
+ def to_param
+ collect { |e| e.to_param }.join '/'
+ end
+end
+
+class Hash
+ # 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).
+ #
+ # ==== Examples
+ # { :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_param(namespace = nil)
+ collect do |key, value|
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
+ end.sort * '&'
+ end
+end
@@ -0,0 +1,27 @@
+require 'active_support/core_ext/object/to_param'
+
+class Object
+ # Converts an object into a string suitable for use as a URL query string, using the given <tt>key</tt> as the
+ # param name.
+ #
+ # Note: This method is defined as a default implementation for all Objects for Hash#to_query to work.
+ def to_query(key)
+ require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
+ "#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}"
+ end
+end
+
+class Array
+ # Converts an array into a string suitable for use as a URL query string,
+ # using the given +key+ as the param name.
+ #
+ # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
+ def to_query(key)
+ prefix = "#{key}[]"
+ collect { |value| value.to_query(prefix) }.join '&'
+ end
+end
+
+class Hash
+ alias_method :to_query, :to_param
+end
@@ -1,12 +0,0 @@
-require 'abstract_unit'
-require 'active_support/core_ext/boolean/conversions'
-
-class BooleanExtAccessTests < Test::Unit::TestCase
- def test_to_param_on_true
- assert_equal true, true.to_param
- end
-
- def test_to_param_on_false
- assert_equal false, false.to_param
- end
-end
@@ -899,42 +899,6 @@ def test_to_xml_dups_options
# :builder, etc, shouldn't be added to options
assert_equal({:skip_instruct => true}, options)
end
-end
-
-class QueryTest < Test::Unit::TestCase
- def test_simple_conversion
- assert_query_equal 'a=10', :a => 10
- end
-
- def test_cgi_escaping
- assert_query_equal 'a%3Ab=c+d', 'a:b' => 'c d'
- end
-
- def test_nil_parameter_value
- empty = Object.new
- def empty.to_param; nil end
- assert_query_equal 'a=', 'a' => empty
- end
-
- def test_nested_conversion
- assert_query_equal 'person%5Blogin%5D=seckar&person%5Bname%5D=Nicholas',
- :person => {:name => 'Nicholas', :login => 'seckar'}
- end
-
- def test_multiple_nested
- assert_query_equal 'account%5Bperson%5D%5Bid%5D=20&person%5Bid%5D=10',
- :person => {:id => 10}, :account => {:person => {:id => 20}}
- end
-
- def test_array_values
- assert_query_equal 'person%5Bid%5D%5B%5D=10&person%5Bid%5D%5B%5D=20',
- :person => {:id => [10, 20]}
- end
-
- def test_array_values_are_not_sorted
- assert_query_equal 'person%5Bid%5D%5B%5D=20&person%5Bid%5D%5B%5D=10',
- :person => {:id => [20, 10]}
- end
def test_expansion_count_is_limited
expected = {
@@ -962,9 +926,4 @@ def test_expansion_count_is_limited
Hash.from_xml(attack_xml)
end
end
-
- private
- def assert_query_equal(expected, actual, message = nil)
- assert_equal expected.split('&'), actual.to_query.split('&')
- end
end
@@ -1,8 +0,0 @@
-require 'abstract_unit'
-require 'active_support/core_ext/nil/conversions'
-
-class NilExtAccessTests < Test::Unit::TestCase
- def test_to_param
- assert_nil nil.to_param
- end
-end
@@ -0,0 +1,19 @@
+require 'abstract_unit'
+require 'active_support/core_ext/object/to_param'
+
+class ToParamTest < Test::Unit::TestCase
+ def test_object
+ foo = Object.new
+ def foo.to_s; 'foo' end
+ assert_equal 'foo', foo.to_param
+ end
+
+ def test_nil
+ assert_nil nil.to_param
+ end
+
+ def test_boolean
+ assert_equal true, true.to_param
+ assert_equal false, false.to_param
+ end
+end
@@ -0,0 +1,43 @@
+require 'abstract_unit'
+require 'active_support/core_ext/object/to_query'
+
+class ToQueryTest < Test::Unit::TestCase
+ def test_simple_conversion
+ assert_query_equal 'a=10', :a => 10
+ end
+
+ def test_cgi_escaping
+ assert_query_equal 'a%3Ab=c+d', 'a:b' => 'c d'
+ end
+
+ def test_nil_parameter_value
+ empty = Object.new
+ def empty.to_param; nil end
+ assert_query_equal 'a=', 'a' => empty
+ end
+
+ def test_nested_conversion
+ assert_query_equal 'person%5Blogin%5D=seckar&person%5Bname%5D=Nicholas',
+ :person => {:name => 'Nicholas', :login => 'seckar'}
+ end
+
+ def test_multiple_nested
+ assert_query_equal 'account%5Bperson%5D%5Bid%5D=20&person%5Bid%5D=10',
+ :person => {:id => 10}, :account => {:person => {:id => 20}}
+ end
+
+ def test_array_values
+ assert_query_equal 'person%5Bid%5D%5B%5D=10&person%5Bid%5D%5B%5D=20',
+ :person => {:id => [10, 20]}
+ end
+
+ def test_array_values_are_not_sorted
+ assert_query_equal 'person%5Bid%5D%5B%5D=20&person%5Bid%5D%5B%5D=10',
+ :person => {:id => [20, 10]}
+ end
+
+ private
+ def assert_query_equal(expected, actual, message = nil)
+ assert_equal expected.split('&'), actual.to_query.split('&')
+ end
+end
@@ -1,16 +1,9 @@
require 'abstract_unit'
require 'active_support/core_ext/object/metaclass'
-require 'active_support/core_ext/object/conversions'
class ObjectExtTest < Test::Unit::TestCase
def test_tap_yields_and_returns_self
foo = Object.new
assert_equal foo, foo.tap { |x| assert_equal foo, x; :bar }
end
-
- def test_to_param
- foo = Object.new
- foo.class_eval("def to_s; 'foo'; end")
- assert_equal 'foo', foo.to_param
- end
end

0 comments on commit b540eca

Please sign in to comment.