aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/callbacks.rb
Commit message (Collapse)AuthorAgeFilesLines
* modernizes hash syntax in activerecordXavier Noria2016-08-061-1/+1
|
* Change to use a more realistic example and not giving the impression that ↵Vipul A M2016-05-051-3/+3
| | | | | | | | destroy_all is preferred way to destroy related records. This example just wants to demonstrate callback behaviour. [ci skip]
* [ci skip] Don't promote SQL interpolation.Kasper Timm Hansen2016-05-051-2/+2
| | | | | | After fb898e9, the `before_destroy` had some code that used SQL interpolation left over. Don't think we should promote that even if the values aren't directly from user input.
* do not pass conditions to `#destroy_all` [ci skip]yuuji.yaginuma2016-05-051-2/+2
| | | | Passing conditions to `#destroy_all` was deprecated in c82c5f8.
* Fix grammar `a` to `an` [ci skip]Ryuta Kamizono2016-02-131-1/+1
|
* Deprecate passing string to define callback.yui-knk2015-12-161-15/+0
|
* Typo correctionYves Siegrist2015-12-071-4/+4
| | | | | | | In the doc the `dependent` option was set with: `dependent: destroy`. This is not working because destroy would call the method of the activerecord::base object. The right way is: `dependent: :destroy`
* Merge pull request #18548 from ↵Sean Griffin2015-10-281-0/+5
|\ | | | | | | | | | | sebjacobs/support-bidirectional-destroy-dependencies Add support for bidirectional destroy dependencies
| * Add support for bidirectional destroy dependenciesSeb Jacobs2015-01-161-1/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Prior to this commit if you defined a bidirectional relationship between two models with destroy dependencies on both sides, a call to `destroy` would result in an infinite callback loop. Take the following relationship. class Content < ActiveRecord::Base has_one :content_position, dependent: :destroy end class ContentPosition < ActiveRecord::Base belongs_to :content, dependent: :destroy end Calling `Content#destroy` or `ContentPosition#destroy` would result in an infinite callback loop. This commit changes the behaviour of `ActiveRecord::Callbacks#destroy` so that it guards against subsequent callbacks. Thanks to @zetter for demonstrating the issue with failing tests[1]. [1] rails#13609
* | applies new doc guidelines to Active Record.Yves Senn2015-10-141-16/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The focus of this change is to make the API more accessible. References to method and classes should be linked to make it easy to navigate around. This patch makes exzessiv use of `rdoc-ref:` to provide more readable docs. This makes it possible to document `ActiveRecord::Base#save` even though the method is within a separate module `ActiveRecord::Persistence`. The goal here is to bring the API closer to the actual code that you would write. This commit only deals with Active Record. The other gems will be updated accordingly but in different commits. The pass through Active Record is not completely finished yet. A follow up commit will change the spots I haven't yet had the time to update. /cc @fxn
* | add missing `:nodoc:` to `AR::Callbacks::ClassMethods`. [ci skip]Yves Senn2015-10-141-1/+1
| |
* | docs, tiny rdoc markup fix. [ci skip]Yves Senn2015-08-121-1/+2
| | | | | | | | `+` doesn't work around content with spaces fallback `<tt>`.
* | `destroy` shouldn't raise when child associations fail to saveSean Griffin2015-07-241-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Deep down in the association internals, we're calling `destroy!` rather than `destroy` when handling things like `dependent` or autosave association callbacks. Unfortunately, due to the structure of the code (e.g. it uses callbacks for everything), it's nearly impossible to pass whether to call `destroy` or `destroy!` down to where we actually need it. As such, we have to do some legwork to handle this. Since the callbacks are what actually raise the exception, we need to rescue it in `ActiveRecord::Callbacks`, rather than `ActiveRecord::Persistence` where it matters. (As an aside, if this code wasn't so callback heavy, it would handling this would likely be as simple as changing `destroy` to call `destroy!` instead of the other way around). Since we don't want to lose the exception when `destroy!` is called (in particular, we don't want the value of the `record` field to change to the parent class), we have to do some additional legwork to hold onto it where we can use it. Again, all of this is ugly and there is definitely a better way to do this. However, barring a much more significant re-architecting for what I consider to be a reletively minor improvement, I'm willing to take this small hit to the flow of this code (begrudgingly).
* | Revert "Revert "Reduce allocations when running AR callbacks.""Guo Xiang Tan2015-07-161-5/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit bdc1d329d4eea823d07cf010064bd19c07099ff3. Before: Calculating ------------------------------------- 22.000 i/100ms ------------------------------------------------- 229.700 (± 0.4%) i/s - 1.166k Total Allocated Object: 9939 After: Calculating ------------------------------------- 24.000 i/100ms ------------------------------------------------- 246.443 (± 0.8%) i/s - 1.248k Total Allocated Object: 7939 ``` begin require 'bundler/inline' rescue LoadError => e $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' raise e end gemfile(true) do source 'https://rubygems.org' # gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3' gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7' gem 'arel', github: 'rails/arel' gem 'sqlite3' gem 'benchmark-ips' end require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection('sqlite3::memory:') ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do create_table :users, force: true do |t| t.string :name, :email t.boolean :admin t.timestamps null: false end end class User < ActiveRecord::Base default_scope { where(admin: true) } end admin = true 1000.times do attributes = { name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", email: "foobar@email.com", admin: admin } User.create!(attributes) admin = !admin end GC.disable Benchmark.ips(5, 3) do |x| x.report { User.all.to_a } end key = if RUBY_VERSION < '2.2' :total_allocated_object else :total_allocated_objects end before = GC.stat[key] User.all.to_a after = GC.stat[key] puts "Total Allocated Object: #{after - before}" ```
* | typo fix [ci skip]karanarora2015-05-201-1/+1
| |
* | Revert "Reduce allocations when running AR callbacks."Guo Xiang Tan2015-03-221-6/+5
|/ | | | This reverts commit 796cab45561fce268aa74e6587cdb9cae3bb243e.
* Deprecate `false` as the way to halt AR callbacksclaudiob2015-01-021-3/+3
| | | | | | | | | | Before this commit, returning `false` in an ActiveRecord `before_` callback such as `before_create` would halt the callback chain. After this commit, the behavior is deprecated: will still work until the next release of Rails but will also display a deprecation warning. The preferred way to halt a callback chain is to explicitly `throw(:abort)`.
* 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
|/