prefix_parameters pulls /:path/:params from the URI prefix

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5809 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2006-12-29 22:16:04 +00:00
parent 6a5388b654
commit 73101af6ab
2 changed files with 20 additions and 21 deletions

View File

@ -4,8 +4,7 @@ require 'set'
module ActiveResource
class Base
# The logger for logging diagnostic and trace information during ARes
# calls.
# The logger for diagnosing and tracing ARes calls.
cattr_accessor :logger
class << self
@ -18,9 +17,8 @@ module ActiveResource
end
def site=(site)
@site = create_site_uri_from(site)
@connection = nil
@site
@site = create_site_uri_from(site)
end
def connection(refresh = false)
@ -40,13 +38,11 @@ module ActiveResource
end
def prefix=(value = '/')
@prefix_parameters = Set.new
prefix_call = value.gsub(/:\w+/) do |key|
@prefix_parameters << key[1..-1].to_sym
"\#{options[#{key}]}"
end
method_decl = %(def prefix(options={}) "#{prefix_call}" end)
instance_eval method_decl, __FILE__, __LINE__
prefix_call = value.gsub(/:\w+/) { |key| "\#{options[#{key}]}" }
instance_eval <<-end_eval, __FILE__, __LINE__
def prefix_source() "#{value}" end
def prefix(options={}) "#{prefix_call}" end
end_eval
rescue
logger.error "Couldn't set prefix: #{$!}\n #{method_decl}"
raise
@ -99,10 +95,13 @@ module ActiveResource
site.is_a?(URI) ? site.dup : URI.parse(site)
end
def prefix_parameters
@prefix_parameters ||= prefix_source.scan(/:\w+/).map { |key| key[1..-1].to_sym }.to_set
end
def query_string(options)
# Omit parameters which appear in the URI path.
prefix unless defined?(@prefix_parameters)
query_params = options.reject { |key, value| @prefix_parameters.include?(key) }
query_params = options.reject { |key, value| prefix_parameters.include?(key) }
# Accumulate a list of escaped key=value pairs for the given parameters.
pairs = []

View File

@ -43,6 +43,11 @@ class BaseTest < Test::Unit::TestCase
assert_equal site, Person.site
end
def test_should_use_site_prefix_and_credentials
assert_equal 'http://foo:bar@beast.caboo.se', Forum.site.to_s
assert_equal 'http://foo:bar@beast.caboo.se/forums/:forum_id', Topic.site.to_s
end
def test_site_reader_uses_superclass_site_until_written
# Superclass is Object so returns nil.
assert_nil ActiveResource::Base.site
@ -88,7 +93,7 @@ class BaseTest < Test::Unit::TestCase
assert_equal '/people.xml?gender=', Person.collection_path(:gender => nil)
assert_equal '/people.xml?gender=male', Person.collection_path('gender' => 'male')
assert_equal '/people.xml?student=true&gender=male', Person.collection_path(:gender => 'male', :student => true)
assert_equal '/people.xml?gender=male&student=true', Person.collection_path(:gender => 'male', :student => true)
assert_equal '/people.xml?name[]=bob&name[]=your+uncle%2Bme&name[]=&name[]=false', Person.collection_path(:name => ['bob', 'your uncle+me', nil, false])
end
@ -128,13 +133,13 @@ class BaseTest < Test::Unit::TestCase
def test_prefix
assert_equal "/", Person.prefix
assert_equal Set.new, Person.instance_variable_get('@prefix_parameters')
assert_equal Set.new, Person.send(:prefix_parameters)
end
def test_custom_prefix
assert_equal '/people//', StreetAddress.prefix
assert_equal '/people/1/', StreetAddress.prefix(:person_id => 1)
assert_equal [:person_id].to_set, StreetAddress.instance_variable_get('@prefix_parameters')
assert_equal [:person_id].to_set, StreetAddress.send(:prefix_parameters)
end
def test_find_by_id
@ -232,9 +237,4 @@ class BaseTest < Test::Unit::TestCase
def test_delete
assert Person.delete(1)
end
def test_should_use_site_prefix_and_credentials
assert_equal 'http://foo:bar@beast.caboo.se', Forum.site.to_s
assert_equal 'http://foo:bar@beast.caboo.se/forums/:forum_id', Topic.site.to_s
end
end