Use update! instead of update_attributes!

It's what we use almost everywhere, and it's shorter.
This commit is contained in:
Javi Martín
2019-10-21 23:27:03 +02:00
parent 7ca55c44e0
commit 49c3402833
10 changed files with 93 additions and 93 deletions

View File

@@ -37,7 +37,7 @@ class Signature < ApplicationRecord
def assign_signature_to_vote def assign_signature_to_vote
vote = Vote.where(votable: signable, voter: user).first vote = Vote.where(votable: signable, voter: user).first
vote&.update(signature: self) vote&.update!(signature: self)
end end
def user_exists? def user_exists?

View File

@@ -158,7 +158,7 @@ describe "Admin legislation questions" do
context "Special translation behaviour" do context "Special translation behaviour" do
before do before do
question.update_attributes(title_en: "Title in English", title_es: "Título en Español") question.update!(title_en: "Title in English", title_es: "Título en Español")
end end
scenario "Add translation for question option", :js do scenario "Add translation for question option", :js do

View File

@@ -38,7 +38,7 @@ describe "Budgets" do
heading1 = create(:budget_heading, group: group1) heading1 = create(:budget_heading, group: group1)
heading2 = create(:budget_heading, group: group2) heading2 = create(:budget_heading, group: group2)
budget.update_attributes!(phase: "informing") budget.update!(phase: "informing")
visit budgets_path visit budgets_path
@@ -51,7 +51,7 @@ describe "Budgets" do
expect(page).to have_link("See all phases") expect(page).to have_link("See all phases")
end end
budget.update_attributes!(phase: "publishing_prices") budget.update!(phase: "publishing_prices")
visit budgets_path visit budgets_path
within("#budget_heading") do within("#budget_heading") do
@@ -114,7 +114,7 @@ describe "Budgets" do
end end
scenario "Show informing index without links" do scenario "Show informing index without links" do
budget.update_attributes!(phase: "informing") budget.update!(phase: "informing")
heading = create(:budget_heading, budget: budget) heading = create(:budget_heading, budget: budget)
visit budgets_path visit budgets_path
@@ -132,7 +132,7 @@ describe "Budgets" do
end end
scenario "Show finished index without heading links" do scenario "Show finished index without heading links" do
budget.update_attributes!(phase: "finished") budget.update!(phase: "finished")
heading = create(:budget_heading, budget: budget) heading = create(:budget_heading, budget: budget)
visit budgets_path visit budgets_path

View File

@@ -226,7 +226,7 @@ describe "Commenting legislation questions" do
end end
scenario "Can't create comments if debate phase is not open", :js do scenario "Can't create comments if debate phase is not open", :js do
process.update_attributes!(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.day) process.update!(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.day)
login_as(user) login_as(user)
visit legislation_process_question_path(legislation_question.process, legislation_question) visit legislation_process_question_path(legislation_question.process, legislation_question)

View File

@@ -160,7 +160,7 @@ describe "Notifications" do
end end
scenario "With internal link" do scenario "With internal link" do
admin_notification.update_attributes!(link: "/stats") admin_notification.update!(link: "/stats")
visit notifications_path visit notifications_path
expect(page).to have_content("Notification title") expect(page).to have_content("Notification title")
@@ -171,7 +171,7 @@ describe "Notifications" do
end end
scenario "Without a link" do scenario "Without a link" do
admin_notification.update_attributes!(link: "/stats") admin_notification.update!(link: "/stats")
visit notifications_path visit notifications_path
expect(page).to have_content("Notification title") expect(page).to have_content("Notification title")

View File

