Skip to content

Commit

Permalink
Don't query salesforce for a .first call if query already has recor…
Browse files Browse the repository at this point in the history
…ds (#77)

* Don't query salesforce for a `.first` call if query already has records

* clarify spec
  • Loading branch information
CharlesMassry authored Dec 19, 2023
1 parent ecb9115 commit 4ddfb81
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
.idea
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Not released

- Change `.first` to not query the API if records have already been retrieved (https://github.com/Beyond-Finance/active_force/pull/77)

## 0.20.1
- Revert "ActiveForce .first performance enhancement (#73)" (https://github.com/Beyond-Finance/active_force/pull/76)

Expand Down
4 changes: 4 additions & 0 deletions lib/active_force/active_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def limit limit
limit == 1 ? super.to_a.first : super
end

def first
super.to_a.first
end

def not args=nil, *rest
return self if args.nil?

Expand Down
16 changes: 12 additions & 4 deletions lib/active_force/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ def find id
end

def first
limit 1
if @records
clone_and_set_instance_variables(
size: 1,
records: [@records.first],
decorated_records: [@decorated_records&.first]
)
else
clone_and_set_instance_variables(size: 1)
end
end

def last(limit = 1)
Expand Down Expand Up @@ -126,9 +134,9 @@ def build_offset

def clone_and_set_instance_variables instance_variable_hash={}
clone = self.clone
clone.instance_variable_set(:@decorated_records, nil)
clone.instance_variable_set(:@records, nil)
instance_variable_hash.each { |k,v| clone.instance_variable_set("@#{k.to_s}", v) }
{ decorated_records: nil, records: nil }
.merge(instance_variable_hash)
.each { |k,v| clone.instance_variable_set("@#{k.to_s}", v) }
clone
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/active_force/active_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,22 @@
end

end

describe '#first' 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(double(:sobject, id: instance['Id']))
end
end

it 'returns a single record when the api was already queried' do
active_query.to_a # this will simulate the api call as to_a executes the query and populates the records
expect(active_query.first.id).to eq("0000000000AAAAABBB")
end

it 'returns a single record when the api was not already queried' do
expect(active_query.first.id).to eq("0000000000AAAAABBB")
end
end
end
16 changes: 16 additions & 0 deletions spec/active_force/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,22 @@
expect(query.to_s).to eq "SELECT Id, name, etc FROM table_name"
expect(new_query.to_s).to eq 'SELECT Id, name, etc FROM table_name LIMIT 1'
end

it "does not query if records have already been fetched" do
query = ActiveForce::Query.new 'table_name'
query.instance_variable_set(:@records, %w[foo bar])
query.instance_variable_set(:@decorated_records, %w[foo bar])
expect(query).not_to receive(:clone_and_set_instance_variables).with(size: 1)
expect(query).to receive(:clone_and_set_instance_variables).with(size: 1, records: ['foo'], decorated_records: ['foo'])
query.first
end

it 'queries the api if it has not been queried yet' do
query = ActiveForce::Query.new 'table_name'
query.instance_variable_set(:@records, nil)
expect(query).to receive(:clone_and_set_instance_variables).with(size: 1)
query.first
end
end

describe '.last' do
Expand Down

0 comments on commit 4ddfb81

Please sign in to comment.