Skip to content

Commit

Permalink
Merge pull request drapergem#505 from haines/fix-extend
Browse files Browse the repository at this point in the history
Avoid using extend in Decorator#==
  • Loading branch information
steveklabnik committed Apr 1, 2013
2 parents dc430b3 + 205a0d4 commit 9e029ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
18 changes: 15 additions & 3 deletions lib/draper/decoratable/equality.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/draper/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
13 changes: 9 additions & 4 deletions spec/draper/decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9e029ff

Please sign in to comment.