1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Jeremy Kemper 2007-01-24 18:27:36 +00:00
parent 873f5e2f3c
commit 1b1ddf1736
3 changed files with 17 additions and 4 deletions

View file

@ -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]

View file

@ -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

View file

@ -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?