From 1b1ddf17367f902dfbacf9143f0c90b7c42436dc Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 24 Jan 2007 18:27:36 +0000 Subject: Loading a resource preserves its prefix_options. Closes #7353. Silence prefix redefinition warnings; fix type in logged error. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6032 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activeresource/CHANGELOG | 2 ++ activeresource/lib/active_resource/base.rb | 12 ++++++++---- activeresource/test/base_test.rb | 7 +++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index f62bce33a1..79c8e66298 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Loading a resource preserves its prefix_options. #7353 [Ryan Daigle] + * Carry over the convenience of #create from ActiveRecord. Closes #7340. [Ryan Daigle] * Increase ActiveResource::Base test coverage. Closes #7173, #7174 [Rich Collins] diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index faf0f046d6..a4e21b3253 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -45,13 +45,17 @@ module ActiveResource # Sets the resource prefix # prefix/collectionname/1.xml def prefix=(value = '/') + # Replace :placeholders with '#{embedded options[:lookups]}' prefix_call = value.gsub(/:\w+/) { |key| "\#{options[#{key}]}" } - instance_eval <<-end_eval, __FILE__, __LINE__ + + # Redefine the new methods. + code = <<-end_code def prefix_source() "#{value}" end def prefix(options={}) "#{prefix_call}" end - end_eval + end_code + silence_warnings { instance_eval code, __FILE__, __LINE__ } rescue - logger.error "Couldn't set prefix: #{$!}\n #{method_decl}" + logger.error "Couldn't set prefix: #{$!}\n #{code}" raise end @@ -235,7 +239,7 @@ module ActiveResource resource = find_or_create_resource_for(key) resource.new(value) when ActiveResource::Base - value.class.new(value.attributes) + value.class.new(value.attributes, value.prefix_options) else value.dup rescue value end diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index 4d3bb7461e..15e9609034 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -217,6 +217,13 @@ class BaseTest < Test::Unit::TestCase assert_equal '5', matzs_house.id end + # Test that loading a resource preserves its prefix_options. + def test_load_preserves_prefix_options + address = StreetAddress.find(1, :person_id => 1) + ryan = Person.new(:id => 1, :name => 'Ryan', :address => address) + assert_equal address.prefix_options, ryan.address.prefix_options + end + def test_create rick = Person.create(:name => 'Rick') assert rick.valid? -- cgit v1.2.3