From 661c1a9f0fd3e47a3f0c610ccc6d385ec8bbed9e Mon Sep 17 00:00:00 2001 From: Douglas Lutz Date: Fri, 3 Jan 2020 18:17:34 -0300 Subject: [PATCH] Add preservation of decorator options on QueryMethods (#868) --- lib/draper/query_methods.rb | 2 +- spec/draper/query_methods_spec.rb | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/draper/query_methods.rb b/lib/draper/query_methods.rb index 267ee8a..59c42bb 100644 --- a/lib/draper/query_methods.rb +++ b/lib/draper/query_methods.rb @@ -6,7 +6,7 @@ module Draper def method_missing(method, *args, &block) return super unless strategy.allowed? method - object.send(method, *args, &block).decorate + object.send(method, *args, &block).decorate(with: decorator_class, context: context) end def respond_to_missing?(method, include_private = false) diff --git a/spec/draper/query_methods_spec.rb b/spec/draper/query_methods_spec.rb index 3884d34..7849654 100644 --- a/spec/draper/query_methods_spec.rb +++ b/spec/draper/query_methods_spec.rb @@ -11,7 +11,8 @@ module Draper describe '#method_missing' do let(:collection) { [ Post.new, Post.new ] } - let(:collection_decorator) { PostDecorator.decorate_collection(collection) } + let(:collection_context) { { user: 'foo' } } + let(:collection_decorator) { PostDecorator.decorate_collection(collection, context: collection_context) } context 'when strategy allows collection to call the method' do let(:results) { spy(:results) } @@ -26,6 +27,12 @@ module Draper expect(results).to have_received(:decorate) end + + it 'calls the method on the collection and keeps the decoration options' do + collection_decorator.some_query_method + + expect(results).to have_received(:decorate).with({ context: collection_context, with: PostDecorator }) + end end context 'when strategy does not allow collection to call the method' do