Permalink
Browse files

Merge pull request #505 from haines/fix-extend

Avoid using extend in Decorator#==
  • Loading branch information...
2 parents dc430b3 + 205a0d4 commit 9e029ff6feb8d49cbf80454bdf081d5ea417d127 @steveklabnik steveklabnik committed Apr 1, 2013
Showing with 25 additions and 8 deletions.
  1. +15 −3 lib/draper/decoratable/equality.rb
  2. +1 −1 lib/draper/decorator.rb
  3. +9 −4 spec/draper/decorator_spec.rb
@@ -5,9 +5,21 @@ module Equality
#
# @return [Boolean]
def ==(other)
- super ||
- other.respond_to?(:decorated?) && other.decorated? &&
- other.respond_to?(:source) && self == other.source
+ super || Equality.test_for_decorator(self, other)
+ end
+
+ # Compares an object to a possibly-decorated object.
+ #
+ # @return [Boolean]
+ def self.test(object, other)
+ return object == other if object.is_a?(Decoratable)
+ object == other || test_for_decorator(object, other)
+ end
+
+ # @private
+ def self.test_for_decorator(object, other)
+ other.respond_to?(:decorated?) && other.decorated? &&
+ other.respond_to?(:source) && test(object, other.source)
end
end
end
View
@@ -163,7 +163,7 @@ def decorated?
#
# @return [Boolean]
def ==(other)
- source.extend(Draper::Decoratable::Equality) == other
+ Draper::Decoratable::Equality.test(source, other)
end
# Checks if `self.kind_of?(klass)` or `source.kind_of?(klass)`
@@ -380,13 +380,18 @@ module Draper
end
describe "#==" do
- it "ensures the source has a decoration-aware #==" do
+ it "works for a source that does not include Decoratable" do
source = Object.new
decorator = Decorator.new(source)
- expect(source).not_to be_a_kind_of Draper::Decoratable::Equality
- decorator == :something
- expect(source).to be_a_kind_of Draper::Decoratable::Equality
+ expect(decorator).to eq Decorator.new(source)
+ end
+
+ it "works for a multiply-decorated source that does not include Decoratable" do
+ source = Object.new
+ decorator = Decorator.new(source)
+
+ expect(decorator).to eq ProductDecorator.new(Decorator.new(source))
end
it "is true when source #== is true" do

0 comments on commit 9e029ff

Please sign in to comment.