Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/devise/async/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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