Permalink
Browse files

raise error if record not persisted

  • Loading branch information...
nragaz committed Jul 17, 2012
1 parent a23ee9c commit f3942cb734233c8a1af64b9099c7a93ca1b11b85
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- resque-async-method (1.0.0)
+ resque-async-method (1.0.1)
activesupport
resque
@@ -3,19 +3,23 @@
module Resque::Plugins::Async::Method
extend ActiveSupport::Concern
+ class NotPersistedError < StandardError; end
+
module ClassMethods
def async_method(method_name, opts={})
# Allow tests to call sync_ methods ...
alias_method :"sync_#{method_name}", method_name
-
+
# ... but don't actually make them asynchronous
return if Rails.env.test?
-
+
define_method "#{method_name}" do |*args|
+ raise NotPersistedError, "Methods can only be async'ed on persisted records (currently: #{inspect})" unless persisted?
+
my_klass = Resque::Plugins::Async::Worker
my_klass.queue = opts[:queue] ||
send(:class).name.underscore.pluralize
-
+
Resque.enqueue(
my_klass,
send(:class).name,
@@ -3,7 +3,7 @@ Gem::Specification.new do |s|
s.summary = "Make Active Record instance methods asynchronous using resque."
s.description = "Make Active Record instance methods asynchronous using resque."
s.files = Dir["lib/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]
- s.version = "1.0.1"
+ s.version = "1.1"
s.authors = ["Nick Ragaz"]
s.email = "nick.ragaz@gmail.com"
s.homepage = "http://github.com/nragaz/resque-async-method"
@@ -16,6 +16,14 @@ class AsyncMethodTest < ActiveSupport::TestCase
assert_equal 'success!', user.sync_long_method
end
+ test "raise error if not persisted" do
+ user = User.new
+
+ assert_raises(Resque::Plugins::Async::Method::NotPersistedError) do
+ user.long_method
+ end
+ end
+
test "lint" do
assert_nothing_raised do
Resque::Plugin.lint(Resque::Plugins::Async::Method)
View
Binary file not shown.
@@ -50,3 +50,29 @@
 (0.1ms) rollback transaction
 (0.1ms) begin transaction
 (0.0ms) rollback transaction
+  (0.4ms) begin transaction
+ Fixture Delete (0.4ms) DELETE FROM "users"
+ Fixture Insert (0.2ms) INSERT INTO "users" ("name", "email", "born_on", "locked_at", "created_at", "updated_at", "id") VALUES ('John Doe', 'john.doe@johndoe.com', '1984-09-17', '2011-06-13 22:01:13.000000', '2012-07-17 19:12:40', '2012-07-17 19:12:40', 980190962)
+  (1.4ms) commit transaction
+  (0.1ms) begin transaction
+  (0.1ms) rollback transaction
+  (0.1ms) begin transaction
+ User Load (0.2ms) SELECT "users".* FROM "users" LIMIT 1
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 980190962]]
+  (0.1ms) rollback transaction
+  (0.1ms) begin transaction
+  (0.0ms) rollback transaction
+  (0.5ms) begin transaction
+ Fixture Delete (0.4ms) DELETE FROM "users"
+ Fixture Insert (0.2ms) INSERT INTO "users" ("name", "email", "born_on", "locked_at", "created_at", "updated_at", "id") VALUES ('John Doe', 'john.doe@johndoe.com', '1984-09-17', '2011-06-13 22:01:13.000000', '2012-07-17 19:14:52', '2012-07-17 19:14:52', 980190962)
+  (0.8ms) commit transaction
+  (0.1ms) begin transaction
+  (0.1ms) rollback transaction
+  (0.1ms) begin transaction
+ User Load (0.3ms) SELECT "users".* FROM "users" LIMIT 1
+ User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 980190962]]
+  (0.1ms) rollback transaction
+  (0.2ms) begin transaction
+  (0.1ms) rollback transaction
+  (0.0ms) begin transaction
+  (0.1ms) rollback transaction

0 comments on commit f3942cb

Please sign in to comment.