Support class reloading with BroadcastPolicy

Since Notification and NotificationPolicy can be reloaded,
BroadcastPolicy needs to re-cache the references on reload.

To test:
You can experience a reloading error before applying this patch
by triggering a notification after a reload.
- rails console
- reload!
- Course.last.enroll_student(User.last).accept!
This will cause `ArgumentError: A copy of Notifier has been removed from the module tree but is still active!`

With this patch applied, there should be no error (assuming
that student can be enrolled in that class).
This commit is contained in:
Dave Gynn 2016-02-11 11:08:17 -05:00
parent b9119afe32
commit 34380c8f7f
3 changed files with 10 additions and 4 deletions

View File

@ -1,4 +1,6 @@
require 'broadcast_policy'
BroadcastPolicy.notifier = lambda { Notifier.new }
BroadcastPolicy.notification_finder = lambda { NotificationFinder.new(Notification.all_cached) }
Rails.configuration.to_prepare do
BroadcastPolicy.notifier = lambda { Notifier.new }
BroadcastPolicy.notification_finder = lambda { NotificationFinder.new(Notification.all_cached) }
end
ActiveRecord::Base.send(:extend, BroadcastPolicy::ClassMethods)

View File

@ -23,8 +23,10 @@ In order to use the gem in Rails, you'll need an initializer something like this
config/initializers/broadcast_policy.rb
require 'broadcast_policy'
BroadcastPolicy.notifier = lambda { Notifier.new }
BroadcastPolicy.notification_finder = lambda { NotificationFinder.new(Nofication.all) }
Rails.configuration.to_prepare do
BroadcastPolicy.notifier = lambda { Notifier.new }
BroadcastPolicy.notification_finder = lambda { NotificationFinder.new(Notification.all_cached) }
end
ActiveRecord::Base.send(:extend, BroadcastPolicy::ClassMethods)
The two BroadcastPolicy services are necessary to supply the canvas domain objects

View File

@ -58,6 +58,7 @@ module BroadcastPolicy #:nodoc:
def self.notifier=(notifier_or_proc)
if notifier_or_proc.respond_to?(:call)
@notifier = nil
@notifier_proc = notifier_or_proc
else
@notifier = notifier_or_proc
@ -71,6 +72,7 @@ module BroadcastPolicy #:nodoc:
def self.notification_finder=(notification_finder_or_proc)
if notification_finder_or_proc.respond_to?(:call)
@notification_finder = nil
@notification_finder_proc = notification_finder_or_proc
else
@notification_finder = notification_finder_or_proc