mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
Merge pull request #189 from cheald/master
Fix accidental clobbering of special ActiveModel methods
This commit is contained in:
commit
21d192b4d1
6 changed files with 35 additions and 5 deletions
6
lib/draper/active_model_support.rb
Normal file → Executable file
6
lib/draper/active_model_support.rb
Normal file → Executable file
|
@ -13,8 +13,10 @@ module Draper::ActiveModelSupport
|
||||||
class << base
|
class << base
|
||||||
self
|
self
|
||||||
end.class_eval do
|
end.class_eval do
|
||||||
send(:define_method, method_name) do |*args, &block|
|
if !base.class.instance_methods.include?(method_name) || base.class.instance_method(method_name).owner === Draper::Base
|
||||||
model.send(method_name, *args, &block)
|
send(:define_method, method_name) do |*args, &block|
|
||||||
|
model.send(method_name, *args, &block)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
0
lib/draper/base.rb
Normal file → Executable file
0
lib/draper/base.rb
Normal file → Executable file
20
spec/draper/base_spec.rb
Normal file → Executable file
20
spec/draper/base_spec.rb
Normal file → Executable file
|
@ -215,19 +215,33 @@ describe Draper::Base do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when an ActiveModel descendant" do
|
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)
|
source.stub(:to_param).and_return(1)
|
||||||
Draper::Base.new(source).to_param.should == 1
|
Draper::Base.new(source).to_param.should == 1
|
||||||
end
|
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)
|
source.stub(:id).and_return(123456789)
|
||||||
Draper::Base.new(source).id.should == 123456789
|
Draper::Base.new(source).id.should == 123456789
|
||||||
end
|
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
|
Draper::Base.new(source).errors.should be_an_instance_of ActiveModel::Errors
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context "when not an ActiveModel descendant" do
|
context "when not an ActiveModel descendant" do
|
||||||
|
|
1
spec/spec_helper.rb
Normal file → Executable file
1
spec/spec_helper.rb
Normal file → Executable 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_multiple_allows'
|
||||||
require './spec/support/samples/decorator_with_application_helper'
|
require './spec/support/samples/decorator_with_application_helper'
|
||||||
require './spec/support/samples/decorator_with_denies'
|
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'
|
||||||
require './spec/support/samples/namespaced_product_decorator'
|
require './spec/support/samples/namespaced_product_decorator'
|
||||||
require './spec/support/samples/non_active_model_product'
|
require './spec/support/samples/non_active_model_product'
|
||||||
|
|
0
spec/support/samples/decorator_with_allows.rb
Normal file → Executable file
0
spec/support/samples/decorator_with_allows.rb
Normal file → Executable file
13
spec/support/samples/decorator_with_special_methods.rb
Executable file
13
spec/support/samples/decorator_with_special_methods.rb
Executable file
|
@ -0,0 +1,13 @@
|
||||||
|
class DecoratorWithSpecialMethods < Draper::Base
|
||||||
|
def to_param
|
||||||
|
"foo"
|
||||||
|
end
|
||||||
|
|
||||||
|
def id
|
||||||
|
1337
|
||||||
|
end
|
||||||
|
|
||||||
|
def errors
|
||||||
|
["omg errors!"]
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue