Skip to content

Commit

Permalink
Update ActiveQuery#select to accept a block (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
jflack-bf authored Sep 13, 2024
1 parent b36f268 commit 8619a99
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
14 changes: 11 additions & 3 deletions lib/active_force/active_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 30 additions & 6 deletions spec/active_force/active_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,48 @@ 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
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

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

Expand Down

0 comments on commit 8619a99

Please sign in to comment.