Skip to content

Commit

Permalink
Ensure callbacks are run on banged methods as well as rescued ones. R…
Browse files Browse the repository at this point in the history
…eturn boolean value on `save` and `update`, and return `self` on `create` as ActiveRecord does.
  • Loading branch information
Lyric Hupp committed Oct 8, 2014
1 parent a596127 commit 05d1b91
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 39 deletions.
35 changes: 20 additions & 15 deletions lib/active_force/sobject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,19 @@ def self.build mash
def update_attributes! attributes = {}
assign_attributes attributes
validate!
sfdc_client.update! table_name, attributes_for_sfdb
changed_attributes.clear
run_callbacks :save do
run_callbacks :update do
sfdc_client.update! table_name, attributes_for_sfdb
changed_attributes.clear
end
end
true
end

alias_method :update!, :update_attributes!

def update_attributes attributes = {}
run_callbacks :update do
update_attributes! attributes
end
update_attributes! attributes
rescue Faraday::Error::ClientError, RecordInvalid => error
handle_save_error error
end
Expand All @@ -81,37 +83,40 @@ def update_attributes attributes = {}

def create!
validate!
self.id = sfdc_client.create! table_name, attributes_for_sfdb
changed_attributes.clear
true
run_callbacks :save do
run_callbacks :create do
self.id = sfdc_client.create! table_name, attributes_for_sfdb
changed_attributes.clear
end
end
self
end

def create
run_callbacks :create do
create!
end
create!
rescue Faraday::Error::ClientError, RecordInvalid => error
handle_save_error error
self
end

def destroy
sfdc_client.destroy! self.class.table_name, id
end

def self.create args
new(args).save
new(args).create
end

def self.create! args
new(args).save!
new(args).create!
end

def save!
run_callbacks :save do
if persisted?
update!
!!update!
else
create!
!!create!
end
end
end
Expand Down
69 changes: 45 additions & 24 deletions spec/active_force/sobject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,29 @@ class IceCream < ActiveForce::SObject
let(:instance){ Whizbang.new(id: '1') }

describe '#update' do
before do
expected_args = [
Whizbang.table_name,
{'Text_Label' => 'some text', 'Boolean_Label' => false, 'Id' => '1'}
]
expect(client).to receive(:update!).with(*expected_args).and_return('id')

context 'with valid attributes' do
before do
expected_args = [
Whizbang.table_name,
{'Text_Label' => 'some text', 'Boolean_Label' => false, 'Id' => '1', "Updated_From__c"=>"Rails"}
]
expect(client).to receive(:update!).with(*expected_args).and_return('id')
end

it 'delegates to the Client with create! and sets the id' do
expect(instance.update( text: 'some text', boolean: false )).to eq(true)
expect(instance.text).to eq('some text')
end
end

it 'saves successfully' do
expect(instance.update!( text: 'some text', boolean: false )).to eq true
context 'with invalid attributes' do
it 'sets the error on the instance' do
expect(instance.update( boolean: true )).to eq(false)
expect(instance.errors).to be_present
expect(instance.errors.full_messages.count).to eq(1)
expect(instance.errors.full_messages[0]).to eq("Percent can't be blank")
end
end
end

Expand All @@ -116,12 +129,13 @@ class IceCream < ActiveForce::SObject
before do
expected_args = [
Whizbang.table_name,
{'Text_Label' => 'some text', 'Boolean_Label' => false, 'Id' => '1'}
{'Text_Label' => 'some text', 'Boolean_Label' => false, 'Id' => '1', "Updated_From__c"=>"Rails"}
]
expect(client).to receive(:update!).with(*expected_args).and_return('id')
end
it 'saves successfully' do
expect(instance.update!( text: 'some text', boolean: false )).to eq true
expect(instance.update!( text: 'some text', boolean: false )).to eq(true)
expect(instance.text).to eq('some text')
end
end

Expand All @@ -146,17 +160,28 @@ class IceCream < ActiveForce::SObject
end

describe '#create' do
before do
expect(client).to receive(:create!).and_return('id')
end
context 'with valid attributes' do
before do
expect(client).to receive(:create!).and_return('id')
end

it 'delegates to the Client with create!' do
instance.create
it 'delegates to the Client with create! and sets the id' do
expect(instance.create).to be_instance_of(Whizbang)
expect(instance.id).to eq('id')
end
end

it 'sets the id' do
instance.create
expect(instance.id).to eq('id')

context 'with invalid attributes' do
let(:instance){ Whizbang.new boolean: true }

it 'sets the error on the instance' do
expect(instance.create).to be_instance_of(Whizbang)
expect(instance.id).to eq(nil)
expect(instance.errors).to be_present
expect(instance.errors.full_messages.count).to eq(1)
expect(instance.errors.full_messages[0]).to eq("Percent can't be blank")
end
end
end

Expand All @@ -167,11 +192,7 @@ class IceCream < ActiveForce::SObject
before{ expect(client).to receive(:create!).and_return('id') }

it 'saves successfully' do
expect(instance.create!).to eq(true)
end

it 'sets the id' do
instance.create!
expect(instance.create!).to be_instance_of(Whizbang)
expect(instance.id).to eq('id')
end
end
Expand Down Expand Up @@ -209,7 +230,7 @@ class IceCream < ActiveForce::SObject
end

it 'should create a new instance' do
expect(Whizbang.create({ text: 'some text' })).to eq true
expect(Whizbang.create(text: 'some text')).to be_instance_of(Whizbang)
end
end
end
Expand Down

0 comments on commit 05d1b91

Please sign in to comment.