Skip to content

Commit

Permalink
ActiveForce .first performance enhancement (#73)
Browse files Browse the repository at this point in the history
* Don't query salesforce for a `.first` call if query already has records

* fix spec name

* fix spec

* Add to CHANGELOG.md

* Update CHANGELOG.md

Co-authored-by: Sean Edge <[email protected]>

---------

Co-authored-by: Sean Edge <[email protected]>
  • Loading branch information
CharlesMassry and asedge authored Nov 20, 2023
1 parent 8cd0cf4 commit 8fbd5bd
Show file tree
Hide file tree
Showing 4 changed files with 31 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/73)

## 0.19.0

- Bulk API methods. (https://github.com/Beyond-Finance/active_force/pull/65)
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
limit(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
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(:limit)
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(:limit)
query.first
end
end

describe '.last' do
Expand Down

0 comments on commit 8fbd5bd

Please sign in to comment.