Skip to content

Commit

Permalink
Fixes #94 - Ids Query Method (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdelpone-beyond-finance authored Jul 17, 2024
1 parent 205e6ff commit 5d1689f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/active_force/active_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def select *selected_fields
end

def ids
clone_and_set_instance_variables(query_fields: ["Id"])
super.pluck(:id)
end

def find!(id)
Expand Down
4 changes: 4 additions & 0 deletions lib/active_force/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def sum field
clone_and_set_instance_variables(query_fields: ["sum(#{field})"])
end

def ids
clone_and_set_instance_variables(query_fields: ["Id"])
end

protected
def and_conditions
"(#{@conditions.join(') AND (')})" unless @conditions.empty?
Expand Down
20 changes: 14 additions & 6 deletions spec/active_force/active_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ def self.decorate(records)
end
end

describe '#ids' do
it 'returns a query that selects only the Id field' do
expect(active_query.where(field: 123).ids.to_s).to eq "SELECT Id FROM table_name WHERE (Field__c = 123)"
end
end

describe "condition mapping" do
it "maps conditions for a .where" do
new_query = active_query.where(field: 123)
Expand Down Expand Up @@ -577,4 +571,18 @@ def check_ranges(base_query, start, finish, &format_block)
expect(active_query.first.id).to eq("0000000000AAAAABBB")
end
end

describe "#ids" do
before do
allow(client).to receive(:query).and_return(api_result)
api_result.each do |instance|
allow(active_query).to receive(:build).with(instance, {}).and_return(build_restforce_sobject(id: instance['Id']))
end
end

it "should return an array of id strings" do
expect(active_query.ids).to be_a Array
expect(active_query.ids).to eq api_result.map { |r| r['Id'] }
end
end
end
10 changes: 10 additions & 0 deletions spec/active_force/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,14 @@
expect(query.where("name = 'cool'").sum(:field1).to_s).to eq "SELECT sum(field1) FROM table_name WHERE (name = 'cool')"
end
end

describe ".ids" do
it "should return the query for selecting Id" do
expect(query.ids.to_s).to eq 'SELECT Id FROM table_name'
end

it "should work with a condition" do
expect(query.where("name = 'cool'").ids.to_s).to eq "SELECT Id FROM table_name WHERE (name = 'cool')"
end
end
end

0 comments on commit 5d1689f

Please sign in to comment.