Browse files

allow Delayed::PerformableMethod to work with class methods. Foo.send…

…_later :process, id
  • Loading branch information...
1 parent 0c084e5 commit d76d34a73f45b63dfe521d315f201037c277eeb7 @technoweenie technoweenie committed Oct 23, 2008
Showing with 18 additions and 3 deletions.
  1. +10 −3 lib/delayed/performable_method.rb
  2. +8 −0 spec/delayed_method_spec.rb
View
13 lib/delayed/performable_method.rb
@@ -1,6 +1,7 @@
module Delayed
class PerformableMethod < Struct.new(:object, :method, :args)
- AR_STRING_FORMAT = /^AR\:([A-Z]\w+)\:(\d+)$/
+ CLASS_STRING_FORMAT = /^CLASS\:([A-Z]\w+)$/
+ AR_STRING_FORMAT = /^AR\:([A-Z]\w+)\:(\d+)$/
def initialize(object, method, args)
raise NoMethodError, "undefined method `#{method}' for #{self.inspect}" unless object.respond_to?(method)
@maia
maia added a line comment Nov 9, 2008

respond_to? causes a problem when using delayed_job with ActionMailer::Base, as the deliver_* methods are defined via method_missing (I think) and they don’t return true on respond_to?.
I assumed http://github.com/rails/rails/commit/3cf773b187e803e16b8237e5923fa4c1139cde8a fixed the problem, but my Rails 2.1.2 apparently still returns false on custom deliver_xxx methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@@ -21,20 +22,26 @@ def perform
def load(arg)
case arg
- when AR_STRING_FORMAT then $1.constantize.find($2)
+ when CLASS_STRING_FORMAT then $1.constantize
+ when AR_STRING_FORMAT then $1.constantize.find($2)
else arg
end
end
def dump(arg)
case arg
+ when Class then class_to_string(arg)
when ActiveRecord::Base then ar_to_string(arg)
else arg
end
end
def ar_to_string(obj)
"AR:#{obj.class}:#{obj.id}"
- end
+ end
+
+ def class_to_string(obj)
+ "CLASS:#{obj.name}"
+ end
end
end
View
8 spec/delayed_method_spec.rb
@@ -57,6 +57,14 @@ def read(story)
Delayed::Job.count.should == 1
end
+ it "should add a new entry to the job table when send_later is called on the class" do
+ Delayed::Job.count.should == 0
+
+ RandomRubyObject.send_later(:to_s)
+
+ Delayed::Job.count.should == 1
+ end
+
it "should run get the original method executed when the job is performed" do
RandomRubyObject.new.send_later(:say_hello)

0 comments on commit d76d34a

Please sign in to comment.