1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

move fake ActiveRecord::Relation to helper, use #kind_of instead of comparing to string

This commit is contained in:
Joseph Hsu 2012-08-22 21:45:55 -04:00
parent 0f02556cc8
commit c46b0fd17d
4 changed files with 9 additions and 5 deletions

View file

@ -256,7 +256,7 @@ module Draper
if model.respond_to?(method) if model.respond_to?(method)
self.class.send :define_method, method do |*args, &blokk| self.class.send :define_method, method do |*args, &blokk|
result = model.send method, *args, &blokk result = model.send method, *args, &blokk
if result.class.name == "ActiveRecord::Relation" if defined?(ActiveRecord) && result.kind_of?(ActiveRecord::Relation)
self.class.new(model,self.options) self.class.new(model,self.options)
else else
result result
@ -275,7 +275,8 @@ module Draper
def self.method_missing(method, *args, &block) def self.method_missing(method, *args, &block)
model_result = model_class.send(method, *args, &block) model_result = model_class.send(method, *args, &block)
if model_result.is_a?(model_class) || model_result.class.name == 'ActiveRecord::Relation' if model_result.kind_of?(model_class) ||
(defined?(ActiveRecord) && model_result.kind_of?(ActiveRecord::Relation))
self.decorate(model_result, :context => args.dup.extract_options!) self.decorate(model_result, :context => args.dup.extract_options!)
else else
model_result model_result

View file

@ -28,7 +28,7 @@ module Draper
if @wrapped_collection.respond_to?(method) if @wrapped_collection.respond_to?(method)
self.class.send :define_method, method do |*args, &blokk| self.class.send :define_method, method do |*args, &blokk|
scoped_result = @wrapped_collection.send(method, *args, &block) scoped_result = @wrapped_collection.send(method, *args, &block)
if scoped_result.class.name == "ActiveRecord::Relation" if defined?(ActiveRecord) && scoped_result.kind_of?(ActiveRecord::Relation)
self.class.new(scoped_result, @klass, @options) self.class.new(scoped_result, @klass, @options)
else else
scoped_result scoped_result

View file

@ -51,7 +51,6 @@ describe Draper::Base do
end end
it "returns a Decorator when a scope is called on decorated object" do it "returns a Decorator when a scope is called on decorated object" do
class ActiveRecord::Relation; end
proxy = ProductDecorator.new(source) proxy = ProductDecorator.new(source)
klass = proxy.model.class klass = proxy.model.class
klass.class_eval { def some_scope ; ActiveRecord::Relation.new ; end } klass.class_eval { def some_scope ; ActiveRecord::Relation.new ; end }
@ -59,7 +58,6 @@ describe Draper::Base do
end end
it "returns a Decorator when a scope is called on the decorator" do it "returns a Decorator when a scope is called on the decorator" do
class ActiveRecord::Relation; end
proxy = ProductDecorator proxy = ProductDecorator
klass = source.class klass = source.class
klass.class_eval { def self.some_scope ; ActiveRecord::Relation.new ; end } klass.class_eval { def self.some_scope ; ActiveRecord::Relation.new ; end }

View file

@ -34,3 +34,8 @@ module ActionController
Draper::System.setup(self) Draper::System.setup(self)
end end
end end
module ActiveRecord
class Relation
end
end