Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
move fake ActiveRecord::Relation to helper, use #kind_of instead of c…
…omparing to string
  • Loading branch information
jhsu committed Aug 23, 2012
1 parent 0f02556 commit c46b0fd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/draper/base.rb
Expand Up @@ -256,7 +256,7 @@ def method_missing(method, *args, &block)
if model.respond_to?(method)
self.class.send :define_method, method do |*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)
else
result
Expand All @@ -275,7 +275,8 @@ def method_missing(method, *args, &block)

def self.method_missing(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!)
else
model_result
Expand Down
2 changes: 1 addition & 1 deletion lib/draper/decorated_enumerable_proxy.rb
Expand Up @@ -28,7 +28,7 @@ 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 scoped_result.class.name == "ActiveRecord::Relation"
if defined?(ActiveRecord) && scoped_result.kind_of?(ActiveRecord::Relation)
self.class.new(scoped_result, @klass, @options)
else
scoped_result
Expand Down
2 changes: 0 additions & 2 deletions spec/draper/base_spec.rb
Expand Up @@ -51,15 +51,13 @@
end

it "returns a Decorator when a scope is called on decorated object" do
class ActiveRecord::Relation; end
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
class ActiveRecord::Relation; end
proxy = ProductDecorator
klass = source.class
klass.class_eval { def self.some_scope ; ActiveRecord::Relation.new ; end }
Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -34,3 +34,8 @@ class Base
Draper::System.setup(self)
end
end

module ActiveRecord
class Relation
end
end

0 comments on commit c46b0fd

Please sign in to comment.