From 23c7ce71971b2e64d98379bf0a620ababf49fd2e Mon Sep 17 00:00:00 2001 From: Rasik Pandey Date: Thu, 25 Sep 2008 14:08:41 -0400 Subject: [PATCH] adding an option to not include format extensions in paths as it is redundant information since the http headers include the mandatory information --- activeresource/lib/active_resource/base.rb | 10 ++++++++-- .../lib/active_resource/custom_methods.rb | 6 +++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 4192fab..38126d3 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -383,6 +383,12 @@ module ActiveResource alias_method :set_element_name, :element_name= #:nodoc: alias_method :set_collection_name, :collection_name= #:nodoc: + attr_accessor_with_default(:use_format_extension, true) #:nodoc: + + def format_extension + use_format_extension ? ".#{format.extension}" : '' + end + # Gets the element path for the given ID in +id+. If the +query_options+ parameter is omitted, Rails # will split from the \prefix options. # @@ -406,7 +412,7 @@ module ActiveResource # def element_path(id, prefix_options = {}, query_options = nil) prefix_options, query_options = split_options(prefix_options) if query_options.nil? - "#{prefix(prefix_options)}#{collection_name}/#{id}.#{format.extension}#{query_string(query_options)}" + "#{prefix(prefix_options)}#{collection_name}/#{id}#{format_extension}#{query_string(query_options)}" end # Gets the collection path for the REST resources. If the +query_options+ parameter is omitted, Rails @@ -432,7 +438,7 @@ module ActiveResource # def collection_path(prefix_options = {}, query_options = nil) prefix_options, query_options = split_options(prefix_options) if query_options.nil? - "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}" + "#{prefix(prefix_options)}#{collection_name}#{format_extension}#{query_string(query_options)}" end alias_method :set_primary_key, :primary_key= #:nodoc: diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb index 4647e83..3bee3dd 100644 --- a/activeresource/lib/active_resource/custom_methods.rb +++ b/activeresource/lib/active_resource/custom_methods.rb @@ -80,7 +80,7 @@ module ActiveResource module ClassMethods def custom_method_collection_url(method_name, options = {}) prefix_options, query_options = split_options(options) - "#{prefix(prefix_options)}#{collection_name}/#{method_name}.#{format.extension}#{query_string(query_options)}" + "#{prefix(prefix_options)}#{collection_name}/#{method_name}#{format_extension}#{query_string(query_options)}" end end @@ -109,11 +109,11 @@ module ActiveResource private def custom_method_element_url(method_name, options = {}) - "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}.#{self.class.format.extension}#{self.class.__send__(:query_string, options)}" + "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}#{self.class.format_extension}#{self.class.__send__(:query_string, options)}" end def custom_method_new_element_url(method_name, options = {}) - "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}.#{self.class.format.extension}#{self.class.__send__(:query_string, options)}" + "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}#{self.class.format_extension}#{self.class.__send__(:query_string, options)}" end end end -- 1.5.5.3 From d813895f2c4d812eab511bea7acf17f6c9dfc741 Mon Sep 17 00:00:00 2001 From: Rasik Pandey Date: Tue, 6 Jan 2009 11:31:53 -0500 Subject: [PATCH] Adding an option to not include format extensions in paths as it is redundant information since the http headers include the mandatory information --- activeresource/lib/active_resource/base.rb | 4 ++++ activeresource/test/base_test.rb | 10 ++++++++++ 2 files changed, 14 insertions(+), 0 deletions(-) diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 38126d3..9b2fd4d 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -385,6 +385,10 @@ module ActiveResource attr_accessor_with_default(:use_format_extension, true) #:nodoc: + # Retrieves the format extension when building the request url. + # If use_format_extension is false then the extension isn't included. + # By default we include the extension to be backwards-compatible. + # def format_extension use_format_extension ? ".#{format.extension}" : '' end diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index d37a6d4..cfa4373 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -875,4 +875,14 @@ class BaseTest < Test::Unit::TestCase end end end + + def test_element_path_without_format_extension + Person.use_format_extension = false + assert_equal "/people/1", Person.element_path(1) + matz = Person.find(1) + assert_equal "Matz", matz.name + + Person.use_format_extension = true + assert_equal "/people/1.xml", Person.element_path(1) + end end -- 1.5.5.3