提交 1b1ddf17 编写于 作者: J Jeremy Kemper

Loading a resource preserves its prefix_options. Closes #7353. Silence prefix...

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
上级 873f5e2f
*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]
......
......@@ -45,13 +45,17 @@ def prefix(options={})
# 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 @@ def load(attributes)
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
......
......@@ -217,6 +217,13 @@ def test_create_with_custom_prefix
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?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册