Skip to content

Commit

Permalink
Adding update and update! class methods on SObject. (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
asedge authored Sep 26, 2023
1 parent ecc3ce5 commit 8eb8418
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
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

- Adding `update` and `update!` class methods on `SObject`. (https://github.com/Beyond-Finance/active_force/pull/66)

## 0.17.0

- Fix bug with has_many queries due to query method chaining mutating in-place (https://github.com/Beyond-Finance/active_force/pull/10)
Expand Down
10 changes: 9 additions & 1 deletion lib/active_force/sobject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ def self.create! args
new(args).create!
end

def self.update(id, attributes)
new(attributes.merge(id: id)).update
end

def self.update!(id, attributes)
new(attributes.merge(id: id)).update!
end

def save!
run_callbacks :save do
if persisted?
Expand Down Expand Up @@ -231,7 +239,7 @@ def attributes_for_create
end

def default_attributes
@attributes.each_value.select do |value|
@attributes.each_value.select do |value|
value.is_a?(ActiveModel::Attribute::UserProvidedDefault) || value.instance_values["original_attribute"].is_a?(ActiveModel::Attribute::UserProvidedDefault)
end.map(&:name)
end
Expand Down
18 changes: 18 additions & 0 deletions spec/active_force/sobject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,24 @@ class IceCream < ActiveForce::SObject
expect(Whizbang.create(text: 'some text')).to be_instance_of(Whizbang)
end
end

describe 'self.update' do
it 'uses the client to update the correct record' do
expect(client).to receive(:update!)
.with(Whizbang.table_name, { 'Id' => '12345678', 'Text_Label' => 'my text', 'Updated_From__c' => 'Rails' })
.and_return(true)
Whizbang.update('12345678', text: 'my text')
end
end

describe 'self.update!' do
it 'uses the client to update the correct record' do
expect(client).to receive(:update!)
.with(Whizbang.table_name, { 'Id' => '123456789', 'Text_Label' => 'some other text', 'Updated_From__c' => 'Rails' })
.and_return(true)
Whizbang.update('123456789', text: 'some other text')
end
end
end

describe '.count' do
Expand Down

0 comments on commit 8eb8418

Please sign in to comment.