Bugfix: do not confuse Kernel#Array

On 1.9.2 (and maybe earlier) Kernel#Array will choke on a decorator,
because it just invokes `to_ary` and `to_a` without using `respond_to?`
first, like it does in 1.9.3-p125.

As a work-around, make sure to not create a delegation method in
`method_missing` when the model will not respond to it anyway. The
decorator object will then behave like `Kernel#Array` expects it to.

See #115.
This commit is contained in:
Arjan van der Gaag 2012-03-12 13:52:23 +01:00
parent 279ea0202d
commit f6c2acf3f4
2 changed files with 10 additions and 4 deletions

View File

@ -204,11 +204,13 @@ module Draper
def method_missing(method, *args, &block)
super unless allow?(method)
self.class.send :define_method, method do |*args, &block|
model.send method, *args, &block
end
if model.respond_to?(method)
self.class.send :define_method, method do |*args, &block|
model.send method, *args, &block
end
send method, *args, &block
send method, *args, &block
end
rescue NoMethodError => no_method_error
super if no_method_error.name == method

View File

@ -622,6 +622,10 @@ describe Draper::Base do
end
context "when the delegated method calls a non-existant method" do
it 'should not try to delegate to non-existant methods to not confuse Kernel#Array' do
Array(subject).should be_kind_of(Array)
end
it "raises the correct NoMethodError" do
begin
subject.some_action