1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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

View file

@ -43,6 +43,11 @@ class BaseTest < Test::Unit::TestCase
assert_equal site, Person.site assert_equal site, Person.site
end 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 def test_site_reader_uses_superclass_site_until_written
# Superclass is Object so returns nil. # Superclass is Object so returns nil.
assert_nil ActiveResource::Base.site 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=', Person.collection_path(:gender => nil)
assert_equal '/people.xml?gender=male', Person.collection_path('gender' => 'male') 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]) assert_equal '/people.xml?name[]=bob&name[]=your+uncle%2Bme&name[]=&name[]=false', Person.collection_path(:name => ['bob', 'your uncle+me', nil, false])
end end
@ -128,13 +133,13 @@ class BaseTest < Test::Unit::TestCase
def test_prefix def test_prefix
assert_equal "/", Person.prefix assert_equal "/", Person.prefix
assert_equal Set.new, Person.instance_variable_get('@prefix_parameters') assert_equal Set.new, Person.send(:prefix_parameters)
end end
def test_custom_prefix def test_custom_prefix
assert_equal '/people//', StreetAddress.prefix assert_equal '/people//', StreetAddress.prefix
assert_equal '/people/1/', StreetAddress.prefix(:person_id => 1) 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 end
def test_find_by_id def test_find_by_id
@ -232,9 +237,4 @@ class BaseTest < Test::Unit::TestCase
def test_delete def test_delete
assert Person.delete(1) assert Person.delete(1)
end 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 end