diff --git a/lib/draper/decoratable.rb b/lib/draper/decoratable.rb index 9e62d11..7b5e414 100644 --- a/lib/draper/decoratable.rb +++ b/lib/draper/decoratable.rb @@ -21,6 +21,10 @@ module Draper::Decoratable false end + def ===(other) + super || (other.respond_to?(:source) && super(other.source)) + end + module ClassMethods def decorate(options = {}) decorator_class.decorate_collection(self.scoped, options) @@ -32,5 +36,9 @@ module Draper::Decoratable rescue NameError raise Draper::UninferrableDecoratorError.new(self) end + + def ===(other) + super || (other.respond_to?(:source) && super(other.source)) + end end end diff --git a/spec/draper/decoratable_spec.rb b/spec/draper/decoratable_spec.rb index 027daaa..a894f60 100644 --- a/spec/draper/decoratable_spec.rb +++ b/spec/draper/decoratable_spec.rb @@ -45,6 +45,75 @@ describe Draper::Decoratable do end end + describe "#===" do + context "with itself" do + it "returns true" do + (subject === subject).should be_true + end + end + + context "with another instance" do + it "returns false" do + (subject === Product.new).should be_false + end + end + + context "with a decorated version of itself" do + it "returns true" do + decorator = double(source: subject) + (subject === decorator).should be_true + end + end + + context "with a decorated other instance" do + it "returns false" do + decorator = double(source: Product.new) + (subject === decorator).should be_false + end + end + end + + describe ".====" do + context "with an instance" do + it "returns true" do + (Product === Product.new).should be_true + end + end + + context "with a derived instance" do + it "returns true" do + (Product === Widget.new).should be_true + end + end + + context "with an unrelated instance" do + it "returns false" do + (Product === Object.new).should be_false + end + end + + context "with a decorated instance" do + it "returns true" do + decorator = double(source: Product.new) + (Product === decorator).should be_true + end + end + + context "with a decorated derived instance" do + it "returns true" do + decorator = double(source: Widget.new) + (Product === decorator).should be_true + end + end + + context "with a decorated unrelated instance" do + it "returns false" do + decorator = double(source: Object.new) + (Product === decorator).should be_false + end + end + end + describe ".decorate" do it "returns a collection decorator" do Product.stub(:scoped).and_return([Product.new]) diff --git a/spec/draper/decorator_spec.rb b/spec/draper/decorator_spec.rb index 218c19f..4f88319 100755 --- a/spec/draper/decorator_spec.rb +++ b/spec/draper/decorator_spec.rb @@ -219,9 +219,66 @@ describe Draper::Decorator do end describe "#==" do - it "compares the decorated models" do - other = Draper::Decorator.new(source) - subject.should == other + context "with itself" do + it "returns true" do + (subject == subject).should be_true + end + end + + context "with another decorator having the same source" do + it "returns true" do + (subject == ProductDecorator.new(source)).should be_true + end + end + + context "with another decorator having a different source" do + it "returns false" do + (subject == ProductDecorator.new(Object.new)).should be_false + end + end + + context "with the source object" do + it "returns true" do + (subject == source).should be_true + end + end + + context "with another object" do + it "returns false" do + (subject == Object.new).should be_false + end + end + end + + describe "#===" do + context "with itself" do + it "returns true" do + (subject === subject).should be_true + end + end + + context "with another decorator having the same source" do + it "returns true" do + (subject === ProductDecorator.new(source)).should be_true + end + end + + context "with another decorator having a different source" do + it "returns false" do + (subject === ProductDecorator.new(Object.new)).should be_false + end + end + + context "with the source object" do + it "returns true" do + (subject === source).should be_true + end + end + + context "with another object" do + it "returns false" do + (subject === Object.new).should be_false + end end end