Files
grecia/app/components/admin/budgets/index_component.rb
Javi Martín 69ade5b131 Fix crash with budgets with disabled current phase
There's an edge case where the current phase of the budget was disabled.
In this case, the application was crashing.

I'm not sure what we should do regarding this case. Is it OK to allow
disabling the current phase? Is it OK to allow selecting a disabled
phase as the current phase?

Since I'm not sure about it, for now I'm leaving it the same way it was.

Co-authored-by: Julian Herrero <microweb10@gmail.com>
2021-06-02 18:00:17 +02:00

53 lines
1.2 KiB
Ruby

class Admin::Budgets::IndexComponent < ApplicationComponent
include Header
attr_reader :budgets
def initialize(budgets)
@budgets = budgets
end
def title
t("admin.budgets.index.title")
end
private
def phase_progress_text(budget)
t("admin.budgets.index.table_phase_progress",
current_phase_number: current_enabled_phase_number(budget),
total_phases: budget.phases.enabled.count)
end
def current_enabled_phase_number(budget)
current_enabled_phase_index(budget) + 1
end
def current_enabled_phase_index(budget)
budget.phases.enabled.order(:id).pluck(:kind).index(budget.phase) || -1
end
def dates(budget)
Admin::Budgets::DurationComponent.new(budget).dates
end
def duration(budget)
Admin::Budgets::DurationComponent.new(budget).duration
end
def status_html_class(budget)
if budget.drafting?
"budget-draft"
elsif budget.finished?
"budget-completed"
end
end
def status_text(budget)
if budget.drafting?
tag.span t("admin.budgets.index.table_draft")
elsif budget.finished?
tag.span t("admin.budgets.index.table_completed")
end
end
end