Merge pull request #343 from haines/threequals

Add === support
This commit is contained in:
Steve Klabnik 2012-11-15 16:55:34 -08:00
commit 970f3a3b67
3 changed files with 137 additions and 3 deletions

View File

@ -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

View File

@ -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])

View File

@ -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