From b582c65dd613e3c5e8783385485d08516b0ee0f2 Mon Sep 17 00:00:00 2001 From: Phanindra Srungavarapu Date: Fri, 27 Sep 2024 10:11:22 +0530 Subject: [PATCH] chore: if scope is empty no need to do more SQLs we can directly return blank array --- lib/pact_broker/pacticipants/repository.rb | 5 ++++- spec/lib/pact_broker/pacticipants/repository_spec.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/pact_broker/pacticipants/repository.rb b/lib/pact_broker/pacticipants/repository.rb index 26f0e74e8..f836cee0e 100644 --- a/lib/pact_broker/pacticipants/repository.rb +++ b/lib/pact_broker/pacticipants/repository.rb @@ -37,7 +37,10 @@ def find_all(options = {}, pagination_options = {}, eager_load_associations = [] end def find(options = {}, pagination_options = {}, eager_load_associations = []) - query = scope_for(PactBroker::Domain::Pacticipant).select_all_qualified + query = scope_for(PactBroker::Domain::Pacticipant) + return [] if query.empty? + + query = query.select_all_qualified query = query.filter(:name, options[:query_string]) if options[:query_string] query = query.label(options[:label_name]) if options[:label_name] query.order_ignore_case(Sequel[:pacticipants][:name]).eager(*eager_load_associations).all_with_pagination_options(pagination_options) diff --git a/spec/lib/pact_broker/pacticipants/repository_spec.rb b/spec/lib/pact_broker/pacticipants/repository_spec.rb index 7509c479f..2538c7633 100644 --- a/spec/lib/pact_broker/pacticipants/repository_spec.rb +++ b/spec/lib/pact_broker/pacticipants/repository_spec.rb @@ -110,6 +110,18 @@ module Pacticipants expect(subject.collect(&:name)).to eq ["Wiffle"] end end + + context "when scope applied" do + it "returns the pacticipants if scope allows" do + allow_any_instance_of(Repository).to receive(:scope_for).and_return(PactBroker::Domain::Pacticipant) # default, with no scope applied + expect(subject.collect(&:name)).to include(*["Bar", "Foo"]) + end + + it "returns blank array if scope does not allow" do + allow_any_instance_of(Repository).to receive(:scope_for).and_return([]) + expect(subject).to eq [] + end + end end describe "#find_by_name" do