Skip to content

Commit 6399deb

Browse files
authored
Detect a non-standard controller (#62)
See Betterment/the-book#250
1 parent 2fa78dc commit 6399deb

File tree

6 files changed

+62
-2
lines changed

6 files changed

+62
-2
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
betterlint (1.15.1)
4+
betterlint (1.16.0)
55
rubocop (~> 1.62.0)
66
rubocop-graphql (~> 1.5.0)
77
rubocop-performance (~> 1.21.0)

betterlint.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
55

66
Gem::Specification.new do |s|
77
s.name = "betterlint"
8-
s.version = "1.15.1"
8+
s.version = "1.16.0"
99
s.authors = ["Development"]
1010
s.email = ["[email protected]"]
1111
s.summary = "Betterment rubocop configuration"

config/default.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ Betterment/NonStandardActions:
6464
- update
6565
StyleGuide: '#bettermentnonstandardactions'
6666

67+
Betterment/NonStandardController:
68+
Description: Detects non-standard controller names.
69+
Include:
70+
- config/routes.rb
71+
6772
Betterment/RedirectStatus:
6873
SafeAutoCorrect: false
6974
Description: Detect missing status codes when redirecting POST, PUT, PATCH, or DELETE responses

lib/rubocop/cop/betterment.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require 'rubocop/cop/betterment/internals_protection'
1414
require 'rubocop/cop/betterment/memoization_with_arguments'
1515
require 'rubocop/cop/betterment/non_standard_actions'
16+
require 'rubocop/cop/betterment/non_standard_controller'
1617
require 'rubocop/cop/betterment/site_prism_loaded'
1718
require 'rubocop/cop/betterment/spec_helper_required_outside_spec_dir'
1819
require 'rubocop/cop/betterment/implicit_redirect_type'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module Betterment
6+
class NonStandardController < RuboCop::Cop::Base
7+
MSG = <<~END.gsub(/\s+/, " ")
8+
`resources` and `resource` should not need to specify a `controller` option.
9+
If your controller lives in a module, please use the `module` option. Otherwise,
10+
please rename your controller to match your routes.
11+
END
12+
13+
# @!method resources_with_controller(node)
14+
def_node_matcher :resources_with_controller, <<~PATTERN
15+
(send _ {:resources | :resource} _ (hash <$(pair (sym :controller) _) ...>))
16+
PATTERN
17+
18+
def on_send(node)
19+
resources_with_controller(node) { |option| add_offense(option) }
20+
end
21+
end
22+
end
23+
end
24+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe RuboCop::Cop::Betterment::NonStandardController, :betterlint_config do
6+
it 'adds an offense when a controller is specified' do
7+
expect_offense(<<~RUBY)
8+
resources :users, controller: 'non_standard'
9+
^^^^^^^^^^^^^^^^^^^^^^^^^^ `resources` and `resource` [...]
10+
11+
resource :user, controller: 'non_standard'
12+
^^^^^^^^^^^^^^^^^^^^^^^^^^ `resources` and `resource` [...]
13+
14+
resources :users, controller: 'non_standard', only: [:index]
15+
^^^^^^^^^^^^^^^^^^^^^^^^^^ `resources` and `resource` [...]
16+
17+
resources :users, only: [:index], controller: 'non_standard'
18+
^^^^^^^^^^^^^^^^^^^^^^^^^^ `resources` and `resource` [...]
19+
RUBY
20+
end
21+
22+
it 'does not add an offense when a controller is not specified' do
23+
expect_no_offenses(<<~RUBY)
24+
resources :users
25+
resources :users, only: [:index]
26+
resource :user
27+
resource :user, only: [:index]
28+
RUBY
29+
end
30+
end

0 commit comments

Comments
 (0)