Class: RuboCop::Cop::Rails::RedundantPresenceValidationOnBelongsTo
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::RedundantPresenceValidationOnBelongsTo
- Extended by:
- AutoCorrector, TargetRailsVersion
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb
Overview
Since Rails 5.0 the default for belongs_to
is optional: false
unless config.active_record.belongs_to_required_by_default
is
explicitly set to false
. The presence validator is added
automatically, and explicit presence validation is redundant.
Constant Summary collapse
- MSG =
'Remove explicit presence validation for %<association>s.'
- RESTRICT_ON_SEND =
%i[validates].freeze
Constants included from TargetRailsVersion
TargetRailsVersion::USES_REQUIRES_GEM_API
Instance Method Summary collapse
-
#any_belongs_to?(node, association: ) ⇒ Array<RuboCop::AST::Node>?
Match a class with
belongs_to
with no regard toforeign_key
option. -
#belongs_to?(node, key: , fk: ) ⇒ Array<RuboCop::AST::Node>
Match a class with a matching association, either by name or an explicit
foreign_key
option. -
#belongs_to_with_a_matching_fk?(node, fk) ⇒ Array<RuboCop::AST::Node>
Match a matching
belongs_to
association with a matching explicitforeign_key
option. -
#belongs_to_without_fk?(node, key) ⇒ Array<RuboCop::AST::Node>
Match a matching
belongs_to
association, without an explicitforeign_key
option. - #on_send(node) ⇒ Object
-
#optional?(node) ⇒ Object
Match a
belongs_to
association with an optional option in a hash. -
#optional_option?(node) ⇒ Object
Match an optional option in a hash.
-
#presence_validation?(node) ⇒ Object
Match a
validates
statement with a presence check.
Methods included from TargetRailsVersion
minimum_target_rails_version, support_target_rails_version?
Instance Method Details
#any_belongs_to?(node, association: ) ⇒ Array<RuboCop::AST::Node>?
Match a class with belongs_to
with no regard to foreign_key
option
106 107 108 109 110 111 112 113 |
# File 'lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb', line 106 def_node_matcher :any_belongs_to?, <<~PATTERN (begin < $(send nil? :belongs_to (sym %association) ...) ... > ) PATTERN |
#belongs_to?(node, key: , fk: ) ⇒ Array<RuboCop::AST::Node>
Match a class with a matching association, either by name or an explicit
foreign_key
option
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb', line 132 def_node_matcher :belongs_to?, <<~PATTERN (begin < ${ #belongs_to_without_fk?(%key) # belongs_to :user #belongs_to_with_a_matching_fk?(%fk) # belongs_to :author, foreign_key: :user_id } ... > ) PATTERN |
#belongs_to_with_a_matching_fk?(node, fk) ⇒ Array<RuboCop::AST::Node>
Match a matching belongs_to
association with a matching explicit foreign_key
option
167 168 169 |
# File 'lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb', line 167 def_node_matcher :belongs_to_with_a_matching_fk?, <<~PATTERN (send nil? :belongs_to ... (hash <(pair (sym :foreign_key) (sym %1)) ...>)) PATTERN |
#belongs_to_without_fk?(node, key) ⇒ Array<RuboCop::AST::Node>
Match a matching belongs_to
association, without an explicit foreign_key
option
150 151 152 153 154 155 156 |
# File 'lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb', line 150 def_node_matcher :belongs_to_without_fk?, <<~PATTERN { (send nil? :belongs_to (sym %1)) # belongs_to :user (send nil? :belongs_to (sym %1) !hash ...) # belongs_to :user, -> { not_deleted } (send nil? :belongs_to (sym %1) !(hash <(pair (sym :foreign_key) _) ...>)) } PATTERN |
#on_send(node) ⇒ Object
171 172 173 174 175 176 177 178 |
# File 'lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb', line 171 def on_send(node) presence_validation?(node) do |all_keys, , presence| keys = non_optional_belongs_to(node.parent, all_keys) return if keys.none? add_offense_and_correct(node, all_keys, keys, , presence) end end |
#optional?(node) ⇒ Object
Match a belongs_to
association with an optional option in a hash
81 82 83 |
# File 'lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb', line 81 def_node_matcher :optional?, <<~PATTERN (send nil? :belongs_to _ ... #optional_option?) PATTERN |
#optional_option?(node) ⇒ Object
Match an optional option in a hash
87 88 89 90 91 92 |
# File 'lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb', line 87 def_node_matcher :optional_option?, <<~PATTERN { (hash <(pair (sym :optional) true) ...>) # optional: true (hash <(pair (sym :required) false) ...>) # required: false } PATTERN |
#presence_validation?(node) ⇒ Object
Match a validates
statement with a presence check
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb', line 67 def_node_matcher :presence_validation?, <<~PATTERN ( send nil? :validates (sym $_)+ $[ (hash <$(pair (sym :presence) true) ...>) # presence: true !(hash <$(pair (sym :strict) {true const}) ...>) # strict: true !(hash <$(pair (sym {:if :unless}) _) ...>) # if: some_condition or unless: some_condition ] ) PATTERN |