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

@@ -67,7 +67,7 @@ shared_examples "acts as imageable" do |imageable_factory|
end
it "image destroy should remove image from file storage" do
image.save
image.save!
image_url = image.attachment.url
new_url = "/attachments/original/missing.png"

View File

@@ -2,7 +2,7 @@ shared_examples "acts as paranoid" do |factory_name|
let!(:resource) { create(factory_name) }
it "#{described_class} can be recovered after soft deletion" do
resource.destroy
resource.destroy!
resource.reload
expect(resource.hidden_at).not_to be_blank
@@ -15,7 +15,7 @@ shared_examples "acts as paranoid" do |factory_name|
describe "#{described_class} translations" do
it "is hidden after parent resource destroy" do
resource.destroy
resource.destroy!
resource.reload
expect(resource.translations.with_deleted.first.hidden_at).not_to be_blank
@@ -26,16 +26,16 @@ shared_examples "acts as paranoid" do |factory_name|
end
it "cannot be recovered through non recursive restore" do
resource.destroy
resource.destroy!
resource.reload
expect { resource.restore }.not_to change { resource.translations.with_deleted.first.hidden_at }
end
it "can be recovered through recursive restore after non-recursive restore" do
resource.destroy
resource.destroy!
resource.restore
resource.destroy
resource.destroy!
resource.reload
expect { resource.restore(recursive: true) }.to change { resource.translations.with_deleted.first.hidden_at }
@@ -48,10 +48,10 @@ shared_examples "acts as paranoid" do |factory_name|
new_translation.send("#{translated_attribute_name}=", original_translation.send(translated_attribute_name))
end
new_translation.locale = :fr
new_translation.save
new_translation.save!
expect(resource.translations.with_deleted.count).to eq(2)
resource.destroy
resource.destroy!
resource.reload
expect(resource.translations.with_deleted.count).to eq(2)
expect(resource.translations.with_deleted.first.hidden_at).not_to be_blank

View File

@@ -47,14 +47,14 @@ shared_examples "document validations" do |documentable_factory|
end
it "is not valid without a documentable_id" do
document.save
document.save!
document.documentable_id = nil
expect(document).not_to be_valid
end
it "is not valid without a documentable_type" do
document.save
document.save!
document.documentable_type = nil
expect(document).not_to be_valid

View File

@@ -55,14 +55,14 @@ shared_examples "image validations" do |imageable_factory|
end
it "is not valid without a imageable_id" do
image.save
image.save!
image.imageable_id = nil
expect(image).not_to be_valid
end
it "is not valid without a imageable_type" do
image.save
image.save!
image.imageable_type = nil
expect(image).not_to be_valid

View File

@@ -51,7 +51,7 @@ shared_examples "map validations" do
it "expires cache when the map is updated" do
map_location = create(:map_location)
mappable.map_location = map_location
mappable.save
mappable.save!
expect { map_location.update(latitude: 12.34) }
.to change { mappable.reload.updated_at }

View File

@@ -78,7 +78,7 @@ shared_examples "notifiable" do
notification = create(:notification, notifiable: notifiable)
if notifiable.respond_to?(:retired_at)
notifiable.update(retired_at: Time.current, retired_reason: "unfeasible",
notifiable.update!(retired_at: Time.current, retired_reason: "unfeasible",
retired_explanation: "Unfeasibility explanation ...")
expect(notification.check_availability(notifiable)).to be(false)
end