Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ActiveQuery#select to accept a block #99

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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