Apply Rails/SaveBang rubocop rule

Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
This commit is contained in:
Javi Martín
2019-10-20 03:54:56 +02:00
parent 777fb55399
commit 7ca55c44e0
167 changed files with 645 additions and 645 deletions

View File

@@ -15,7 +15,7 @@ class Activity < ApplicationRecord
scope :for_render, -> { includes(user: [:moderator, :administrator]).includes(:actionable) }
def self.log(user, action, actionable)
create(user: user, action: action.to_s, actionable: actionable)
create!(user: user, action: action.to_s, actionable: actionable)
end
def self.on(actionable)

View File

@@ -30,7 +30,7 @@ class AdminNotification < ApplicationRecord
def deliver
list_of_recipients.each { |user| Notification.add(user, self) }
self.update(sent_at: Time.current, recipients_count: list_of_recipients.count)
update!(sent_at: Time.current, recipients_count: list_of_recipients.count)
end
private

View File

@@ -256,7 +256,7 @@ class Budget
def send_unfeasible_email
Mailer.budget_investment_unfeasible(self).deliver_later
update(unfeasible_email_sent_at: Time.current)
update!(unfeasible_email_sent_at: Time.current)
end
def reason_for_not_being_selectable_by(user)

View File

@@ -42,7 +42,7 @@ class Budget
def set_winner
@money_spent += @current_investment.price
@current_investment.update(winner: true)
@current_investment.update!(winner: true)
end
def winners

View File

@@ -7,7 +7,7 @@ module Communitable
end
def associate_community
community = Community.create
community = Community.create!
self.community_id = community.id
end

View File

@@ -7,10 +7,10 @@ class DownloadSetting < ApplicationRecord
name_field: field_name,
config: config)
if download_setting.nil?
download_setting = DownloadSetting.create(downloadable: false,
name_model: model.name,
name_field: field_name,
config: config)
download_setting = DownloadSetting.create!(downloadable: false,
name_model: model.name,
name_field: field_name,
config: config)
end
download_setting
end

View File

@@ -12,7 +12,7 @@ class Flag < ApplicationRecord
def self.flag(user, flaggable)
return false if flagged?(user, flaggable)
create(user: user, flaggable: flaggable)
create!(user: user, flaggable: flaggable)
end
def self.unflag(user, flaggable)

View File

@@ -5,6 +5,6 @@ class Identity < ApplicationRecord
validates :uid, presence: true, uniqueness: { scope: :provider }
def self.first_or_create_from_oauth(auth)
where(uid: auth.uid, provider: auth.provider).first_or_create
where(uid: auth.uid, provider: auth.provider).first_or_create!
end
end

View File

@@ -45,7 +45,7 @@ class LocalCensusRecords::Import
if local_census_record.invalid?
invalid_records << local_census_record
else
local_census_record.save
local_census_record.save!
created_records << local_census_record
end
end

View File

@@ -21,7 +21,7 @@ class Lock < ApplicationRecord
end
def self.increase_tries(user)
Lock.find_or_create_by(user: user).increment!(:tries).save
find_or_create_by!(user: user).increment!(:tries).save!
end
def self.max_tries

View File

@@ -29,7 +29,7 @@ class Officing::Residence
if user_exists?
self.user = find_user_by_document
user.update(verified_at: Time.current)
user.update!(verified_at: Time.current)
else
user_params = {
document_number: document_number,

View File

@@ -27,7 +27,7 @@ class Poll::BallotSheet < ApplicationRecord
def create_ballots(investment_ids, index)
poll_ballot = Poll::Ballot.where(ballot_sheet: self,
data: investment_ids,
external_id: index).first_or_create
external_id: index).first_or_create!
create_ballot(poll_ballot)
poll_ballot
end
@@ -36,7 +36,7 @@ class Poll::BallotSheet < ApplicationRecord
Budget::Ballot.where(physical: true,
user: nil,
poll_ballot: poll_ballot,
budget: poll.budget).first_or_create
budget: poll.budget).first_or_create!
end
end

View File

@@ -73,12 +73,12 @@ class Poll::Question::Answer < ApplicationRecord
answers = question.question_answers.visibles
.map { |a| count_positive_negative(a, true) - count_positive_negative(a, false) }
is_most_voted = answers.none? { |a| a > total_votes_positive_negative }
update(most_voted: is_most_voted)
update!(most_voted: is_most_voted)
when "prioritized"
answers = question.question_answers.visibles
.map { |a| Poll::Answer.where(question_id: a.question, answer: a.title).sum(:value) }
is_most_voted = answers.none? { |a| a > total_votes_prioritized }
update(most_voted: is_most_voted)
update!(most_voted: is_most_voted)
else
for_only_votes
end
@@ -95,7 +95,7 @@ class Poll::Question::Answer < ApplicationRecord
answers = question.question_answers.visibles
.map { |a| Poll::Answer.where(question_id: a.question, answer: a.title).count }
is_most_voted = answers.none? { |a| a > total_votes }
update(most_voted: is_most_voted)
update!(most_voted: is_most_voted)
end
end

View File

@@ -96,7 +96,7 @@ class Proposal < ApplicationRecord
end
def publish
update(published_at: Time.current)
update!(published_at: Time.current)
send_new_actions_notification_on_published
end

View File

@@ -28,10 +28,10 @@ class SignatureSheet < ApplicationRecord
signature = signatures.where(document_number: document_number,
date_of_birth: date_of_birth,
postal_code: postal_code).first_or_create
postal_code: postal_code).first_or_create!
signature.verify
end
update(processed: true)
update!(processed: true)
end
def parsed_required_fields_to_verify_groups

View File

@@ -181,11 +181,11 @@ class User < ApplicationRecord
def add_official_position!(position, level)
return if position.blank? || level.blank?
update official_position: position, official_level: level.to_i
update! official_position: position, official_level: level.to_i
end
def remove_official_position!
update official_position: nil, official_level: 0
update! official_position: nil, official_level: 0
end
def has_official_email?
@@ -215,7 +215,7 @@ class User < ApplicationRecord
end
def erase(erase_reason = nil)
update(
update!(
erased_at: Time.current,
erase_reason: erase_reason,
username: nil,
@@ -241,7 +241,7 @@ class User < ApplicationRecord
.where(document_type: document_type).first
if erased_user.present?
take_votes_from(erased_user)
erased_user.update(document_number: nil, document_type: nil)
erased_user.update!(document_number: nil, document_type: nil)
end
end
@@ -251,11 +251,11 @@ class User < ApplicationRecord
Budget::Ballot.where(user_id: other_user.id).update_all(user_id: id)
Vote.where("voter_id = ? AND voter_type = ?", other_user.id, "User").update_all(voter_id: id)
data_log = "id: #{other_user.id} - #{Time.current.strftime("%Y-%m-%d %H:%M:%S")}"
update(former_users_data_log: "#{former_users_data_log} | #{data_log}")
update!(former_users_data_log: "#{former_users_data_log} | #{data_log}")
end
def locked?
Lock.find_or_create_by(user: self).locked?
Lock.find_or_create_by!(user: self).locked?
end
def self.search(term)
@@ -315,11 +315,11 @@ class User < ApplicationRecord
def save_requiring_finish_signup
begin
self.registering_with_oauth = true
save(validate: false)
save!(validate: false)
# Devise puts unique constraints for the email the db, so we must detect & handle that
rescue ActiveRecord::RecordNotUnique
self.email = nil
save(validate: false)
save!(validate: false)
end
true
end

View File

@@ -15,7 +15,7 @@ class Verification::Email
return false unless valid?
generate_token
user.update(email_verification_token: @plain_token)
user.update!(email_verification_token: @plain_token)
end
def user

View File

@@ -25,11 +25,11 @@ class Verification::Management::Email
plain_token, encrypted_token = Devise.token_generator.generate(User, :email_verification_token)
user.update(document_type: document_type,
document_number: document_number,
residence_verified_at: Time.current,
level_two_verified_at: Time.current,
email_verification_token: plain_token)
user.update!(document_type: document_type,
document_number: document_number,
residence_verified_at: Time.current,
level_two_verified_at: Time.current,
email_verification_token: plain_token)
Mailer.email_verification(user, email, encrypted_token, document_type, document_number).deliver_later
true

View File

@@ -81,9 +81,9 @@ class VotationType < ApplicationRecord
result = votes.by_author(user.id).find_by(answer: answer)
if result.nil?
if check_max_votes(user, votes)
result = votes.create(author: user,
answer: answer,
positive: options[:positive])
result = votes.create!(author: user,
answer: answer,
positive: options[:positive])
end
else
!result.update(positive: options[:positive])
@@ -91,7 +91,7 @@ class VotationType < ApplicationRecord
when "answer_couples_closed", "answer_couples_open"
if check_max_votes(user, votes)
result = votes.create(
result = votes.create!(
answer: answer,
author: user,
positive: true,
@@ -114,7 +114,7 @@ class VotationType < ApplicationRecord
return if questionable.question_answers.where(title: answer).any?
questionable.question_answers
.create(
.create!(
title: answer,
given_order: questionable.question_answers.maximum(:given_order).to_i + 1,
hidden: hidden
@@ -140,7 +140,7 @@ class VotationType < ApplicationRecord
def self.create_by_type(questionable, params)
votation_type = build_by_type(questionable, params)
votation_type.save
votation_type.save!
end
def update_priorized_values(user)

View File

@@ -13,7 +13,7 @@ class Widget::Feed < ApplicationRecord
def self.active
KINDS.collect do |kind|
feed = find_or_create_by(kind: kind)
feed = find_or_create_by!(kind: kind)
feed if feed.active?
end.compact
end