Fix crash with budgets with no published phases

In this case, the duration of the budget cannot be determined, and the
application was crashing when trying to do so.

Now we're just returning `nil` as duration.
This commit is contained in:
Javi Martín
2021-05-20 14:45:53 +02:00
parent 69ade5b131
commit 93a7d28a82
4 changed files with 29 additions and 4 deletions

View File

@@ -10,7 +10,7 @@ class Admin::Budgets::DurationComponent < ApplicationComponent
end end
def duration def duration
distance_of_time_in_words(durable.starts_at, durable.ends_at) distance_of_time_in_words(durable.starts_at, durable.ends_at) if durable.starts_at && durable.ends_at
end end
private private

View File

@@ -72,11 +72,11 @@ class Budget < ApplicationRecord
end end
def starts_at def starts_at
phases.published.first.starts_at phases.published.first&.starts_at
end end
def ends_at def ends_at
phases.published.last.ends_at phases.published.last&.ends_at
end end
def description def description

View File

@@ -43,6 +43,22 @@ describe Admin::Budgets::DurationComponent, type: :component do
expect(page.text).to eq "about 1 year" expect(page.text).to eq "about 1 year"
end end
it "is not defined when no end date is defined" do
durable = double(starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0), ends_at: nil)
render Admin::Budgets::DurationComponent.new(durable).duration
expect(page.text).to be_empty
end
it "is not defined when no start date is defined" do
durable = double(starts_at: nil, ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00))
render Admin::Budgets::DurationComponent.new(durable).duration
expect(page.text).to be_empty
end
end end
attr_reader :content attr_reader :content
@@ -52,6 +68,6 @@ describe Admin::Budgets::DurationComponent, type: :component do
end end
def page def page
Capybara::Node::Simple.new(content) Capybara::Node::Simple.new(content.to_s)
end end
end end

View File

@@ -15,4 +15,13 @@ describe Admin::Budgets::IndexComponent, type: :component do
expect(page.find("tr", text: "Not enabled phase")).to have_content "0/8" expect(page.find("tr", text: "Not enabled phase")).to have_content "0/8"
end end
it "displays phase zero out of zero for budgets with no enabled phases" do
budget = create(:budget, name: "Without phases")
budget.phases.each { |phase| phase.update!(enabled: false) }
render_inline Admin::Budgets::IndexComponent.new(Budget.page(1))
expect(page.find("tr", text: "Without phases")).to have_content "0/0"
end
end end