From 5b5c9f45d9ad6914305300cac738da646925ce92 Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Mon, 15 Apr 2013 22:46:19 +0100 Subject: [PATCH] Return self from CollectionDecorator#replace Closes #516 --- lib/draper/collection_decorator.rb | 5 +++++ spec/draper/collection_decorator_spec.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/draper/collection_decorator.rb b/lib/draper/collection_decorator.rb index e38df46..03d5f31 100644 --- a/lib/draper/collection_decorator.rb +++ b/lib/draper/collection_decorator.rb @@ -74,6 +74,11 @@ module Draper end alias_method :is_a?, :kind_of? + def replace(other) + decorated_collection.replace(other) + self + end + protected # @return the collection being decorated. diff --git a/spec/draper/collection_decorator_spec.rb b/spec/draper/collection_decorator_spec.rb index 7ab7832..30783f4 100644 --- a/spec/draper/collection_decorator_spec.rb +++ b/spec/draper/collection_decorator_spec.rb @@ -278,5 +278,21 @@ module Draper end end + describe "#replace" do + it "replaces the decorated collection" do + decorator = CollectionDecorator.new([Product.new]) + replacement = [:foo, :bar] + + decorator.replace replacement + expect(decorator).to match_array replacement + end + + it "returns itself" do + decorator = CollectionDecorator.new([Product.new]) + + expect(decorator.replace([:foo, :bar])).to be decorator + end + end + end end