Skip to content

Instantly share code, notes, and snippets.

@AquaGeek
Created May 14, 2011 02:35
Show Gist options
  • Select an option

  • Save AquaGeek/971843 to your computer and use it in GitHub Desktop.

Select an option

Save AquaGeek/971843 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #6716
From 03b6333fc14433803d58f844e93b0e37a6f722a4 Mon Sep 17 00:00:00 2001
From: Brian Durand <[email protected]>
Date: Mon, 18 Apr 2011 14:34:03 -0500
Subject: [PATCH] Fix loading of classes from within ActiveResource to always look in the current namespace. [#6716 state:resolved]
---
activeresource/lib/active_resource/base.rb | 2 +-
activeresource/test/cases/base_test.rb | 6 ++++++
activeresource/test/fixtures/sound.rb | 8 ++++++++
3 files changed, 15 insertions(+), 1 deletions(-)
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 1607637..1988255 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -1370,7 +1370,7 @@ module ActiveResource
# Raises a NameError if it was not found in any of the given nested modules
def find_resource_in_modules(resource_name, module_names)
receiver = Object
- namespaces = module_names[0, module_names.size-1].map do |module_name|
+ namespaces = module_names.map do |module_name|
receiver = receiver.const_get(module_name)
end
const_args = RUBY_VERSION < "1.9" ? [resource_name] : [resource_name, false]
diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb
index 48dacbd..96cde82 100644
--- a/activeresource/test/cases/base_test.rb
+++ b/activeresource/test/cases/base_test.rb
@@ -1102,4 +1102,10 @@ class BaseTest < Test::Unit::TestCase
sound = Asset::Sound.find(1)
assert_equal "Asset::Sound::Author", sound.author.class.to_s
end
+
+ def test_use_embedded_classes
+ sound = Asset::Sound.new("settings" => {"bass" => 10, "treble" => -5})
+ assert_equal Asset::Sound::Settings, sound.settings.class
+ assert_equal({"bass" => 10, "treble" => -5}, sound.settings.attributes)
+ end
end
diff --git a/activeresource/test/fixtures/sound.rb b/activeresource/test/fixtures/sound.rb
index d9d2b99..8371fc0 100644
--- a/activeresource/test/fixtures/sound.rb
+++ b/activeresource/test/fixtures/sound.rb
@@ -1,6 +1,14 @@
module Asset
class Sound < ActiveResource::Base
self.site = "http://37s.sunrise.i:3000"
+
+ # To test non-ActiveResource models for an embedded serialized object
+ class Settings
+ attr_reader :attributes
+ def initialize(attributes = {})
+ @attributes = attributes
+ end
+ end
end
end
--
1.7.3.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment