class RuboCop::Cop::Rails::SkipsModelValidations
This cop checks for the use of methods which skip validations which are listed in guides.rubyonrails.org/active_record_validations.html#skipping-validations
Methods may be ignored from this rule by configuring a `AllowedMethods`.
@example
# bad Article.first.decrement!(:view_count) DiscussionBoard.decrement_counter(:post_count, 5) Article.first.increment!(:view_count) DiscussionBoard.increment_counter(:post_count, 5) person.toggle :active product.touch Billing.update_all("category = 'authorized', author = 'David'") user.update_attribute(:website, 'example.com') user.update_columns(last_request_at: Time.current) Post.update_counters 5, comment_count: -1, action_count: 1 # good user.update(website: 'example.com') FileUtils.touch('file')
@example AllowedMethods: [“touch”]
# bad DiscussionBoard.decrement_counter(:post_count, 5) DiscussionBoard.increment_counter(:post_count, 5) person.toggle :active # good user.touch
Constants
- METHODS_WITH_ARGUMENTS
- MSG
Public Class Methods
new(*)
click to toggle source
Calls superclass method
# File lib/rubocop/cop/rails/skips_model_validations.rb, line 83 def initialize(*) super @displayed_allowed_warning = false @displayed_forbidden_warning = false end
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/rails/skips_model_validations.rb, line 72 def on_send(node) return if allowed_methods.include?(node.method_name.to_s) return unless forbidden_methods.include?(node.method_name.to_s) return if allowed_method?(node) return if good_touch?(node) return if good_insert?(node) add_offense(node.loc.selector, message: message(node)) end
Also aliased as: on_csend
Private Instance Methods
allowed_method?(node)
click to toggle source
# File lib/rubocop/cop/rails/skips_model_validations.rb, line 95 def allowed_method?(node) METHODS_WITH_ARGUMENTS.include?(node.method_name.to_s) && !node.arguments? end
allowed_methods()
click to toggle source
# File lib/rubocop/cop/rails/skips_model_validations.rb, line 111 def allowed_methods obsolete_result = cop_config['Whitelist'] if obsolete_result warn '`Whitelist` has been renamed to `AllowedMethods`.' unless @displayed_allowed_warning @displayed_allowed_warning = true return obsolete_result end cop_config['AllowedMethods'] || [] end
forbidden_methods()
click to toggle source
# File lib/rubocop/cop/rails/skips_model_validations.rb, line 100 def forbidden_methods obsolete_result = cop_config['Blacklist'] if obsolete_result warn '`Blacklist` has been renamed to `ForbiddenMethods`.' unless @displayed_forbidden_warning @displayed_forbidden_warning = true return obsolete_result end cop_config['ForbiddenMethods'] || [] end
message(node)
click to toggle source
# File lib/rubocop/cop/rails/skips_model_validations.rb, line 91 def message(node) format(MSG, method: node.method_name) end