Revert "Wrap relation calls and scope calls in decorators"

This reverts commit e4595d9341.

Fixes #267

Conflicts:

	lib/draper/base.rb
	lib/draper/decorated_enumerable_proxy.rb
	spec/draper/base_spec.rb
This commit is contained in:
Steve Klabnik 2012-08-31 09:28:53 -07:00
parent 3a01322281
commit 044a6e6519
3 changed files with 7 additions and 56 deletions

View File

@ -255,12 +255,7 @@ module Draper
if model.respond_to?(method)
self.class.send :define_method, method do |*args, &blokk|
result = model.send method, *args, &blokk
if defined?(ActiveRecord) && result.kind_of?(ActiveRecord::Relation)
self.class.new(model,self.options)
else
result
end
model.send method, *args, &blokk
end
send method, *args, &block
@ -274,12 +269,10 @@ module Draper
end
def self.method_missing(method, *args, &block)
model_result = model_class.send(method, *args, &block)
if model_result.kind_of?(model_class) ||
(defined?(ActiveRecord) && model_result.kind_of?(ActiveRecord::Relation))
self.decorate(model_result, :context => args.dup.extract_options!)
if method.to_s.match(/^find_((all_|last_)?by_|or_(initialize|create)_by_).*/)
self.decorate(model_class.send(method, *args, &block), :context => args.dup.extract_options!)
else
model_result
model_class.send(method, *args, &block)
end
end

View File

@ -44,24 +44,7 @@ module Draper
end
def method_missing (method, *args, &block)
if @wrapped_collection.respond_to?(method)
self.class.send :define_method, method do |*args, &blokk|
scoped_result = @wrapped_collection.send(method, *args, &block)
if defined?(ActiveRecord) && scoped_result.kind_of?(ActiveRecord::Relation)
self.class.new(scoped_result, @klass, @options)
else
scoped_result
end
end
send method, *args, &block
else
super
end
rescue NoMethodError => no_method_error
super if no_method_error.name == method
raise no_method_error
@wrapped_collection.send(method, *args, &block)
end
def respond_to?(method, include_private = false)

View File

@ -50,20 +50,6 @@ describe Draper::Base do
ProductDecorator.new(product_decorator).model.should be_instance_of Product
end
it "returns a Decorator when a scope is called on decorated object" do
proxy = ProductDecorator.new(source)
klass = proxy.model.class
klass.class_eval { def some_scope ; ActiveRecord::Relation.new ; end }
proxy.some_scope.should be_instance_of(proxy.class)
end
it "returns a Decorator when a scope is called on the decorator" do
proxy = ProductDecorator
klass = source.class
klass.class_eval { def self.some_scope ; ActiveRecord::Relation.new ; end }
proxy.some_scope.should be_instance_of(proxy)
end
it "handle plural-like words properly'" do
class Business; end
expect do
@ -176,18 +162,6 @@ describe Draper::Base do
end
end
context "with a scope applied after decoration" do
it "returns a DecoratedEnumerableProxy when a scope is called" do
class ActiveRecord::Relation
def some_scope; self ;end
end
klass = subject.model.class
klass.class_eval { def self.some_scope ; ActiveRecord::Relation.new ; end }
proxy = Draper::DecoratedEnumerableProxy
proxy.new(ActiveRecord::Relation.new, klass).some_scope.should be_instance_of(proxy)
end
end
context "for a polymorphic association" do
before(:each){ subject.class_eval{ decorates_association :thing, :polymorphic => true } }
it "causes the association to be decorated with the right decorator" do
@ -378,7 +352,8 @@ describe Draper::Base do
it "uses the options hash in the decorator instantiation" do
Product.should_receive(:find_by_name_and_size).with("apples", "large", {:role => :admin})
ProductDecorator.find_by_name_and_size("apples", "large", {:role => :admin})
pd = ProductDecorator.find_by_name_and_size("apples", "large", {:role => :admin})
pd.context[:role].should == :admin
end
end