Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Skip saving if the dirty test returns false
Browse files Browse the repository at this point in the history
* This will speed up any cases where the resource is being saved and there
  are before(:save) or after(:save) filters.
* Removed now redundant code from _create and _update

[#1155 state:resolved]
  • Loading branch information
dkubb committed Feb 2, 2010
1 parent 9207bb4 commit 1b76693
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
10 changes: 4 additions & 6 deletions lib/dm-core/resource.rb
Expand Up @@ -849,9 +849,6 @@ def child_collections
#
# @api private
def _create
# Can't create a resource that is not dirty and doesn't have serial keys
return false if new? && clean?

# set defaults for new resource
properties.each do |property|
unless property.serial? || property.loaded?(self)
Expand Down Expand Up @@ -884,9 +881,7 @@ def _create
def _update
original_attributes = self.original_attributes

if original_attributes.empty?
true
elsif original_attributes.any? { |property, _value| !property.valid?(property.get!(self)) }
if original_attributes.any? { |property, _value| !property.valid?(property.get!(self)) }
false
else
# remove from the identity map
Expand Down Expand Up @@ -919,6 +914,9 @@ def _save(safe)
#
# @api semipublic
def save_self(safe = true)
# short-circuit if the resource is not dirty
return saved? unless dirty_self?

new_resource = new?
if safe
new_resource ? create_hook : update_hook
Expand Down
33 changes: 32 additions & 1 deletion spec/public/shared/resource_shared_spec.rb
Expand Up @@ -704,6 +704,17 @@
it { @user.should respond_to(method) }

describe "##{method}" do
before :all do
@user_model.class_eval do
attr_accessor :save_hook_call_count

before :save do
@save_hook_call_count ||= 0
@save_hook_call_count += 1
end
end
end

describe 'on a new, not dirty resource' do
before :all do
@user = @user_model.new
Expand All @@ -713,11 +724,23 @@
it 'should return false' do
@return.should be_false
end

it 'should call save hook expected number of times' do
@user.save_hook_call_count.should be_nil
end
end

describe 'on a not new, not dirty resource' do
before :all do
@return = @user.__send__(method)
end

it 'should return true even when resource is not dirty' do
@user.__send__(method).should be_true
@return.should be_true
end

it 'should call save hook expected number of times' do
@user.save_hook_call_count.should be_nil
end
end

Expand All @@ -736,6 +759,10 @@
it 'should actually store the changes to persistent storage' do
@user.attributes.should == @user.reload.attributes
end

it 'should call save hook expected number of times' do
@user.save_hook_call_count.should == (method == :save ? 1 : nil)
end
end

describe 'on a dirty invalid resource' do
Expand All @@ -748,6 +775,10 @@
it 'should not save an invalid resource' do
@user.__send__(method).should be_false
end

it 'should call save hook expected number of times' do
@user.save_hook_call_count.should == (method == :save ? 1 : nil)
end
end

describe 'with new resources in a has relationship' do
Expand Down

0 comments on commit 1b76693

Please sign in to comment.