Permalink
Browse files

Add === support

Closes #341
  • Loading branch information...
1 parent c94479a commit ca116154f37f01faf4d04dcb916b1bca07c64a74 @haines haines committed Nov 15, 2012
Showing with 137 additions and 3 deletions.
  1. +8 −0 lib/draper/decoratable.rb
  2. +69 −0 spec/draper/decoratable_spec.rb
  3. +60 −3 spec/draper/decorator_spec.rb
@@ -21,6 +21,10 @@ def decorated?
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 @@ def decorator_class
rescue NameError
raise Draper::UninferrableDecoratorError.new(self)
end
+
+ def ===(other)
+ super || (other.respond_to?(:source) && super(other.source))
+ end
end
end
@@ -45,6 +45,75 @@
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])
@@ -219,9 +219,66 @@
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

0 comments on commit ca11615

Please sign in to comment.