From 42fa2714c5005bb50679bf9a081d8bf0774f7eec Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 4 May 2010 22:27:17 -0300 Subject: Make use of to_xml and to_json in tests Signed-off-by: Jeremy Kemper --- activeresource/test/cases/base_test.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'activeresource') diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index 2ed7e1c95f..e5f556962b 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -1009,7 +1009,10 @@ class BaseTest < Test::Unit::TestCase def test_to_xml matz = Person.find(1) - xml = matz.encode + encode = matz.encode + xml = matz.to_xml + + assert encode, xml assert xml.include?('') assert xml.include?('Matz') assert xml.include?('1') @@ -1019,9 +1022,11 @@ class BaseTest < Test::Unit::TestCase Person.include_root_in_json = true Person.format = :json joe = Person.find(6) - json = joe.encode + encode = joe.encode + json = joe.to_json Person.format = :xml + assert encode, json assert_match %r{^\{"person":\{"person":\{}, json assert_match %r{"id":6}, json assert_match %r{"name":"Joe"}, json -- cgit v1.2.3 From bea3c26833ad3e1e94f7331e0553a4e2164e7de5 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 6 May 2010 12:35:08 -0300 Subject: Make ActiveResource serialize XML correctly when element_name is set. [#4529] Signed-off-by: Jeremy Kemper --- activeresource/lib/active_resource/base.rb | 19 ++++++++++++---- activeresource/test/cases/base_test.rb | 36 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index ad994214f6..15d77df3b5 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -551,11 +551,22 @@ module ActiveResource @headers ||= {} end - # Do not include any modules in the default element name. This makes it easier to seclude ARes objects - # in a separate namespace without having to set element_name repeatedly. - attr_accessor_with_default(:element_name) { ActiveSupport::Inflector.underscore(to_s.split("::").last) } #:nodoc: + def element_name + model_name.element + end + + def element_name=(value) + model_name.element = value + end + + def collection_name + model_name.collection + end + + def collection_name=(value) + model_name.collection = value + end - attr_accessor_with_default(:collection_name) { ActiveSupport::Inflector.pluralize(element_name) } #:nodoc: attr_accessor_with_default(:primary_key, 'id') #:nodoc: # Gets the \prefix for a resource's nested URL (e.g., prefix/collectionname/1.xml) diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index e5f556962b..5df1f411a4 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -1018,6 +1018,23 @@ class BaseTest < Test::Unit::TestCase assert xml.include?('1') end + def test_to_xml_with_element_name + old_elem_name = Person.element_name + matz = Person.find(1) + Person.element_name = 'ruby_creator' + encode = matz.encode + xml = matz.to_xml + + assert encode, xml + assert xml.include?('') + assert xml.include?('') + assert xml.include?('Matz') + assert xml.include?('1') + assert xml.include?('') + ensure + Person.element_name = old_elem_name + end + def test_to_json Person.include_root_in_json = true Person.format = :json @@ -1033,6 +1050,25 @@ class BaseTest < Test::Unit::TestCase assert_match %r{\}\}\}$}, json end + def test_to_json_with_element_name + old_elem_name = Person.element_name + Person.include_root_in_json = true + Person.format = :json + joe = Person.find(6) + Person.element_name = 'ruby_creator' + encode = joe.encode + json = joe.to_json + Person.format = :xml + + assert encode, json + assert_match %r{^\{"ruby_creator":\{"person":\{}, json + assert_match %r{"id":6}, json + assert_match %r{"name":"Joe"}, json + assert_match %r{\}\}\}$}, json + ensure + Person.element_name = old_elem_name + end + def test_to_param_quacks_like_active_record new_person = Person.new assert_nil new_person.to_param -- cgit v1.2.3 From 903637f5f0a1a9789fa12da1519e028b9faa37d8 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 12 May 2010 15:31:37 -0300 Subject: Fixes to_json and to_xml for ActiveResource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activeresource/lib/active_resource/base.rb | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 15d77df3b5..ffdb5fb50a 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -552,19 +552,20 @@ module ActiveResource end def element_name - model_name.element + @element_name ||= model_name.element end - def element_name=(value) - model_name.element = value + def element_name=(element_name) + @element_name = element_name + @collection_name ||= model_name.collection.sub(/[^\/]*$/, @element_name.pluralize) end def collection_name - model_name.collection + @collection_name ||= model_name.collection end - def collection_name=(value) - model_name.collection = value + def collection_name=(collection_name) + @collection_name = collection_name end attr_accessor_with_default(:primary_key, 'id') #:nodoc: @@ -1306,6 +1307,14 @@ module ActiveResource end end + def to_json(options={}) + super({ :root => self.class.element_name }.merge(options)) + end + + def to_xml(options={}) + super({ :root => self.class.element_name }.merge(options)) + end + protected def connection(refresh = false) self.class.connection(refresh) -- cgit v1.2.3 From a0621c1086165e4b3cff71b54f08a190851b6314 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Fri, 14 May 2010 15:10:10 -0400 Subject: Better code formatting and proper line numbers for stack traces [#4596 state:resolved] Signed-off-by: Jeremy Kemper --- activeresource/lib/active_resource/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/schema.rb b/activeresource/lib/active_resource/schema.rb index 8368b652c2..5758ac9502 100644 --- a/activeresource/lib/active_resource/schema.rb +++ b/activeresource/lib/active_resource/schema.rb @@ -42,7 +42,7 @@ module ActiveResource # :nodoc: # TODO: We should eventually support all of these: # %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |attr_type| KNOWN_ATTRIBUTE_TYPES.each do |attr_type| - class_eval <<-EOV + class_eval <<-EOV, __FILE__, __LINE__ + 1 def #{attr_type.to_s}(*args) options = args.extract_options! attr_names = args -- cgit v1.2.3 From 7ffe76046ad142377bf6b286f4477b09513d1e37 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 14 May 2010 09:34:33 -0300 Subject: ActiveResource shouldn't consider modules in the path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [#4529 state:committed] Signed-off-by: José Valim --- activeresource/lib/active_resource/base.rb | 18 ++---------------- activeresource/test/cases/base_test.rb | 5 +++++ activeresource/test/fixtures/sound.rb | 5 +++++ 3 files changed, 12 insertions(+), 16 deletions(-) create mode 100644 activeresource/test/fixtures/sound.rb (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index ffdb5fb50a..b976844c1c 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -551,22 +551,8 @@ module ActiveResource @headers ||= {} end - def element_name - @element_name ||= model_name.element - end - - def element_name=(element_name) - @element_name = element_name - @collection_name ||= model_name.collection.sub(/[^\/]*$/, @element_name.pluralize) - end - - def collection_name - @collection_name ||= model_name.collection - end - - def collection_name=(collection_name) - @collection_name = collection_name - end + attr_accessor_with_default(:element_name) { model_name.element } #:nodoc: + attr_accessor_with_default(:collection_name) { ActiveSupport::Inflector.pluralize(element_name) } #:nodoc: attr_accessor_with_default(:primary_key, 'id') #:nodoc: diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index 5df1f411a4..4e21e84596 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -2,6 +2,7 @@ require 'abstract_unit' require "fixtures/person" require "fixtures/customer" require "fixtures/street_address" +require "fixtures/sound" require "fixtures/beast" require "fixtures/proxy" require 'active_support/json' @@ -563,6 +564,10 @@ class BaseTest < Test::Unit::TestCase assert_equal '/people/Greg/addresses/1.xml', StreetAddress.element_path(1, 'person_id' => 'Greg') end + def test_module_element_path + assert_equal '/sounds/1.xml', Asset::Sound.element_path(1) + end + def test_custom_element_path_with_redefined_to_param Person.module_eval do alias_method :original_to_param_element_path, :to_param diff --git a/activeresource/test/fixtures/sound.rb b/activeresource/test/fixtures/sound.rb new file mode 100644 index 0000000000..5c0ef5d55c --- /dev/null +++ b/activeresource/test/fixtures/sound.rb @@ -0,0 +1,5 @@ +module Asset + class Sound < ActiveResource::Base + self.site = "http://37s.sunrise.i:3000" + end +end -- cgit v1.2.3