aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource
diff options
context:
space:
mode:
Diffstat (limited to 'activeresource')
-rw-r--r--activeresource/lib/active_resource/base.rb14
-rw-r--r--activeresource/lib/active_resource/schema.rb2
-rw-r--r--activeresource/test/cases/base_test.rb50
-rw-r--r--activeresource/test/fixtures/sound.rb5
4 files changed, 64 insertions, 7 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index ad994214f6..b976844c1c 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -551,11 +551,9 @@ 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:
-
+ 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:
# Gets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.xml</tt>)
@@ -1295,6 +1293,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)
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
diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb
index 2ed7e1c95f..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
@@ -1009,25 +1014,66 @@ 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?('<?xml version="1.0" encoding="UTF-8"?>')
assert xml.include?('<name>Matz</name>')
assert xml.include?('<id type="integer">1</id>')
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?('<?xml version="1.0" encoding="UTF-8"?>')
+ assert xml.include?('<ruby-creator>')
+ assert xml.include?('<name>Matz</name>')
+ assert xml.include?('<id type="integer">1</id>')
+ assert xml.include?('</ruby-creator>')
+ ensure
+ Person.element_name = old_elem_name
+ end
+
def test_to_json
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
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
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