Permalink
Browse files

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
  • Loading branch information...
1 parent 6a5388b commit 73101af6ab89034b53e86c28b2f8a9f14eece950 @jeremy jeremy committed Dec 29, 2006
Showing with 20 additions and 21 deletions.
  1. +12 −13 activeresource/lib/active_resource/base.rb
  2. +8 −8 activeresource/test/base_test.rb
@@ -4,8 +4,7 @@
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 @@ def site
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 @@ def prefix(options={})
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 @@ def create_site_uri_from(site)
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 = []
@@ -43,6 +43,11 @@ def test_site_accessor_accepts_uri_or_string_argument
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 @@ def test_collection_path_with_parameters
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 @@ def test_nested_element_name
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 @@ def test_destroy_with_custom_prefix
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

0 comments on commit 73101af

Please sign in to comment.