Skip to content

Commit

Permalink
Fix missing nil attributes in SObject.update (#79)
Browse files Browse the repository at this point in the history
* fix missing nil attributes in SObject.update

* update changelog
  • Loading branch information
rferg authored Jan 9, 2024
1 parent 4ddfb81 commit 8796078
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
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 `.update` and `.update!`: include given `nil` valued attributes in request (https://github.com/Beyond-Finance/active_force/pull/79)
- 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
Expand Down
24 changes: 16 additions & 8 deletions lib/active_force/sobject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,24 @@ class << self
def_delegators :query, :not, :or, :where, :first, :last, :all, :find, :find!, :find_by, :find_by!, :sum, :count, :includes, :limit, :order, :select, :none
def_delegators :mapping, :table, :table_name, :custom_table?, :mappings

def update(id, attributes)
prepare_for_update(id, attributes).update
end

def update!(id, attributes)
prepare_for_update(id, attributes).update!
end

private

def prepare_for_update(id, attributes)
new(attributes.merge(id: id)).tap do |obj|
attributes.each do |name, value|
obj.public_send("#{name}_will_change!") if value.nil?
end
end
end

###
# Provide each subclass with a default id field. Can be overridden
# in the subclass if needed
Expand Down Expand Up @@ -131,14 +147,6 @@ 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
18 changes: 18 additions & 0 deletions spec/active_force/sobject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ class IceCream < ActiveForce::SObject
.and_return(true)
Whizbang.update('12345678', text: 'my text')
end

it 'includes given nil values in the request' do
allow(client).to receive(:update!).and_return(true)
Whizbang.update('test123', text: nil, date: nil)
expect(client).to have_received(:update!).with(
Whizbang.table_name,
{ 'Id' => 'test123', 'Text_Label' => nil, 'Date_Label' => nil, 'Updated_From__c' => 'Rails' }
)
end
end

describe 'self.update!' do
Expand All @@ -330,6 +339,15 @@ class IceCream < ActiveForce::SObject
.and_return(true)
Whizbang.update('123456789', text: 'some other text')
end

it 'includes given nil values in the request' do
allow(client).to receive(:update!).and_return(true)
Whizbang.update!('test123', text: nil, date: nil)
expect(client).to have_received(:update!).with(
Whizbang.table_name,
{ 'Id' => 'test123', 'Text_Label' => nil, 'Date_Label' => nil, 'Updated_From__c' => 'Rails' }
)
end
end
end

Expand Down

0 comments on commit 8796078

Please sign in to comment.