Class: RuboCop::Cop::DevDoc::Route::ResourceNameNumber
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::DevDoc::Route::ResourceNameNumber
- Defined in:
- lib/rubocop/cop/dev_doc/route/resource_name_number.rb
Overview
Use a plural name for ‘resources` and a singular name for `resource`.
## Rationale Follow Rails’ standard REST conventions. A plural ‘resources` maps to a collection (`/products`), so its name should be plural; a singular `resource` maps to a single implicit-id resource (`/profile`), so its name should be singular. Mismatched number reads against every Rails convention and produces awkward path/helper names.
❌ resources :product # => /product
❌ resource :sessions # singular resource, plural name
✔️ resources :products # => /products
✔️ resource :session # => /session
The check uses ActiveSupport’s inflector, so irregular and uncountable nouns are handled (‘resources :people`, `resources :fish` are fine).
## Exception A project with custom inflections (‘config/initializers/inflections.rb`) the bundled inflector doesn’t know about may be misjudged. Disable the cop on that line with a reason.
Constant Summary collapse
- MSG =
'`%<method>s` should name a %<number>s resource — use `:%<expected>s`.'.freeze
- RESTRICT_ON_SEND =
%i[resources resource].freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rubocop/cop/dev_doc/route/resource_name_number.rb', line 43 def on_send(node) node.arguments.each do |arg| name = name_of(arg) next unless name expected = expected_name(node.method_name, name) next if expected == name add_offense( arg, message: format(MSG, method: node.method_name, number: number_word(node.method_name), expected: expected) ) end end |