Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow class methods to be queued on ActiveRecord objects. Closes #41

  • Loading branch information...
commit 782ceb8646455132bf1acce4f0ee0e9aac1035b9 1 parent c7ce97a
@bkeepers bkeepers authored
View
6 lib/delayed/backend/active_record.rb
@@ -2,7 +2,11 @@
class ActiveRecord::Base
def self.load_for_delayed_job(id)
- find(id)
+ if id
+ find(id)
+ else
+ super
+ end
end
def dump_for_delayed_job
View
77 spec/delayed_method_spec.rb
@@ -1,93 +1,24 @@
require 'spec_helper'
-class SimpleJob
- cattr_accessor :runs; self.runs = 0
- def perform; @@runs += 1; end
-end
-
-class RandomRubyObject
- def say_hello
- 'hello'
- end
-end
-
-class StoryReader
-
- def read(story)
- "Epilog: #{story.tell}"
- end
-
-end
-
describe 'random ruby objects' do
before { Delayed::Job.delete_all }
it "should respond_to :send_later method" do
-
- RandomRubyObject.new.respond_to?(:send_later)
-
+ Object.new.respond_to?(:send_later)
end
it "should raise a ArgumentError if send_later is called but the target method doesn't exist" do
- lambda { RandomRubyObject.new.send_later(:method_that_deos_not_exist) }.should raise_error(NoMethodError)
+ lambda { Object.new.send_later(:method_that_deos_not_exist) }.should raise_error(NoMethodError)
end
it "should add a new entry to the job table when send_later is called on it" do
- Delayed::Job.count.should == 0
-
- RandomRubyObject.new.send_later(:to_s)
-
- Delayed::Job.count.should == 1
+ lambda { Object.new.send_later(:to_s) }.should change { Delayed::Job.count }.by(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)
-
- Delayed::Job.count.should == 1
+ lambda { Object.send_later(:to_s) }.should change { Delayed::Job.count }.by(1)
end
- it "should ignore ActiveRecord::RecordNotFound errors because they are permanent" do
- story = Story.create :text => 'Once upon...'
- job = story.send_later(:tell)
- story.destroy
- lambda { job.invoke_job }.should_not raise_error
- end
-
- it "should store the object as string if its an active record" do
- story = Story.create :text => 'Once upon...'
- story.send_later(:tell)
-
- job = Delayed::Job.find(:first)
- job.payload_object.class.should == Delayed::PerformableMethod
- job.payload_object.object.should == "LOAD;Story;#{story.id}"
- job.payload_object.method.should == :tell
- job.payload_object.args.should == []
- job.payload_object.perform.should == 'Once upon...'
- end
-
- it "should store arguments as string if they an active record" do
-
- story = Story.create :text => 'Once upon...'
-
- reader = StoryReader.new
- reader.send_later(:read, story)
-
- job = Delayed::Job.find(:first)
- job.payload_object.class.should == Delayed::PerformableMethod
- job.payload_object.method.should == :read
- job.payload_object.args.should == ["LOAD;Story;#{story.id}"]
- job.payload_object.perform.should == 'Epilog: Once upon...'
- end
-
it "should call send later on methods which are wrapped with handle_asynchronously" do
story = Story.create :text => 'Once upon...'
View
45 spec/performable_method_spec.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+class StoryReader
+ def read(story)
+ "Epilog: #{story.tell}"
+ end
+end
+
+describe Delayed::PerformableMethod do
+
+ it "should ignore ActiveRecord::RecordNotFound errors because they are permanent" do
+ story = Story.create :text => 'Once upon...'
+ p = Delayed::PerformableMethod.new(story, :tell, [])
+ story.destroy
+ lambda { p.perform }.should_not raise_error
+ end
+
+ it "should store the object as string if its an active record" do
+ story = Story.create :text => 'Once upon...'
+ p = Delayed::PerformableMethod.new(story, :tell, [])
+ p.class.should == Delayed::PerformableMethod
+ p.object.should == "LOAD;Story;#{story.id}"
+ p.method.should == :tell
+ p.args.should == []
+ p.perform.should == 'Once upon...'
+ end
+
+ it "should allow class methods to be called on ActiveRecord models" do
+ p = Delayed::PerformableMethod.new(Story, :count, [])
+ lambda { p.send(:load, p.object) }.should_not raise_error
+ end
+
+ it "should store arguments as string if they are active record objects" do
+ story = Story.create :text => 'Once upon...'
+ reader = StoryReader.new
+ p = Delayed::PerformableMethod.new(reader, :read, [story])
+ p.class.should == Delayed::PerformableMethod
+ p.method.should == :read
+ p.args.should == ["LOAD;Story;#{story.id}"]
+ p.perform.should == 'Epilog: Once upon...'
+ end
+
+
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.