From 9bce773cd0d88ba7bf1dfc2fdf2b9c851070e176 Mon Sep 17 00:00:00 2001 From: Joshua Flack Date: Wed, 11 Sep 2024 16:40:37 -0400 Subject: [PATCH 1/3] update select method --- lib/active_force/active_query.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/active_force/active_query.rb b/lib/active_force/active_query.rb index cb54a7a..401da3a 100644 --- a/lib/active_force/active_query.rb +++ b/lib/active_force/active_query.rb @@ -78,9 +78,17 @@ def where args=nil, *rest super build_condition args, rest end - def select *selected_fields - selected_fields.map! { |field| mappings[field] } - super *selected_fields + def select *selected_fields, &block + if block + result = [] + self.each do |record| + result << record if block.call(record) + end + result + else + selected_fields.map! { |field| mappings[field] } + super *selected_fields + end end def ids From e0bec2f1e73d042156d5e6414e001bf7c5239e4a Mon Sep 17 00:00:00 2001 From: Joshua Flack Date: Thu, 12 Sep 2024 10:38:11 -0400 Subject: [PATCH 2/3] fix typos, update select spec --- spec/active_force/active_query_spec.rb | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/spec/active_force/active_query_spec.rb b/spec/active_force/active_query_spec.rb index f35bb2c..05031df 100644 --- a/spec/active_force/active_query_spec.rb +++ b/spec/active_force/active_query_spec.rb @@ -96,24 +96,33 @@ def self.decorate(records) context 'when there are no records' do let(:api_result) { [] } - it 'returns true' do + it 'returns false' do result = active_query.where("Text_Label = 'foo'").any? expect(result).to be false end end context 'when records are returned' do - it 'returns false' do + it 'returns true' do result = active_query.where("Text_Label = 'foo'").any? expect(result).to be true end end end - describe "select only some field using mappings" do - it "should return a query only with selected field" do - new_query = active_query.select(:field) - expect(new_query.to_s).to eq("SELECT Field__c FROM table_name") + describe "#select" do + context "when passed a block" do + it "should return an array of records" do + result = active_query.select { |record| record.id == '123' } + expect(result).to be_a Array + end + end + + context "when passed one or more fields" do + it "should return a query only with selected fields" do + new_query = active_query.select(:field) + expect(new_query.to_s).to eq("SELECT Field__c FROM table_name") + end end end From c1efdc792c356dace586323ed253f10700395ad7 Mon Sep 17 00:00:00 2001 From: Joshua Flack Date: Thu, 12 Sep 2024 11:12:47 -0400 Subject: [PATCH 3/3] beef up the spec a little bit --- spec/active_force/active_query_spec.rb | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/spec/active_force/active_query_spec.rb b/spec/active_force/active_query_spec.rb index 05031df..6604972 100644 --- a/spec/active_force/active_query_spec.rb +++ b/spec/active_force/active_query_spec.rb @@ -112,9 +112,24 @@ def self.decorate(records) describe "#select" do context "when passed a block" do - it "should return an array of records" do - result = active_query.select { |record| record.id == '123' } - expect(result).to be_a Array + before do + allow(client).to receive(:query).and_return(api_result) + end + + context "when records satisfy the block conditions" do + it "should return an array of records" do + result = active_query.select { |record| record == record } + expect(result).to be_a Array + expect(result.size).to eq 2 + end + end + + context "when records do not satisfy the block conditions" do + it "should return an empty array" do + result = active_query.select { |record| record != record } + expect(result).to be_a Array + expect(result.size).to eq 0 + end end end