Skip to content

Commit

Permalink
Return new ActiveQuery object when current ActiveQuery object already… (
Browse files Browse the repository at this point in the history
#7)

* Return new ActiveQuery object when current ActiveQuery object already has records and a new query is called.

* update Changelog.
  • Loading branch information
asedge authored Mar 2, 2023
1 parent 41f2019 commit dc9439d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## Not released
* Fix `where` chaining on `ActiveQuery` (https://github.com/Beyond-Finance/active_force/pull/7)

## 0.9.1
* Fix invalid error class (https://github.com/Beyond-Finance/active_force/pull/6)

## 0.9.0

Expand Down
8 changes: 8 additions & 0 deletions lib/active_force/active_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def limit limit

def where args=nil, *rest
return self if args.nil?
return clone_self_and_clear_cache.where(args, *rest) if @decorated_records.present?
super build_condition args, rest
self
end
Expand Down Expand Up @@ -156,5 +157,12 @@ def quote_string(s)
def result
sfdc_client.query(self.to_s)
end

def clone_self_and_clear_cache
new_query = self.clone
new_query.instance_variable_set(:@decorated_records, nil)
new_query.instance_variable_set(:@records, nil)
new_query
end
end
end
56 changes: 43 additions & 13 deletions spec/active_force/active_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
end
let(:mappings){ { id: "Id", field: "Field__c", other_field: "Other_Field" } }
let(:client){ double("client") }
let(:active_query){ ActiveForce::ActiveQuery.new(sobject) }
let(:active_query){ described_class.new(sobject) }
let(:api_result) do
[
{"Id" => "0000000000AAAAABBB"},
{"Id" => "0000000000CCCCCDDD"}
]
end


before do
allow(active_query).to receive(:sfdc_client).and_return client
Expand All @@ -22,23 +29,11 @@
expect(client).to receive(:query).and_return(api_result)
end

let(:api_result) {
[
{"Id" => "0000000000AAAAABBB"},
{"Id" => "0000000000CCCCCDDD"}
]
}

it "should return an array of objects" do
result = active_query.where("Text_Label = 'foo'").to_a
expect(result).to be_a Array
end

it "should allow to chain query methods" do
result = active_query.where("Text_Label = 'foo'").where("Checkbox_Label = true").to_a
expect(result).to be_a Array
end

it "should decorate the array of objects" do
expect(sobject).to receive(:decorate)
active_query.where("Text_Label = 'foo'").to_a
Expand Down Expand Up @@ -132,6 +127,41 @@
end
end

describe '#where' do
before do
allow(client).to receive(:query).with("SELECT Id FROM table_name WHERE (Text_Label = 'foo')").and_return(api_result1)
allow(client).to receive(:query).with("SELECT Id FROM table_name WHERE (Text_Label = 'foo') AND (Checkbox_Label = true)").and_return(api_result2)
end
let(:api_result1) do
[
{"Id" => "0000000000AAAAABBB"},
{"Id" => "0000000000CCCCCDDD"},
{"Id" => "0000000000EEEEEFFF"}
]
end
let(:api_result2) do
[
{"Id" => "0000000000EEEEEFFF"}
]
end
it 'allows method chaining' do
result = active_query.where("Text_Label = 'foo'").where("Checkbox_Label = true")
expect(result).to be_a described_class
end

context 'when calling `where` on an ActiveQuery object that already has records' do
it 'returns a new ActiveQuery object' do
first_active_query = active_query.where("Text_Label = 'foo'")
first_active_query.inspect # so the query is executed
second_active_query = first_active_query.where("Checkbox_Label = true")
second_active_query.inspect
expect(second_active_query).to be_a described_class
expect(second_active_query).not_to eq first_active_query
end
end

end

describe "#find_by" do
it "should query the client, with the SFDC field names and correctly enclosed values" do
expect(client).to receive :query
Expand Down

0 comments on commit dc9439d

Please sign in to comment.