diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2012-02-20 21:15:11 -0800 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2012-02-20 21:15:11 -0800 |
commit | c5bf53822f5887c9b235ceef388b1c8a977bcc88 (patch) | |
tree | 19d6912f9af63802436ddfc3de7061e427213583 | |
parent | f48a33be7db20db753ff06f430daf71ba31b0770 (diff) | |
parent | a93cbbd40c22821336953647e43ae5a8771b7886 (diff) | |
download | rails-c5bf53822f5887c9b235ceef388b1c8a977bcc88.tar.gz rails-c5bf53822f5887c9b235ceef388b1c8a977bcc88.tar.bz2 rails-c5bf53822f5887c9b235ceef388b1c8a977bcc88.zip |
Merge pull request #5033 from markmcspadden/inherited_headers
Allow ActiveResource subclasses to inherit headers from parent
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 6 | ||||
-rw-r--r-- | activeresource/test/cases/base_test.rb | 35 |
2 files changed, 41 insertions, 0 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 5ef50b6e03..0c2d070aef 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -588,6 +588,12 @@ module ActiveResource def headers @headers ||= {} + + if superclass != Object && superclass.headers + @headers = superclass.headers.merge(@headers) + else + @headers + end end attr_writer :element_name diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index f5a58793d1..33a6596602 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -437,6 +437,41 @@ class BaseTest < ActiveSupport::TestCase assert_not_equal(first_connection, second_connection, 'Connection should be re-created') end + def test_header_inheritance + fruit = Class.new(ActiveResource::Base) + apple = Class.new(fruit) + fruit.site = 'http://market' + + fruit.headers['key'] = 'value' + assert_equal 'value', apple.headers['key'] + end + + def test_header_inheritance_set_at_multiple_points + fruit = Class.new(ActiveResource::Base) + apple = Class.new(fruit) + fruit.site = 'http://market' + + fruit.headers['key'] = 'value' + assert_equal 'value', apple.headers['key'] + + apple.headers['key2'] = 'value2' + fruit.headers['key3'] = 'value3' + + assert_equal 'value', apple.headers['key'] + assert_equal 'value2', apple.headers['key2'] + assert_equal 'value3', apple.headers['key3'] + end + + def test_header_inheritance_should_not_leak_upstream + fruit = Class.new(ActiveResource::Base) + apple = Class.new(fruit) + fruit.site = 'http://market' + + fruit.headers['key'] = 'value' + + apple.headers['key2'] = 'value2' + assert_equal nil, fruit.headers['key2'] + end ######################################################################## # Tests for setting up remote URLs for a given model (including adding |