@@ -132,35 +132,35 @@ describe Budget::Phase do
describe "when being enabled" do describe "when being enabled" do
before do before do
second_phase.update_attributes(enabled: false, second_phase.update!(enabled: false,
starts_at: Date.current, starts_at: Date.current,
ends_at: Date.current + 2.days) ends_at: Date.current + 2.days)
end end
it "adjusts previous enabled phase end date to its own start date" do it "adjusts previous enabled phase end date to its own start date" do
expect { second_phase.update_attributes(enabled: true) } expect { second_phase.update(enabled: true) }
.to change { prev_enabled_phase.ends_at.to_date }.to(Date.current) .to change { prev_enabled_phase.ends_at.to_date }.to(Date.current)
end end
it "adjusts next enabled phase start date to its own end date" do it "adjusts next enabled phase start date to its own end date" do
expect do expect do
second_phase.update_attributes(enabled: true) second_phase.update(enabled: true)
end.to change { next_enabled_phase.starts_at.to_date }.to(Date.current + 2.days) end.to change { next_enabled_phase.starts_at.to_date }.to(Date.current + 2.days)
end end
end end
describe "when disabled" do describe "when disabled" do
before do before do
second_phase.update_attributes(enabled: false) second_phase.update!(enabled: false)
end end
it "doesn't change previous enabled phase end date" do it "doesn't change previous enabled phase end date" do
expect { second_phase.update_attributes(starts_at: Date.current, ends_at: Date.current + 2.days) } expect { second_phase.update(starts_at: Date.current, ends_at: Date.current + 2.days) }
.not_to change { prev_enabled_phase.ends_at } .not_to change { prev_enabled_phase.ends_at }
end end
it "doesn't change next enabled phase start date" do it "doesn't change next enabled phase start date" do
expect { second_phase.update_attributes(starts_at: Date.current, ends_at: Date.current + 2.days) } expect { second_phase.update(starts_at: Date.current, ends_at: Date.current + 2.days) }
.not_to change { next_enabled_phase.starts_at } .not_to change { next_enabled_phase.starts_at }
end end
end end
@@ -168,17 +168,17 @@ describe Budget::Phase do
describe "when being disabled" do describe "when being disabled" do
it "doesn't adjust previous enabled phase end date to its own start date" do it "doesn't adjust previous enabled phase end date to its own start date" do
expect do expect do
second_phase.update_attributes(enabled: false, second_phase.update(enabled: false,
starts_at: Date.current, starts_at: Date.current,
ends_at: Date.current + 2.days) ends_at: Date.current + 2.days)
end.not_to change { prev_enabled_phase.ends_at } end.not_to change { prev_enabled_phase.ends_at }
end end
it "adjusts next enabled phase start date to its own start date" do it "adjusts next enabled phase start date to its own start date" do
expect do expect do
second_phase.update_attributes(enabled: false, second_phase.update(enabled: false,
starts_at: Date.current, starts_at: Date.current,
ends_at: Date.current + 2.days) ends_at: Date.current + 2.days)
end.to change { next_enabled_phase.starts_at.to_date }.to(Date.current) end.to change { next_enabled_phase.starts_at.to_date }.to(Date.current)
end end
end end
@@ -186,7 +186,7 @@ describe Budget::Phase do
describe "next & prev enabled phases" do describe "next & prev enabled phases" do
before do before do
second_phase.update_attributes(enabled: false) second_phase.update(enabled: false)
end end
describe "#next_enabled_phase" do describe "#next_enabled_phase" do

View File

@@ -15,14 +15,14 @@ shared_examples_for "sluggable" do |updatable_slug_trait:|
context "slug updating condition is true" do context "slug updating condition is true" do
it "slug is updated" do it "slug is updated" do
updatable = create(factory_name, updatable_slug_trait, name: "Old Name") updatable = create(factory_name, updatable_slug_trait, name: "Old Name")
expect { updatable.update_attributes(name: "New Name") } expect { updatable.update(name: "New Name") }
.to change { updatable.slug }.from("old-name").to("new-name") .to change { updatable.slug }.from("old-name").to("new-name")
end end
end end
context "slug updating condition is false" do context "slug updating condition is false" do
it "slug isn't updated" do it "slug isn't updated" do
expect { sluggable.update_attributes(name: "New Name") } expect { sluggable.update(name: "New Name") }
.not_to change { sluggable.slug } .not_to change { sluggable.slug }
end end
end end

View File

