lib/backgrounded/handler/resque_handler.rb in backgrounded-0.6.2 vs lib/backgrounded/handler/resque_handler.rb in backgrounded-0.7.0

- old
+ new

@@ -6,17 +6,32 @@ class ResqueHandler DEFAULT_QUEUE = 'backgrounded' @@queue = DEFAULT_QUEUE def request(object, method, *args) - @@queue = object.backgrounded_options[method.to_sym][:queue] || DEFAULT_QUEUE - Resque.enqueue(ResqueHandler, object.class.name, object.id, method, *args) + options = object.backgrounded_options[method.to_sym] + @@queue = options[:queue] || DEFAULT_QUEUE + instance, id = instance_identifiers(object) + Resque.enqueue(ResqueHandler, instance, id, method, *args) end def self.queue @@queue end def self.perform(clazz, id, method, *args) - clazz.constantize.find(id).send(method, *args) + find_instance(clazz, id, method).send(method, *args) + end + + private + def self.find_instance(clazz, id, method) + clazz = clazz.constantize + clazz.respond_to?(method) ? clazz : clazz.find(id) + end + def instance_identifiers(object) + instance, id = if object.is_a?(Class) + [object.name, -1] + else + [object.class.name, object.id] + end end end end end