Skip to content

Commit

Permalink
Discourage the use of Object#delay and Delayed::Job.enqueue (#54)
Browse files Browse the repository at this point in the history
We want all of our jobs to use ActiveJob.
  • Loading branch information
rzane authored Sep 26, 2024
1 parent 47826cf commit 4a773b0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ Betterment/UnscopedFind:
- ZipCode
```

### Betterment/DirectDelayedEnqueue

This cop flags code that uses `Object#delay` or `Delayed::Job.enqueue`. Please use `ActiveJob` instead.

```ruby
user.delay.save!
Delayed::Job.enqueue(MyJob.new)
```

### Betterment/DynamicParams

This cop flags code that accesses parameters whose names may be dynamically generated, such as a list of parameters in an a global variable or a return value from a method. In some cases, dynamically accessing parameter names can obscure what the client is expected to send and may make it difficult to reason about the code, both manually and programmatically. For example:
Expand Down
3 changes: 3 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Betterment/AuthorizationInController:
Enabled: false
StyleGuide: '#bettermentauthorizationincontroller'

Betterment/DirectDelayedEnqueue:
StyleGuide: '#bettermentdirectdelayedenqueue'

Betterment/DynamicParams:
StyleGuide: '#bettermentdynamicparams'

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/betterment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'rubocop/cop/betterment/utils/method_return_table'
require 'rubocop/cop/betterment/utils/hardcoded_attribute'
require 'rubocop/cop/betterment/authorization_in_controller'
require 'rubocop/cop/betterment/direct_delayed_enqueue'
require 'rubocop/cop/betterment/dynamic_params'
require 'rubocop/cop/betterment/unscoped_find'
require 'rubocop/cop/betterment/unsafe_job'
Expand Down
22 changes: 22 additions & 0 deletions lib/rubocop/cop/betterment/direct_delayed_enqueue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Betterment
class DirectDelayedEnqueue < Base
DELAY_MESSAGE = 'Please use Active Job instead of using `Object#delay`'
ENQUEUE_MESSAGE = 'Please use Active Job instead of using `Delayed::Job.enqueue`'

# @!method enqueue?(node)
def_node_matcher :enqueue?, <<-PATTERN
(send (const (const nil? :Delayed) :Job) :enqueue ...)
PATTERN

def on_send(node)
add_offense(node, message: DELAY_MESSAGE) if node.method?(:delay)
add_offense(node, message: ENQUEUE_MESSAGE) if enqueue?(node)
end
end
end
end
end
26 changes: 26 additions & 0 deletions spec/rubocop/cop/betterment/direct_delayed_enqueue_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RuboCop::Cop::Betterment::DirectDelayedEnqueue, :config do
it "adds an offense when using `.delay` without arguments" do
expect_offense(<<~RUBY)
user.delay.save!
^^^^^^^^^^ Please use Active Job instead of using `Object#delay`
RUBY
end

it "adds an offense when using `.delay` with arguments" do
expect_offense(<<~RUBY)
user.delay(foo: 'bar').save!
^^^^^^^^^^^^^^^^^^^^^^ Please use Active Job instead of using `Object#delay`
RUBY
end

it "adds an offense when using `Delayed::Job.enqueue`" do
expect_offense(<<~RUBY)
Delayed::Job.enqueue(Job.new)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Please use Active Job instead of using `Delayed::Job.enqueue`
RUBY
end
end

0 comments on commit 4a773b0

Please sign in to comment.