1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

Simplify CollectionDecorator#find

This commit is contained in:
Andrew Haines 2012-10-31 23:52:55 +00:00
parent f21b92cda4
commit ecd5b06274
2 changed files with 5 additions and 8 deletions

View file

@ -1,4 +1,3 @@
require 'active_support/core_ext/object/blank'
module Draper module Draper
class CollectionDecorator class CollectionDecorator
include Enumerable include Enumerable
@ -28,13 +27,11 @@ module Draper
end end
alias_method :to_ary, :decorated_collection alias_method :to_ary, :decorated_collection
def find(ifnone_or_id = nil, &blk) def find(*args, &block)
if block_given? if block_given?
decorated_collection.find(ifnone_or_id, &blk) decorated_collection.find(*args, &block)
else else
obj = decorated_collection.first decorator_class.find(*args)
return nil if obj.blank?
obj.class.find(ifnone_or_id)
end end
end end

View file

@ -31,8 +31,8 @@ describe Draper::CollectionDecorator do
context "without a block" do context "without a block" do
it "decorates Model.find" do it "decorates Model.find" do
source.should_not_receive(:find) source.should_not_receive(:find)
Product.should_receive(:find).with(1) Product.should_receive(:find).with(1).and_return(:product)
subject.find(1) subject.find(1).should == ProductDecorator.new(:product)
end end
end end
end end