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

@@ -20,24 +20,24 @@ describe Budget::Ballot::Line do
describe "Money" do
it "is not valid if insufficient funds" do
investment.update(price: heading.price + 1)
investment.update!(price: heading.price + 1)
expect(ballot_line).not_to be_valid
end
it "is valid if sufficient funds" do
investment.update(price: heading.price - 1)
investment.update!(price: heading.price - 1)
expect(ballot_line).to be_valid
end
end
describe "Selectibility" do
it "is not valid if investment is unselected" do
investment.update(selected: false)
investment.update!(selected: false)
expect(ballot_line).not_to be_valid
end
it "is valid if investment is selected" do
investment.update(selected: true, price: 20000)
investment.update!(selected: true, price: 20000)
expect(ballot_line).to be_valid
end
end

View File

@@ -19,13 +19,13 @@ describe Budget::Group do
end
it "may be repeated for the same group and a different locale" do
group.update(name_fr: "object name")
group.update!(name_fr: "object name")
expect(group.translations.last).to be_valid
end
it "must not be repeated for a different group in any locale" do
group.update(name_en: "English", name_es: "Español")
group.update!(name_en: "English", name_es: "Español")
expect(build(:budget_group, budget: budget, name_en: "English")).not_to be_valid
expect(build(:budget_group, budget: budget, name_en: "Español")).not_to be_valid

View File

@@ -40,13 +40,13 @@ describe Budget::Heading do
end
it "can be repeated for the same heading and a different locale" do
heading.update(name_fr: "object name")
heading.update!(name_fr: "object name")
expect(heading.translations.last).to be_valid
end
it "must not be repeated for a different heading in any locale" do
heading.update(name_en: "English", name_es: "Español")
heading.update!(name_en: "English", name_es: "Español")
expect(build(:budget_heading, group: group, name_en: "English")).not_to be_valid
expect(build(:budget_heading, group: group, name_en: "Español")).not_to be_valid

View File

