Skip to content

Commit

Permalink
Add find_by! method that raises ActiveForce::RecordNotFound if no… (
Browse files Browse the repository at this point in the history
#8)

* Add `find_by!` method that raises `ActiveForce::RecordNotFound` if nothing is found.

* Update CHANGELOG.
  • Loading branch information
asedge authored Mar 2, 2023
1 parent dc9439d commit c7f64c4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Not released
* Fix `where` chaining on `ActiveQuery` (https://github.com/Beyond-Finance/active_force/pull/7)
* Add `find_by!` which raises `ActiveForce::RecordNotFound` if nothing is found. (https://github.com/Beyond-Finance/active_force/pull/8)

## 0.9.1
* Fix invalid error class (https://github.com/Beyond-Finance/active_force/pull/6)
Expand Down
7 changes: 7 additions & 0 deletions lib/active_force/active_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module ActiveForce
class PreparedStatementInvalid < ArgumentError; end
class RecordNotFound < StandardError; end
class ActiveQuery < Query
extend Forwardable

Expand Down Expand Up @@ -54,6 +55,12 @@ def find_by conditions
where(conditions).limit 1
end

def find_by! conditions
res = find_by(conditions)
raise RecordNotFound if res.nil?
res
end

def includes(*relations)
relations.each do |relation|
association = sobject.associations[relation]
Expand Down
2 changes: 1 addition & 1 deletion lib/active_force/sobject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SObject

class << self
extend Forwardable
def_delegators :query, :where, :first, :last, :all, :find, :find_by, :count, :includes, :limit, :order, :select, :none
def_delegators :query, :where, :first, :last, :all, :find, :find_by, :find_by!, :count, :includes, :limit, :order, :select, :none
def_delegators :mapping, :table, :table_name, :custom_table?, :mappings

private
Expand Down
7 changes: 7 additions & 0 deletions spec/active_force/active_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@
end
end

describe "#find_by!" do
it "raises if record not found" do
allow(client).to receive(:query).and_return(nil)
expect { active_query.find_by!(field: 123) }.to raise_error(ActiveForce::RecordNotFound)
end
end

describe "responding as an enumerable" do
before do
expect(active_query).to receive(:to_a).and_return([])
Expand Down
11 changes: 11 additions & 0 deletions spec/active_force/sobject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,17 @@ class IceCream < ActiveForce::SObject
end
end

describe "#find_by!" do
it "queries the client, with the SFDC field names and correctly enclosed values" do
expect(client).to receive(:query).with("SELECT #{Whizbang.fields.join ', '} FROM Whizbang__c WHERE (Id = 123) AND (Text_Label = 'foo') LIMIT 1").and_return([Restforce::Mash.new(Id: 123, text: 'foo')])
Whizbang.find_by! id: 123, text: "foo"
end
it "raises if nothing found" do
expect(client).to receive(:query).with("SELECT #{Whizbang.fields.join ', '} FROM Whizbang__c WHERE (Id = 123) AND (Text_Label = 'foo') LIMIT 1")
expect { Whizbang.find_by! id: 123, text: "foo" }.to raise_error(ActiveForce::RecordNotFound)
end
end

describe '#reload' do
let(:client) do
double("sfdc_client", query: [Restforce::Mash.new(Id: 1, Name: 'Jeff')])
Expand Down

0 comments on commit c7f64c4

Please sign in to comment.