canvas-lms/gems/activesupport-suspend_callb...
Cody Cutrer eacec0a606 bundle update factory_bot
Change-Id: I3ab74c597004c220cc2d0515546f0f584579d433
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/337908
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Jacob Burroughs <jburroughs@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
Build-Review: Cody Cutrer <cody@instructure.com>
2024-01-24 16:18:47 +00:00
..
lib/active_support/callbacks RuboCop: Style/StringLiterals, Style/StringLiteralsInInterpolation 2021-11-25 14:03:06 +00:00
spec switch from byebug to debug 2023-09-20 23:48:39 +00:00
.rspec raise bundler minimum requirement 2021-02-19 22:49:02 +00:00
Gemfile fix lockfile syncing from canvas lockfile to sub-gems 2023-05-09 22:57:42 +00:00
Gemfile.lock bundle update factory_bot 2024-01-24 16:18:47 +00:00
LICENSE.txt da licença part 53 2017-05-01 21:06:11 +00:00
README.md ActiveSupport::Callbacks::Suspension 2014-02-19 20:20:00 +00:00
Rakefile add frozen_string_literal comment to engines and gems 2021-03-30 18:14:15 +00:00
activesupport-suspend_callbacks.gemspec switch from byebug to debug 2023-09-20 23:48:39 +00:00
test.sh simplify gem test harnesses 2016-01-19 17:52:58 +00:00

README.md

Suspend Callbacks

ActiveSupport's Callbacks module allows you to define a callback hook and then register methods to be run before/after/around that hook.

It also let's you skip callbacks in a specific context. For example, ActiveRecord::Base defines a callback hook around save. My top level Person model may register an :ensure_privacy method to run before save. But the Celebrity model that inherits from Person can then skip that callback. End result: when I save a john_doe Person object, :ensure_privacy will run, but when I save the dhh Celebrity object, it won't.

But what if you want to suspend callbacks, regardless of subclass, but only for a duration of time? That's when you want to suspend callbacks.

Example

class MyModel < ActiveRecord::Base include ActiveSupport::Callbacks::Suspension

before :save, :expensive_callback after :save, :other_callback

def expensive_callback # stuff end

def other_callback # stuff end end

instance1 = MyModel.new instance2 = MyModel.new

MyModel.suspend_callbacks do

neither callback will run for either instance

instance1.save instance2.save end

MyModel.suspend_callbacks(kind: :save) do

same

instance1.save instance2.save end

MyModel.suspend_callbacks(:expensive_callback) do

expensive_callback won't run, but other_callback will

instance1.save instance2.save end

MyModel.suspend_callbacks(type: :before) do

same

instance1.save instance2.save end

instance1.suspend_callbacks do

callbacks won't run this time...

instance1.save

... but they will this time.

instance2.save end