Class: Cops::FormErrorResponse
- Inherits:
-
RuboCop::Cop::Base
- Object
- RuboCop::Cop::Base
- Cops::FormErrorResponse
- Extended by:
- RuboCop::Cop::AutoCorrector
- Defined in:
- lib/generators/rolemodel/linters/rubocop/templates/lib/cops/form_error_response.rb
Overview
Custom Cop to Ensure Form Error Response Statuses
There are three levels of matching here:
Methods (def on_if)
This scans code and only stops on the method specified.
E.G. on_if will only move into the below checks when it encounters an if statement.
Node matcher (def_node_matcher :form_error) This scans the block of code (in this case, the else branch of the if block) using AST (Abstract Tree Syntax) to match certain patterns
Regex This checks the specific matching line to ensure it meets our expectations
Constant Summary collapse
- MSG =
"Use status: :#{self.http_status} for invalid form requests."- STATUS_PAIR =
Ensure the render is returning the correct status, otherwise add the offense
/\(pair\s+\(sym :status\)\s+\(sym :#{self.http_status}\)\)/m
Class Method Summary collapse
Instance Method Summary collapse
-
#on_if(node) ⇒ Object
Only check within if blocks.
Class Method Details
.http_status ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/generators/rolemodel/linters/rubocop/templates/lib/cops/form_error_response.rb', line 24 def self.http_status if rack_version >= '3.1' :unprocessable_content else :unprocessable_entity end end |
.rack_version ⇒ Object
20 21 22 |
# File 'lib/generators/rolemodel/linters/rubocop/templates/lib/cops/form_error_response.rb', line 20 def self.rack_version Gem.loaded_specs['rack'].version end |
Instance Method Details
#on_if(node) ⇒ Object
Only check within if blocks
44 45 46 47 48 49 50 51 52 |
# File 'lib/generators/rolemodel/linters/rubocop/templates/lib/cops/form_error_response.rb', line 44 def on_if(node) form_error(node.else_branch) do |name| next if name.to_s.match?(STATUS_PAIR) add_offense(name) do |corrector| corrector.insert_after(name, ", status: :#{self.class.http_status}") end end end |