Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

raise error if record not persisted

  • Loading branch information...
commit f3942cb734233c8a1af64b9099c7a93ca1b11b85 1 parent a23ee9c
Nick Ragaz authored July 17, 2012
2  Gemfile.lock
... ...
@@ -1,7 +1,7 @@
1 1
 PATH
2 2
   remote: .
3 3
   specs:
4  
-    resque-async-method (1.0.0)
  4
+    resque-async-method (1.0.1)
5 5
       activesupport
6 6
       resque
7 7
 
10  lib/resque/plugins/async/method.rb
@@ -3,19 +3,23 @@
3 3
 module Resque::Plugins::Async::Method
4 4
   extend ActiveSupport::Concern
5 5
 
  6
+  class NotPersistedError < StandardError; end
  7
+
6 8
   module ClassMethods
7 9
     def async_method(method_name, opts={})
8 10
       # Allow tests to call sync_ methods ...
9 11
       alias_method :"sync_#{method_name}", method_name
10  
-    
  12
+
11 13
       # ... but don't actually make them asynchronous
12 14
       return if Rails.env.test?
13  
-    
  15
+
14 16
       define_method "#{method_name}" do |*args|
  17
+        raise NotPersistedError, "Methods can only be async'ed on persisted records (currently: #{inspect})" unless persisted?
  18
+
15 19
         my_klass       = Resque::Plugins::Async::Worker
16 20
         my_klass.queue = opts[:queue] ||
17 21
                          send(:class).name.underscore.pluralize
18  
-    
  22
+
19 23
         Resque.enqueue(
20 24
           my_klass,
21 25
           send(:class).name,
2  resque-async-method.gemspec
@@ -3,7 +3,7 @@ Gem::Specification.new do |s|
3 3
   s.summary = "Make Active Record instance methods asynchronous using resque."
4 4
   s.description = "Make Active Record instance methods asynchronous using resque."
5 5
   s.files = Dir["lib/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]
6  
-  s.version = "1.0.1"
  6
+  s.version = "1.1"
7 7
   s.authors = ["Nick Ragaz"]
8 8
   s.email = "nick.ragaz@gmail.com"
9 9
   s.homepage = "http://github.com/nragaz/resque-async-method"
8  test/async_method_test.rb
@@ -16,6 +16,14 @@ class AsyncMethodTest < ActiveSupport::TestCase
16 16
     assert_equal 'success!', user.sync_long_method
17 17
   end
18 18
 
  19
+  test "raise error if not persisted" do
  20
+    user = User.new
  21
+
  22
+    assert_raises(Resque::Plugins::Async::Method::NotPersistedError) do
  23
+      user.long_method
  24
+    end
  25
+  end
  26
+
19 27
   test "lint" do
20 28
     assert_nothing_raised do
21 29
       Resque::Plugin.lint(Resque::Plugins::Async::Method)
BIN  test/dummy/db/test.sqlite3
Binary file not shown
26  test/dummy/log/async_testing.log
@@ -50,3 +50,29 @@
50 50
    (0.1ms)  rollback transaction
51 51
    (0.1ms)  begin transaction
52 52
    (0.0ms)  rollback transaction
  53
+   (0.4ms)  begin transaction
  54
+  Fixture Delete (0.4ms)  DELETE FROM "users"
  55
+  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)
  56
+   (1.4ms)  commit transaction
  57
+   (0.1ms)  begin transaction
  58
+   (0.1ms)  rollback transaction
  59
+   (0.1ms)  begin transaction
  60
+  User Load (0.2ms)  SELECT "users".* FROM "users" LIMIT 1
  61
+  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 980190962]]
  62
+   (0.1ms)  rollback transaction
  63
+   (0.1ms)  begin transaction
  64
+   (0.0ms)  rollback transaction
  65
+   (0.5ms)  begin transaction
  66
+  Fixture Delete (0.4ms)  DELETE FROM "users"
  67
+  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)
  68
+   (0.8ms)  commit transaction
  69
+   (0.1ms)  begin transaction
  70
+   (0.1ms)  rollback transaction
  71
+   (0.1ms)  begin transaction
  72
+  User Load (0.3ms)  SELECT "users".* FROM "users" LIMIT 1
  73
+  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 980190962]]
  74
+   (0.1ms)  rollback transaction
  75
+   (0.2ms)  begin transaction
  76
+   (0.1ms)  rollback transaction
  77
+   (0.0ms)  begin transaction
  78
+   (0.1ms)  rollback transaction

0 notes on commit f3942cb

Please sign in to comment.
Something went wrong with that request. Please try again.