@@ -50,7 +50,7 @@ describe Budget::Investment do
expect(investment.budget_id).to eq budget.id
expect(investment.group_id).to eq group_1.id
investment.update(heading: heading_2)
investment.update!(heading: heading_2)
expect(investment.budget_id).to eq budget.id
expect(investment.group_id).to eq group_2.id
@@ -68,7 +68,7 @@ describe Budget::Investment do
expect(investment.previous_heading_id).to eq nil
investment.update(heading: heading_2)
investment.update!(heading: heading_2)
expect(investment.previous_heading_id).to eq heading_1.id
end
@@ -220,7 +220,7 @@ describe Budget::Investment do
it "returns true for selected investments which budget's phase is publishing_prices or later" do
Budget::Phase::PUBLISHED_PRICES_PHASES.each do |phase|
budget.update(phase: phase)
budget.update!(phase: phase)
expect(investment.should_show_price?).to eq(true)
end
@@ -228,7 +228,7 @@ describe Budget::Investment do
it "returns false in any other phase" do
(Budget::Phase::PHASE_KINDS - Budget::Phase::PUBLISHED_PRICES_PHASES).each do |phase|
budget.update(phase: phase)
budget.update!(phase: phase)
expect(investment.should_show_price?).to eq(false)
end
@@ -255,7 +255,7 @@ describe Budget::Investment do
it "returns true for selected with price_explanation & budget in publishing_prices or later" do
Budget::Phase::PUBLISHED_PRICES_PHASES.each do |phase|
budget.update(phase: phase)
budget.update!(phase: phase)
expect(investment.should_show_price_explanation?).to eq(true)
end
@@ -263,7 +263,7 @@ describe Budget::Investment do
it "returns false in any other phase" do
(Budget::Phase::PHASE_KINDS - Budget::Phase::PUBLISHED_PRICES_PHASES).each do |phase|
budget.update(phase: phase)
budget.update!(phase: phase)
expect(investment.should_show_price_explanation?).to eq(false)
end
@@ -290,25 +290,25 @@ describe Budget::Investment do
it "returns true for unfeasible investments with unfeasibility explanation and valuation finished" do
Budget::Phase::PUBLISHED_PRICES_PHASES.each do |phase|
budget.update(phase: phase)
budget.update!(phase: phase)
expect(investment.should_show_unfeasibility_explanation?).to eq(true)
end
end
it "returns false in valuation has not finished" do
investment.update(valuation_finished: false)
investment.update!(valuation_finished: false)
Budget::Phase::PUBLISHED_PRICES_PHASES.each do |phase|
budget.update(phase: phase)
budget.update!(phase: phase)
expect(investment.should_show_unfeasibility_explanation?).to eq(false)
end
end
it "returns false if not unfeasible" do
investment.update(feasibility: "undecided")
investment.update!(feasibility: "undecided")
Budget::Phase::PUBLISHED_PRICES_PHASES.each do |phase|
budget.update(phase: phase)
budget.update!(phase: phase)
expect(investment.should_show_unfeasibility_explanation?).to eq(false)
end
@@ -317,7 +317,7 @@ describe Budget::Investment do
it "returns false if unfeasibility explanation blank" do
investment.unfeasibility_explanation = ""
Budget::Phase::PUBLISHED_PRICES_PHASES.each do |phase|
budget.update(phase: phase)
budget.update!(phase: phase)
expect(investment.should_show_unfeasibility_explanation?).to eq(false)
end
@@ -703,7 +703,7 @@ describe Budget::Investment do
end
it "returns selected investments" do
budget.update(phase: "balloting")
budget.update!(phase: "balloting")
investment1 = create(:budget_investment, :feasible, :selected, budget: budget)
investment2 = create(:budget_investment, :feasible, :selected, budget: budget)
@@ -717,7 +717,7 @@ describe Budget::Investment do
end
it "returns unselected investments" do
budget.update(phase: "balloting")
budget.update!(phase: "balloting")
investment1 = create(:budget_investment, :feasible, :unselected, budget: budget)
investment2 = create(:budget_investment, :feasible, :unselected, budget: budget)
@@ -855,7 +855,7 @@ describe Budget::Investment do
end
it "accepts votes in multiple headings of the same group" do
group.update(max_votable_headings: 2)
group.update!(max_votable_headings: 2)
carabanchel = create(:budget_heading, group: group)
salamanca = create(:budget_heading, group: group)
@@ -867,7 +867,7 @@ describe Budget::Investment do
end
it "accepts votes in any heading previously voted in" do
group.update(max_votable_headings: 2)
group.update!(max_votable_headings: 2)
carabanchel = create(:budget_heading, group: group)
salamanca = create(:budget_heading, group: group)
@@ -927,7 +927,7 @@ describe Budget::Investment do
let(:latina_investment) { create(:budget_investment, heading: latina) }
it "returns true if the user has voted in less headings than the maximum" do
districts.update(max_votable_headings: 2)
districts.update!(max_votable_headings: 2)
create(:vote, votable: carabanchel_investment, voter: user)
@@ -935,7 +935,7 @@ describe Budget::Investment do
end
it "returns false if the user has already voted in the maximum number of headings" do
districts.update(max_votable_headings: 2)
districts.update!(max_votable_headings: 2)
create(:vote, votable: carabanchel_investment, voter: user)
create(:vote, votable: salamanca_investment, voter: user)
@@ -997,7 +997,7 @@ describe Budget::Investment do
it "does not get updated if the user is erased" do
user.erase
user.update(document_number: nil)
user.update!(document_number: nil)
expect(user.document_number).to be_blank
investment.touch
expect(investment.responsible_name).to eq("123456")
@@ -1127,7 +1127,7 @@ describe Budget::Investment do
it "returns false if budget is not balloting phase" do
Budget::Phase::PHASE_KINDS.reject { |phase| phase == "balloting" }.each do |phase|
budget.update(phase: phase)
budget.update!(phase: phase)
investment = create(:budget_investment, budget: budget)
investment.heading = heading2
@@ -1147,7 +1147,7 @@ describe Budget::Investment do
expect(investment.previous_heading_id).to eq(nil)
investment.heading = heading2
investment.save
investment.save!
investment.reload
expect(investment.heading_id).to eq(heading2.id)
@@ -1205,7 +1205,7 @@ describe Budget::Investment do
expect(investment.ballot_lines_count).to eq(3)
investment.heading = heading2
investment.save
investment.save!
investment.reload
expect(investment.ballot_lines_count).to eq(0)
@@ -1219,7 +1219,7 @@ describe Budget::Investment do
expect(investment.ballot_lines_count).to eq(3)
investment.save
investment.save!
investment.reload
expect(investment.ballot_lines_count).to eq(3)

View File

@@ -64,7 +64,7 @@ describe Budget::Result do
create(:budget_investment, :winner, heading: heading, price: 200, ballot_lines_count: 600)
create(:budget_investment, :winner, heading: heading, price: 100, ballot_lines_count: 500)
wrong_win.update(incompatible: true)
wrong_win.update!(incompatible: true)
expect(heading.investments.winners.pluck(:ballot_lines_count)).to match_array([800, 700, 600])
end
@@ -79,7 +79,7 @@ describe Budget::Result do
create(:budget_investment, :winner, heading: heading, price: 200, ballot_lines_count: 600)
create(:budget_investment, :winner, heading: heading, price: 100, ballot_lines_count: 500)
miss.update(incompatible: false)
miss.update!(incompatible: false)
expect(heading.investments.winners.pluck(:ballot_lines_count)).to match_array([900, 800, 700])
end