Permalink
Browse files

Merge pull request #343 from haines/threequals

Add === support
  • Loading branch information...
2 parents dca010c + ca11615 commit 970f3a3b67bd6a69d2cb7b24ba90489845008476 @steveklabnik steveklabnik committed Nov 16, 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
View
8 lib/draper/decoratable.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
View
69 spec/draper/decoratable_spec.rb
@@ -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])
View
63 spec/draper/decorator_spec.rb
@@ -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

3 comments on commit 970f3a3

@steveklabnik
drapergem member

I wonder why we get weird failures like https://travis-ci.org/drapergem/draper/jobs/3221337 now. @haines, any ideas?

@haines
drapergem member

Ugh, not repeatable and not a very helpful failure message either. It must have been an error spinning up the Rails server or something.

I'll write a better RSpec matcher for the integration tests, so it at least spits out the page's html on failure.

I was thinking that my approach of using a headless browser is kinda overkill since we don't need to interact with the page to verify that the html is produced correctly. Just GETing it should be sufficient (hopefully one less thing to go wrong), and then it would work on current Rubinius as well. Except, it didn't. In that case again I think a more descriptive failure would help - maybe it was a similar problem. I'll look into it.

In general, though, I've found JRuby has been a pain with some of these integration tests, I was trying to write some for the console:

output = `echo "Post.find(1).decorate.url_with_model" | bundle exec rails console`
# or even
output = IO.popen("bundle exec rails console", "r+") do |console|
  console.puts "Post.find(1).decorate.url_with_model"
  console.puts "quit"
  console.read
end

but while both of these work on MRI, I had no end of trouble getting them to work on JRuby - and then when I finally got something working locally, it didn't work on Travis.

@steveklabnik
drapergem member

:/ I do think you're right, though, we shouldn't need the full browser.

Please sign in to comment.