canvas-lms/gems/activesupport-suspend_callb...
Jacob Fugal f2e7523419 fix activesupport-suspend_callbacks for 4.1
activesupport 4.1 doesn't have CallbackChain inherit from Array anymore,
instead, it houses an internal array and delegates to it.

Change-Id: I5730f734be5e7c0422ac56245609043f52b6425a
test-plan: specs
Reviewed-on: https://gerrit.instructure.com/33081
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Joel Hough <joel@instructure.com>
Product-Review: Simon Williams <simon@instructure.com>
Product-Review: Jacob Fugal <jacob@instructure.com>
QA-Review: Jacob Fugal <jacob@instructure.com>
2014-04-16 15:12:22 +00:00
..
lib/active_support/callbacks fix activesupport-suspend_callbacks for 4.1 2014-04-16 15:12:22 +00:00
spec spec: simple_cov vendored_gems report merging 2014-02-28 09:04:50 +00:00
.rspec ActiveSupport::Callbacks::Suspension 2014-02-19 20:20:00 +00:00
Gemfile spec: canvas rspec rake task for vendored_gems 2014-02-24 05:47:34 +00:00
LICENSE.txt ActiveSupport::Callbacks::Suspension 2014-02-19 20:20:00 +00:00
README.md ActiveSupport::Callbacks::Suspension 2014-02-19 20:20:00 +00:00
Rakefile ActiveSupport::Callbacks::Suspension 2014-02-19 20:20:00 +00:00
activesupport-suspend_callbacks.gemspec Move time related code into canvas_time gem 2014-04-14 20:47:01 +00:00
test.sh Move time related code into canvas_time gem 2014-04-14 20:47:01 +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