If special ActiveModel methods like #id, #errors, and #to_param are defined directly on the decorator, do not redefine them as proxy methods.

This commit is contained in:
Chris Heald 2012-05-10 17:50:22 -07:00
parent 3356b5ffe7
commit eea4c866a6
5 changed files with 22 additions and 5 deletions

6
lib/draper/active_model_support.rb Normal file → Executable file
View File

@ -13,8 +13,10 @@ module Draper::ActiveModelSupport
class << base
self
end.class_eval do
send(:define_method, method_name) do |*args, &block|
model.send(method_name, *args, &block)
if !base.class.instance_methods.include?(method_name) || base.class.instance_method(method_name).owner === Draper::Base
send(:define_method, method_name) do |*args, &block|
model.send(method_name, *args, &block)
end
end
end
end

0
lib/draper/base.rb Normal file → Executable file
View File

20
spec/draper/base_spec.rb Normal file → Executable file
View File

@ -215,19 +215,33 @@ describe Draper::Base do
end
context "when an ActiveModel descendant" do
it "should always proxy to_param" do
it "should always proxy to_param if it is not defined on the decorator itself" do
source.stub(:to_param).and_return(1)
Draper::Base.new(source).to_param.should == 1
end
it "should always proxy id" do
it "should always proxy id if it is not defined on the decorator itself" do
source.stub(:id).and_return(123456789)
Draper::Base.new(source).id.should == 123456789
end
it "should always proxy errors" do
it "should always proxy errors if it is not defined on the decorator itself" do
Draper::Base.new(source).errors.should be_an_instance_of ActiveModel::Errors
end
it "should never proxy to_param if it is defined on the decorator itself" do
source.stub(:to_param).and_return(1)
DecoratorWithSpecialMethods.new(source).to_param.should == "foo"
end
it "should never proxy id if it is defined on the decorator itself" do
source.stub(:id).and_return(123456789)
DecoratorWithSpecialMethods.new(source).id.should == 1337
end
it "should never proxy errors if it is defined on the decorator itself" do
DecoratorWithSpecialMethods.new(source).errors.should be_an_instance_of Array
end
end
context "when not an ActiveModel descendant" do

1
spec/spec_helper.rb Normal file → Executable file
View File

@ -14,6 +14,7 @@ require './spec/support/samples/decorator_with_allows'
require './spec/support/samples/decorator_with_multiple_allows'
require './spec/support/samples/decorator_with_application_helper'
require './spec/support/samples/decorator_with_denies'
require './spec/support/samples/decorator_with_special_methods'
require './spec/support/samples/namespaced_product'
require './spec/support/samples/namespaced_product_decorator'
require './spec/support/samples/non_active_model_product'

0
spec/support/samples/decorator_with_allows.rb Normal file → Executable file
View File