diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bc08fc..10f626f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/active_force/sobject.rb b/lib/active_force/sobject.rb index 9ca5ae9..254d703 100644 --- a/lib/active_force/sobject.rb +++ b/lib/active_force/sobject.rb @@ -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? @@ -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 diff --git a/spec/active_force/sobject_spec.rb b/spec/active_force/sobject_spec.rb index f54ab12..69b6122 100644 --- a/spec/active_force/sobject_spec.rb +++ b/spec/active_force/sobject_spec.rb @@ -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