diff --git a/lib/devise/async/model.rb b/lib/devise/async/model.rb index 11b4f63..272328b 100644 --- a/lib/devise/async/model.rb +++ b/lib/devise/async/model.rb @@ -2,6 +2,8 @@ module Devise module Models module Async extend ActiveSupport::Concern + + STRINGIFYABLE_TYPES = [Symbol] included do # Register hook to send all devise pending notifications. @@ -40,7 +42,8 @@ def send_devise_notification(notification, *args) # If the record isn't dirty (aka has already been saved) enqueue right away # because the callback has already been triggered. else - Devise::Async::Worker.enqueue(notification, self.class.name, self.id.to_s, *args) + args = stringify_args_values(args) + Devise::Async::Worker.enqueue(notification.to_s, self.class.name, self.id.to_s, *args) end end @@ -49,7 +52,8 @@ def send_devise_pending_notifications devise_pending_notifications.each do |notification, args| # Use `id.to_s` to avoid problems with mongoid 2.4.X ids being serialized # wrong with YAJL. - Devise::Async::Worker.enqueue(notification, self.class.name, self.id.to_s, *args) + args = stringify_args_values(args) + Devise::Async::Worker.enqueue(notification.to_s, self.class.name, self.id.to_s, *args) end @devise_pending_notifications = [] end @@ -73,7 +77,22 @@ def add_current_locale_to_args(args) opts['locale'] = I18n.locale args.push(opts) end + + def stringify_args_values(args) + args.each_with_object([]) do |a, acc| + arg = is_arg_hash?(a) ? a.deep_merge(a) {|_,_,v| is_val_stringifyable?(v) ? v.to_s : v} : a + acc << arg + end + end + def is_arg_hash?(arg) + arg.present? && arg.is_a?(Hash) + end + + def is_val_stringifyable?(val) + # val is stringifyable if is one of the STRINGIFYABLE_TYPES + val.present? && STRINGIFYABLE_TYPES.include?(val.class) + end end end end