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

Return new ActiveQuery object when current ActiveQuery object already… #7

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
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
asedge marked this conversation as resolved.
Show resolved Hide resolved
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