@@ -8,7 +8,7 @@ RSpec.describe Legislation::Process::Phase, type: :model do
it "checks debate phase" do it "checks debate phase" do
expect(process.debate_phase.enabled?).to be true expect(process.debate_phase.enabled?).to be true
process.update_attributes!(debate_phase_enabled: false) process.update!(debate_phase_enabled: false)
expect(process.debate_phase.enabled?).to be false expect(process.debate_phase.enabled?).to be false
end end
@@ -16,14 +16,14 @@ RSpec.describe Legislation::Process::Phase, type: :model do
expect(process.draft_phase.enabled?).to be false expect(process.draft_phase.enabled?).to be false
expect(process_in_draft_phase.draft_phase.enabled?).to be true expect(process_in_draft_phase.draft_phase.enabled?).to be true
process.update_attributes!(draft_phase_enabled: false) process.update!(draft_phase_enabled: false)
expect(process.draft_phase.enabled?).to be false expect(process.draft_phase.enabled?).to be false
end end
it "checks allegations phase" do it "checks allegations phase" do
expect(process.allegations_phase.enabled?).to be true expect(process.allegations_phase.enabled?).to be true
process.update_attributes!(allegations_phase_enabled: false) process.update!(allegations_phase_enabled: false)
expect(process.allegations_phase.enabled?).to be false expect(process.allegations_phase.enabled?).to be false
end end
end end
@@ -31,67 +31,67 @@ RSpec.describe Legislation::Process::Phase, type: :model do
describe "#started?" do describe "#started?" do
it "checks debate phase" do it "checks debate phase" do
# future # future
process.update_attributes!(debate_start_date: Date.current + 2.days, process.update!(debate_start_date: Date.current + 2.days,
debate_end_date: Date.current + 3.days) debate_end_date: Date.current + 3.days)
expect(process.debate_phase.started?).to be false expect(process.debate_phase.started?).to be false
# started # started
process.update_attributes!(debate_start_date: Date.current - 2.days, process.update!(debate_start_date: Date.current - 2.days,
debate_end_date: Date.current + 1.day) debate_end_date: Date.current + 1.day)
expect(process.debate_phase.started?).to be true expect(process.debate_phase.started?).to be true
# starts today # starts today
process.update_attributes!(debate_start_date: Date.current, process.update!(debate_start_date: Date.current,
debate_end_date: Date.current + 1.day) debate_end_date: Date.current + 1.day)
expect(process.debate_phase.started?).to be true expect(process.debate_phase.started?).to be true
# past # past
process.update_attributes!(debate_start_date: Date.current - 2.days, process.update!(debate_start_date: Date.current - 2.days,
debate_end_date: Date.current - 1.day) debate_end_date: Date.current - 1.day)
expect(process.debate_phase.started?).to be true expect(process.debate_phase.started?).to be true
end end
it "checks draft phase" do it "checks draft phase" do
# future # future
process.update_attributes!(draft_start_date: Date.current + 2.days, process.update!(draft_start_date: Date.current + 2.days,
draft_end_date: Date.current + 3.days, draft_phase_enabled: true) draft_end_date: Date.current + 3.days, draft_phase_enabled: true)
expect(process.draft_phase.started?).to be false expect(process.draft_phase.started?).to be false
# started # started
process.update_attributes!(draft_start_date: Date.current - 2.days, process.update!(draft_start_date: Date.current - 2.days,
draft_end_date: Date.current + 1.day, draft_phase_enabled: true) draft_end_date: Date.current + 1.day, draft_phase_enabled: true)
expect(process.draft_phase.started?).to be true expect(process.draft_phase.started?).to be true
# starts today # starts today
process.update_attributes!(draft_start_date: Date.current, process.update!(draft_start_date: Date.current,
draft_end_date: Date.current + 1.day, draft_phase_enabled: true) draft_end_date: Date.current + 1.day, draft_phase_enabled: true)
expect(process.draft_phase.started?).to be true expect(process.draft_phase.started?).to be true
# past # past
process.update_attributes!(draft_start_date: Date.current - 2.days, process.update!(draft_start_date: Date.current - 2.days,
draft_end_date: Date.current - 1.day, draft_phase_enabled: true) draft_end_date: Date.current - 1.day, draft_phase_enabled: true)
expect(process.draft_phase.started?).to be true expect(process.draft_phase.started?).to be true
end end
it "checks allegations phase" do it "checks allegations phase" do
# future # future
process.update_attributes!(allegations_start_date: Date.current + 2.days, process.update!(allegations_start_date: Date.current + 2.days,
allegations_end_date: Date.current + 3.days) allegations_end_date: Date.current + 3.days)
expect(process.allegations_phase.started?).to be false expect(process.allegations_phase.started?).to be false
# started # started
process.update_attributes!(allegations_start_date: Date.current - 2.days, process.update!(allegations_start_date: Date.current - 2.days,
allegations_end_date: Date.current + 1.day) allegations_end_date: Date.current + 1.day)
expect(process.allegations_phase.started?).to be true expect(process.allegations_phase.started?).to be true
# starts today # starts today
process.update_attributes!(allegations_start_date: Date.current, process.update!(allegations_start_date: Date.current,
allegations_end_date: Date.current + 1.day) allegations_end_date: Date.current + 1.day)
expect(process.allegations_phase.started?).to be true expect(process.allegations_phase.started?).to be true
# past # past
process.update_attributes!(allegations_start_date: Date.current - 2.days, process.update!(allegations_start_date: Date.current - 2.days,
allegations_end_date: Date.current - 1.day) allegations_end_date: Date.current - 1.day)
expect(process.allegations_phase.started?).to be true expect(process.allegations_phase.started?).to be true
end end
end end
@@ -99,68 +99,68 @@ RSpec.describe Legislation::Process::Phase, type: :model do
describe "#open?" do describe "#open?" do
it "checks debate phase" do it "checks debate phase" do
# future # future
process.update_attributes!(debate_start_date: Date.current + 2.days, process.update!(debate_start_date: Date.current + 2.days,
debate_end_date: Date.current + 3.days) debate_end_date: Date.current + 3.days)
expect(process.debate_phase.open?).to be false expect(process.debate_phase.open?).to be false
# started # started
process.update_attributes!(debate_start_date: Date.current - 2.days, process.update!(debate_start_date: Date.current - 2.days,
debate_end_date: Date.current + 1.day) debate_end_date: Date.current + 1.day)
expect(process.debate_phase.open?).to be true expect(process.debate_phase.open?).to be true
# starts today # starts today
process.update_attributes!(debate_start_date: Date.current, process.update!(debate_start_date: Date.current,
debate_end_date: Date.current + 1.day) debate_end_date: Date.current + 1.day)
expect(process.debate_phase.open?).to be true expect(process.debate_phase.open?).to be true
# past # past
process.update_attributes!(debate_start_date: Date.current - 2.days, process.update!(debate_start_date: Date.current - 2.days,
debate_end_date: Date.current - 1.day) debate_end_date: Date.current - 1.day)
expect(process.debate_phase.open?).to be false expect(process.debate_phase.open?).to be false
end end
it "checks draft phase" do it "checks draft phase" do
# future # future
process.update_attributes!(draft_start_date: Date.current + 2.days, process.update!(draft_start_date: Date.current + 2.days,
draft_end_date: Date.current + 3.days, draft_phase_enabled: true) draft_end_date: Date.current + 3.days, draft_phase_enabled: true)
expect(process.draft_phase.open?).to be false expect(process.draft_phase.open?).to be false
# started # started
process.update_attributes!(draft_start_date: Date.current - 2.days, process.update!(draft_start_date: Date.current - 2.days,
draft_end_date: Date.current + 1.day, draft_phase_enabled: true) draft_end_date: Date.current + 1.day, draft_phase_enabled: true)
expect(process.draft_phase.open?).to be true expect(process.draft_phase.open?).to be true
# starts today # starts today
process.update_attributes!(draft_start_date: Date.current, process.update!(draft_start_date: Date.current,
draft_end_date: Date.current + 1.day, draft_phase_enabled: true) draft_end_date: Date.current + 1.day, draft_phase_enabled: true)
expect(process.draft_phase.open?).to be true expect(process.draft_phase.open?).to be true
# past # past
process.update_attributes!(draft_start_date: Date.current - 2.days, process.update!(draft_start_date: Date.current - 2.days,
draft_end_date: Date.current - 1.day, draft_phase_enabled: true) draft_end_date: Date.current - 1.day, draft_phase_enabled: true)
expect(process.draft_phase.open?).to be false expect(process.draft_phase.open?).to be false
end end
it "checks allegations phase" do it "checks allegations phase" do
# future # future
process.update_attributes!(allegations_start_date: Date.current + 2.days, process.update!(allegations_start_date: Date.current + 2.days,
allegations_end_date: Date.current + 3.days) allegations_end_date: Date.current + 3.days)
expect(process.allegations_phase.open?).to be false expect(process.allegations_phase.open?).to be false
# started # started
process.update_attributes!(allegations_start_date: Date.current - 2.days, process.update!(allegations_start_date: Date.current - 2.days,
allegations_end_date: Date.current + 1.day) allegations_end_date: Date.current + 1.day)
expect(process.allegations_phase.open?).to be true expect(process.allegations_phase.open?).to be true
# starts today # starts today
process.update_attributes!(allegations_start_date: Date.current, process.update!(allegations_start_date: Date.current,
allegations_end_date: Date.current + 1.day) allegations_end_date: Date.current + 1.day)
expect(process.allegations_phase.open?).to be true expect(process.allegations_phase.open?).to be true
# past # past
process.update_attributes!(allegations_start_date: Date.current - 2.days, process.update!(allegations_start_date: Date.current - 2.days,
allegations_end_date: Date.current - 1.day) allegations_end_date: Date.current - 1.day)
expect(process.allegations_phase.open?).to be false expect(process.allegations_phase.open?).to be false
end end
end end

