diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-12-29 01:18:09 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-12-29 01:18:09 +0000 |
commit | 4e57ac35443759778bd6c1fcfd29e8bd85e9d15d (patch) | |
tree | d1414e69479fcc0807d4e57f3c86b624f1981ebe /activeresource/lib | |
parent | f9f84d9f6dea72cae4fc8e31448df408caccbd59 (diff) | |
download | rails-4e57ac35443759778bd6c1fcfd29e8bd85e9d15d.tar.gz rails-4e57ac35443759778bd6c1fcfd29e8bd85e9d15d.tar.bz2 rails-4e57ac35443759778bd6c1fcfd29e8bd85e9d15d.zip |
Query string support. Closes #6855.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5804 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activeresource/lib')
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index a8918b1eee..8c4d82c421 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -1,4 +1,6 @@ require 'active_resource/connection' +require 'cgi' +require 'set' module ActiveResource class Base @@ -29,7 +31,7 @@ module ActiveResource attr_accessor_with_default(:element_name) { to_s.underscore } attr_accessor_with_default(:collection_name) { element_name.pluralize } attr_accessor_with_default(:primary_key, 'id') - + def prefix(options={}) default = site.path default << '/' unless default[-1..-1] == '/' @@ -38,21 +40,29 @@ module ActiveResource end def prefix=(value = '/') - prefix_call = value.gsub(/:\w+/) { |s| "\#{options[#{s}]}" } - method_decl = %(def self.prefix(options={}) "#{prefix_call}" end) - eval method_decl + @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__ + rescue + logger.error "Couldn't set prefix: #{$!}\n #{method_decl}" + raise end + alias_method :set_prefix, :prefix= alias_method :set_element_name, :element_name= alias_method :set_collection_name, :collection_name= def element_path(id, options = {}) - "#{prefix(options)}#{collection_name}/#{id}.xml" + "#{prefix(options)}#{collection_name}/#{id}.xml#{query_string(options)}" end def collection_path(options = {}) - "#{prefix(options)}#{collection_name}.xml" + "#{prefix(options)}#{collection_name}.xml#{query_string(options)}" end alias_method :set_primary_key, :primary_key= @@ -88,6 +98,28 @@ module ActiveResource def create_site_uri_from(site) site.is_a?(URI) ? site.dup : URI.parse(site) end + + def query_string(options) + # Omit parameters which appear in the URI path. + query_params = options.reject { |key, value| @prefix_parameters.include?(key) } + + # Accumulate a list of escaped key=value pairs for the given parameters. + pairs = [] + query_params.each do |key, value| + key = CGI.escape(key.to_s) + + # a => b becomes a=b + # a => [b, c] becomes a[]=b&a[]=c + case value + when Array + value.each { |val| pairs << "#{key}[]=#{CGI.escape(val.to_s)}" } + else + pairs << "#{key}=#{CGI.escape(value.to_s)}" + end + end + + "?#{pairs * '&'}" unless pairs.empty? + end end attr_accessor :attributes |