Skip to content

Commit

Permalink
Removed after_find and after_initialize callbacks.
Browse files Browse the repository at this point in the history
This removed about 30% of the time spent in finding documents. Big time improvement for something that is not worth much.
  • Loading branch information
jnunemaker committed Mar 16, 2011
1 parent b879c6d commit f19d772
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 36 deletions.
15 changes: 0 additions & 15 deletions lib/mongo_mapper/plugins/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@ module Callbacks
extend ActiveSupport::Concern

module InstanceMethods
def initialize(attrs = {})
super.tap { run_callbacks(:initialize) }
end

def initialize_from_database(attrs={})
super.tap do
run_callbacks(:find)
run_callbacks(:initialize)
end
end

def initialize_copy(other)
super.tap { run_callbacks(:initialize) }
end

def valid?(context = nil)
context ||= (new_record? ? :create : :update)
super(context) && errors.empty?
Expand Down
3 changes: 1 addition & 2 deletions lib/mongo_mapper/plugins/embedded_callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ module EmbeddedCallbacks
extend ::ActiveModel::Callbacks
include ::ActiveModel::Validations::Callbacks

define_model_callbacks :validation, :save, :create, :update, :destroy, :only => [ :before, :after ]
define_model_callbacks :initialize, :find, :only => :after
define_model_callbacks :validation, :save, :create, :update, :destroy, :only => [:before, :after]
end

module InstanceMethods
Expand Down
20 changes: 1 addition & 19 deletions test/functional/test_callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ module CallbacksSupport
def self.included base
base.key :name, String

[ :after_find, :after_initialize,
:before_validation, :after_validation,
[ :before_validation, :after_validation,
:before_create, :after_create,
:before_update, :after_update,
:before_save, :after_save,
Expand All @@ -29,7 +28,6 @@ def clear_history

class CallbacksTest < Test::Unit::TestCase
CreateCallbackOrder = [
:after_initialize,
:before_validation,
:after_validation,
:before_save,
Expand All @@ -52,22 +50,6 @@ class CallbacksTest < Test::Unit::TestCase
@document = Doc { include CallbacksSupport }
end

should "run after_initialize" do
doc = @document.new
doc.history.should == [:after_initialize]
end

should "run callbacks on find" do
doc = @document.create(:name => 'John Nunemaker')
@document.find!(doc.id).history.should == [:after_find, :after_initialize]
end

should "run after_initialize when cloning an object" do
doc = @document.create(:name => 'John Nunemaker')
doc.clear_history
doc.dup.history.should == [:after_initialize]
end

should "get the order right for creating documents" do
doc = @document.create(:name => 'John Nunemaker')
doc.history.should == CreateCallbackOrder
Expand Down

2 comments on commit f19d772

@brianhempel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI we did use after_initialize in our app twice. (Small dataset, but those 2 callbacks account for 29% of the MongoMapper callbacks defined in our app.) Not a big deal, we can move the code to the initialize method.

@jnunemaker
Copy link
Contributor Author

@jnunemaker jnunemaker commented on f19d772 Mar 19, 2011 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.