lib/abstractivator/fiber_defer.rb in abstractivator-0.2.0 vs lib/abstractivator/fiber_defer.rb in abstractivator-0.3.0

- old
+ new

@@ -31,7 +31,17 @@ EM.defer(safe_action, proc { |result, error| f.resume([result, error]) }) result, error = Fiber.yield raise error if error result end + + def mongoid_fiber_defer(&action) + db = Mongoid::Threaded.database_override + fiber_defer do + # in the background thread + Mongoid.override_database(db) # set the db to what it was in the main thread + action.call + end + Mongoid.override_database(db) # main thread has moved on before we resume here. restore the db override. + end end end