aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/callbacks.rb
Commit message (Collapse)AuthorAgeFilesLines
* Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-131-1/+0
| | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* Should not pass extra args to `_update_record`Ryuta Kamizono2019-02-211-2/+2
| | | | | | | | | | | | | | | | | | | | The argument of `_update_record` and `_create_record` is `attribute_names`, that is reserved for overriding by partial writes attribute names. https://github.com/rails/rails/blob/67e20d1d4854d834e9e43e56486d37cd98983f0d/activerecord/lib/active_record/persistence.rb#L719 https://github.com/rails/rails/blob/67e20d1d4854d834e9e43e56486d37cd98983f0d/activerecord/lib/active_record/persistence.rb#L737 https://github.com/rails/rails/blob/67e20d1d4854d834e9e43e56486d37cd98983f0d/activerecord/lib/active_record/attribute_methods/dirty.rb#L171 https://github.com/rails/rails/blob/67e20d1d4854d834e9e43e56486d37cd98983f0d/activerecord/lib/active_record/attribute_methods/dirty.rb#L177 The reason that no failing with extra args is that `Timestamp` module which is most outside module of the `_update_record` discards the extra args. https://github.com/rails/rails/blob/67e20d1d4854d834e9e43e56486d37cd98983f0d/activerecord/lib/active_record/timestamp.rb#L104 But that looks odd dependency. It should not be passed extra args, should only be passed attribute names.
* Replaced usage of where.delete/destroy_all with delete/destroy_byAbhay Nikam2019-02-201-1/+1
|
* Updating sample code on ActiveRecord#before_destroy callback [ci skip]Espartaco Palma2018-10-311-1/+1
| | | | It was executing a delete_all method with wrong parameter
* `Persistence#increment!` requires an attribute argument which is incrementedRyuta Kamizono2018-09-241-1/+1
|
* Fix sentence [ci skip]Santosh Wadghule2018-05-281-1/+1
|
* Fix that association's after_touch is not called with counter cacheRyuta Kamizono2018-05-271-0/+4
| | | | | | | | | | | | Since #31405, using `#increment!` with touch option instead of `#touch` to touch belongs_to association if counter cache is enabled. It caused the regression since `#increment!` won't invoke after_touch callbacks even if touch option is given. To fix the regression, make `#increment!` invokes after_touch callbacks if touch option is given. Fixes #31559. Fixes #32408.
* Remove expired explanation [ci skip]Shia2018-03-301-15/+1
| | | | Override callback doesn't work anymore.
* [ci skip] Inherit ActiveRecord::Base in example codesYoshiyuki Hirano2017-12-151-2/+2
|
* Remove deprecated `:if` and `:unless` string filter for callbacksRafael Mendonça França2017-10-231-2/+2
|
* Fix some typos.Mike Boone2017-10-101-2/+2
|
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Merge pull request #27294 from eavgerinos/doc-ar-callbacks-orderRafael França2017-01-061-0/+49
|\ | | | | [documentation] ActiveRecord: Document order of Callbacks
| * [ci skip] ActiveRecord: Document order of CallbacksAggelos Avgerinos2016-12-221-0/+49
| |
* | No need to nodoc private methodsAkira Matsuda2016-12-241-3/+3
|/
* Deprecate the behavior of AR::Dirty inside of after_(create|update|save) ↵Sean Griffin2016-11-011-11/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | callbacks We pretty frequently get bug reports that "dirty is broken inside of after callbacks". Intuitively they are correct. You'd expect `Model.after_save { puts changed? }; model.save` to do the same thing as `model.save; puts model.changed?`, but it does not. However, changing this goes much farther than just making the behavior more intuitive. There are a _ton_ of places inside of AR that can be drastically simplified with this change. Specifically, autosave associations, timestamps, touch, counter cache, and just about anything else in AR that works with callbacks have code to try to avoid "double save" bugs which we will be able to flat out remove with this change. We introduce two new sets of methods, both with names that are meant to be more explicit than dirty. The first set maintains the old behavior, and their names are meant to center that they are about changes that occurred during the save that just happened. They are equivalent to `previous_changes` when called outside of after callbacks, or once the deprecation cycle moves. The second set is the new behavior. Their names imply that they are talking about changes from the database representation. The fact that this is what we really care about became clear when looking at `BelongsTo.touch_record` when tests were failing. I'm unsure that this set of methods should be in the public API. Outside of after callbacks, they are equivalent to the existing methods on dirty. Dirty itself is not deprecated, nor are the methods inside of it. They will only emit the warning when called inside of after callbacks. The scope of this breakage is pretty large, but the migration path is simple. Given how much this can improve our codebase, and considering that it makes our API more intuitive, I think it's worth doing.
* 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
| |