View File

@@ -7,14 +7,14 @@ RSpec.describe Legislation::Process::Publication, type: :model do
it "checks draft publication" do it "checks draft publication" do
expect(process.draft_publication.enabled?).to be true expect(process.draft_publication.enabled?).to be true
process.update_attributes!(draft_publication_enabled: false) process.update!(draft_publication_enabled: false)
expect(process.draft_publication.enabled?).to be false expect(process.draft_publication.enabled?).to be false
end end
it "checks result publication" do it "checks result publication" do
expect(process.result_publication.enabled?).to be true expect(process.result_publication.enabled?).to be true
process.update_attributes!(result_publication_enabled: false) process.update!(result_publication_enabled: false)
expect(process.result_publication.enabled?).to be false expect(process.result_publication.enabled?).to be false
end end
end end
@@ -22,29 +22,29 @@ RSpec.describe Legislation::Process::Publication, type: :model do
describe "#started?" do describe "#started?" do
it "checks draft publication" do it "checks draft publication" do
# future # future
process.update_attributes!(draft_publication_date: Date.current + 2.days) process.update!(draft_publication_date: Date.current + 2.days)
expect(process.draft_publication.started?).to be false expect(process.draft_publication.started?).to be false
# past # past
process.update_attributes!(draft_publication_date: Date.current - 2.days) process.update!(draft_publication_date: Date.current - 2.days)
expect(process.draft_publication.started?).to be true expect(process.draft_publication.started?).to be true
# starts today # starts today
process.update_attributes!(draft_publication_date: Date.current) process.update!(draft_publication_date: Date.current)
expect(process.draft_publication.started?).to be true expect(process.draft_publication.started?).to be true
end end
it "checks result publication" do it "checks result publication" do
# future # future
process.update_attributes!(result_publication_date: Date.current + 2.days) process.update!(result_publication_date: Date.current + 2.days)
expect(process.result_publication.started?).to be false expect(process.result_publication.started?).to be false
# past # past
process.update_attributes!(result_publication_date: Date.current - 2.days) process.update!(result_publication_date: Date.current - 2.days)
expect(process.result_publication.started?).to be true expect(process.result_publication.started?).to be true
# starts today # starts today
process.update_attributes!(result_publication_date: Date.current) process.update!(result_publication_date: Date.current)
expect(process.result_publication.started?).to be true expect(process.result_publication.started?).to be true
end end
end end
@@ -52,29 +52,29 @@ RSpec.describe Legislation::Process::Publication, type: :model do
describe "#open?" do describe "#open?" do
it "checks draft publication" do it "checks draft publication" do
# future # future
process.update_attributes!(draft_publication_date: Date.current + 2.days) process.update!(draft_publication_date: Date.current + 2.days)
expect(process.draft_publication.open?).to be false expect(process.draft_publication.open?).to be false
# past # past
process.update_attributes!(draft_publication_date: Date.current - 2.days) process.update!(draft_publication_date: Date.current - 2.days)
expect(process.draft_publication.open?).to be true expect(process.draft_publication.open?).to be true
# starts today # starts today
process.update_attributes!(draft_publication_date: Date.current) process.update!(draft_publication_date: Date.current)
expect(process.draft_publication.open?).to be true expect(process.draft_publication.open?).to be true
end end
it "checks result publication" do it "checks result publication" do
# future # future
process.update_attributes!(result_publication_date: Date.current + 2.days) process.update!(result_publication_date: Date.current + 2.days)
expect(process.result_publication.open?).to be false expect(process.result_publication.open?).to be false
# past # past
process.update_attributes!(result_publication_date: Date.current - 2.days) process.update!(result_publication_date: Date.current - 2.days)
expect(process.result_publication.open?).to be true expect(process.result_publication.open?).to be true
# starts today # starts today
process.update_attributes!(result_publication_date: Date.current) process.update!(result_publication_date: Date.current)
expect(process.result_publication.open?).to be true expect(process.result_publication.open?).to be true
end end
end end

View File

@@ -162,12 +162,12 @@ describe Legislation::Process do
describe "#status" do describe "#status" do
it "detects planned phase" do it "detects planned phase" do
process.update_attributes!(start_date: Date.current + 2.days) process.update!(start_date: Date.current + 2.days)
expect(process.status).to eq(:planned) expect(process.status).to eq(:planned)
end end
it "detects closed phase" do it "detects closed phase" do
process.update_attributes!(end_date: Date.current - 2.days) process.update!(end_date: Date.current - 2.days)
expect(process.status).to eq(:closed) expect(process.status).to eq(:closed)
end end