diff --git a/lib/active_force/active_query.rb b/lib/active_force/active_query.rb index ad00cca..cb54a7a 100644 --- a/lib/active_force/active_query.rb +++ b/lib/active_force/active_query.rb @@ -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) diff --git a/lib/active_force/query.rb b/lib/active_force/query.rb index aa0072e..fcaf35c 100644 --- a/lib/active_force/query.rb +++ b/lib/active_force/query.rb @@ -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? diff --git a/spec/active_force/active_query_spec.rb b/spec/active_force/active_query_spec.rb index 763e880..f35bb2c 100644 --- a/spec/active_force/active_query_spec.rb +++ b/spec/active_force/active_query_spec.rb @@ -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) @@ -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 diff --git a/spec/active_force/query_spec.rb b/spec/active_force/query_spec.rb index 0345ab7..0180186 100644 --- a/spec/active_force/query_spec.rb +++ b/spec/active_force/query_spec.rb @@ -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