aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/callbacks.rb
Commit message (Collapse)AuthorAgeFilesLines
* Provide :touch option to save() to accommodate saving without updating ↵Dan Olson2014-12-271-1/+1
| | | | timestamps. [#18202]
* Prefix internal method with _Rafael Mendonça França2014-10-251-5/+5
| | | | This will avoid naming clash with user defined methods
* Reduce allocations when running AR callbacks.Pete Higgins2014-09-281-5/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Inspired by @tenderlove's work in c363fff29f060e6a2effe1e4bb2c4dd4cd805d6e, this reduces the number of strings allocated when running callbacks for ActiveRecord instances. I measured that using this script: ``` require 'objspace' require 'active_record' require 'allocation_tracer' ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" ActiveRecord::Base.connection.instance_eval do create_table(:articles) { |t| t.string :name } end class Article < ActiveRecord::Base; end a = Article.create name: "foo" a = Article.find a.id N = 10 result = ObjectSpace::AllocationTracer.trace do N.times { Article.find a.id } end result.sort.each do |k,v| p k => v end puts "total: #{result.values.map(&:first).inject(:+)}" ``` When I run this against master and this branch I get this output: ``` pete@balloon:~/projects/rails/activerecord$ git checkout master M Gemfile Switched to branch 'master' pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_before pete@balloon:~/projects/rails/activerecord$ git checkout remove-dynamic-send-on-built-in-callbacks M Gemfile Switched to branch 'remove-dynamic-send-on-built-in-callbacks' pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_after pete@balloon:~/projects/rails/activerecord$ diff allocations_before allocations_after 39d38 < {["/home/pete/projects/rails/activesupport/lib/active_support/callbacks.rb", 81]=>[40, 0, 0, 0, 0, 0]} 42c41 < total: 630 --- > total: 590 ``` In addition to this, there are two micro-optimizations present: * Using `block.call if block` vs `yield if block_given?` when the block was being captured already. ``` pete@balloon:~/projects$ cat benchmark_block_call_vs_yield.rb require 'benchmark/ips' def block_capture_with_yield &block yield if block_given? end def block_capture_with_call &block block.call if block end def no_block_capture yield if block_given? end Benchmark.ips do |b| b.report("block_capture_with_yield") { block_capture_with_yield } b.report("block_capture_with_call") { block_capture_with_call } b.report("no_block_capture") { no_block_capture } end pete@balloon:~/projects$ ruby benchmark_block_call_vs_yield.rb Calculating ------------------------------------- block_capture_with_yield 124979 i/100ms block_capture_with_call 138340 i/100ms no_block_capture 136827 i/100ms ------------------------------------------------- block_capture_with_yield 5703108.9 (±2.4%) i/s - 28495212 in 4.999368s block_capture_with_call 6840730.5 (±3.6%) i/s - 34169980 in 5.002649s no_block_capture 5821141.4 (±2.8%) i/s - 29144151 in 5.010580s ``` * Defining and calling methods instead of using send. ``` pete@balloon:~/projects$ cat benchmark_method_call_vs_send.rb require 'benchmark/ips' class Foo def tacos nil end end my_foo = Foo.new Benchmark.ips do |b| b.report('send') { my_foo.send('tacos') } b.report('call') { my_foo.tacos } end pete@balloon:~/projects$ ruby benchmark_method_call_vs_send.rb Calculating ------------------------------------- send 97736 i/100ms call 151142 i/100ms ------------------------------------------------- send 2683730.3 (±2.8%) i/s - 13487568 in 5.029763s call 8005963.9 (±2.7%) i/s - 40052630 in 5.006604s ``` The result of this is making typical ActiveRecord operations slightly faster: https://gist.github.com/phiggins/e46e51dcc7edb45b5f98
* [Active Record] Renamed private methods create_record and update_recordPrathamesh Sonpatki2014-02-201-2/+2
| | | | | | This is to ensure that they are not accidentally called by the app code. They are renamed to _create_record and _update_record respectively. Closes #11645
* Fix ActiveRecord::Callbacks sample code [ci skip]joker10072013-12-031-2/+2
| | | | | | | | | Callback caller class uses `after_initialize`, but Callback callee defines `after_find`. Current sample code causes following error. NoMethodError: undefined method `after_initialize' for #<EncryptionWrapper:0x007fe4931fa5c0>
* Add documentation for after_touch [ci skip]claudiob2013-10-081-1/+4
|
* fix typosVipul A M2013-04-211-1/+1
|
* Rename update_attributes method to update, keep update_attributes as an aliasAmparo Luna + Guillermo Iguaran2013-01-031-2/+2
|
* 1.9 hash syntax changesAvnerCohen2012-11-081-1/+1
|
* Merge branch 'master' of https://github.com/lifo/docrailsPablo Ifran2012-10-221-8/+5
|\ | | | | | | | | Conflicts: activerecord/lib/active_record/callbacks.rb
| * copy edits [ci skip]Vijay Dev2012-10-211-14/+7
| |
* | Changeing some code-styles of the examples & fix a typo on dependent optionPablo Ifran2012-10-221-18/+12
|/
* ActiveRecord Callbacks ordering examplesPablo Ifran2012-10-191-0/+43
|
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2012-06-221-1/+1
|\
| * Typo in documentation.Andrés Mejía2012-06-191-1/+1
| |
* | Simplify AR configuration code.Jon Leighton2012-06-151-24/+0
|/ | | | | Get rid of ActiveModel::Configuration, make better use of ActiveSupport::Concern + class_attribute, etc.
* Remove Array.wrap calls in ActiveRecordRafael Mendonça França2012-01-061-2/+0
|
* Support configuration on ActiveRecord::Model.Jon Leighton2011-12-281-12/+39
| | | | | | | | | | | | | | | The problem: We need to be able to specify configuration in a way that can be inherited to models that include ActiveRecord::Model. So it is no longer sufficient to put 'top level' config on ActiveRecord::Base, but we do want configuration specified on ActiveRecord::Base and descendants to continue to work. So we need something like class_attribute that can be defined on a module but that is inherited when ActiveRecord::Model is included. The solution: added ActiveModel::Configuration module which provides a config_attribute macro. It's a bit specific hence I am not putting this in Active Support or making it a 'public API' at present.
* Added back the Callback debugging section by interrogating the _*_callbacks ↵ozzyaaron2011-03-291-0/+18
| | | | method
* Active Record typos.R.T. Lechow2011-03-051-1/+1
|
* Merge branch 'master' of git://github.com/lifo/docrailsXavier Noria2011-02-021-10/+6
|\
| * FIX not using _on_create or _on_update callbacks only _create and _updatePaco Guzman2011-02-011-1/+1
| |
| * Correct docs for after_find and after_initializeJesse Storimer2011-01-281-9/+5
| |
* | Use run_callbacks; the generated _run_<name>_callbacks method is not a ↵John Firebaugh2011-01-311-5/+5
|/ | | | | | public interface. Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
* Remove doc for debugging callbacks. Methods don't exist in Rails masterRafael Mendonça França2010-10-131-10/+0
|
* Cleanup deprecation warnings in active recordCarlos Antonio da Silva2010-09-061-17/+0
| | | | Signed-off-by: José Valim <jose.valim@gmail.com>
* lifecycle should be two words, life cycleJaime Iniesta2010-08-261-2/+2
|
* fisting after_rollback and after commit callbacksAaron Patterson2010-08-201-1/+1
|
* Deletes trailing whitespaces (over text files only find * -type f -exec sed ↵Santiago Pastorino2010-08-141-24/+24
| | | | 's/[ \t]*$//' -i {} \;)
* fixing documentationNeeraj Singh2010-08-031-2/+2
|
* Merge remote branch 'docrails/master' into 3-0-stableXavier Noria2010-08-031-31/+35
|\
| * ensuring that description does not exceed 100 columnsNeeraj Singh2010-08-021-31/+35
| |
* | Add an internal (private API) after_touch callback. [#5271 state:resolved]José Valim2010-08-021-2/+6
|/
* Merge remote branch 'rails/master'Xavier Noria2010-06-201-25/+2
|\ | | | | | | | | Conflicts: actionpack/lib/abstract_controller/base.rb
| * ActiveModel::Validations::Callbacks should not be required by default.José Valim2010-06-191-8/+2
| |
| * moving before_validation and after_validation functionality from ↵Neeraj Singh2010-06-191-20/+3
| | | | | | | | | | | | | | | | ActiveRecord to ActiveModel [#4653 state:resolved] Signed-off-by: José Valim <jose.valim@gmail.com>
* | Adds title and basic description where needed.Rizwan Reza2010-06-151-0/+2
|/
* update docs before_validation_on_create => before_validation(:on => :create)Santiago Pastorino and José Ignacio Costa2010-06-071-1/+1
|
* eliminate alias_method_chain from ActiveRecordwycats2010-05-091-32/+16
|
* say something about after_(commit|rollback) in callbacks.rb, the fact that ↵Xavier Noria2010-05-021-1/+6
| | | | their implementation is elsewhere is not important for rdoc purposes
* Avoid deprecated String#to_a by using Array.wrap(...) instead of Array(...)Jeremy Kemper2010-04-101-2/+4
|
* Add debugging documentation for _callback_chain to ActiveRecord::Callbackchrisfinne2010-03-261-0/+10
| | | | Signed-off-by: Rizwan Reza <rizwanreza@gmail.com>
* Ensure deprecated validate methods are invoked when they are private [#3214 ↵José Valim2010-01-181-1/+1
| | | | status:resolved]
* Ensure before_validation and after_validation accepts :on as option.José Valim2010-01-061-2/+20
|
* Get rid of DeprecatedCallbacks in ActiveRecord::Associations and finally ↵José Valim2009-12-301-1/+1
| | | | remove it.
* Move ActiveRecord callbacks implementation to ActiveModel and make use of it.José Valim2009-12-281-54/+5
| | | | Signed-off-by: Yehuda Katz <wycats@Yehuda-Katz.local>
* Callbacks, DeprecatedCallbacks = NewCallbacks, CallbacksJoshua Peek2009-10-121-2/+2
|
* Refactor new callbacks and AR implementation.José Valim2009-09-081-59/+20
| | | | Signed-off-by: Joshua Peek <josh@joshpeek.com>
* Changed ActiveRecord to use new callbacks and speed up observers by only ↵José Valim2009-09-081-129/+126
| | | | | | notifying events that are actually being consumed. Signed-off-by: Joshua Peek <josh@joshpeek.com>
* Fix warnings in AMoJoshua Peek2009-09-051-1/+1
|