Include duration in budgets table

This commit is contained in:
Julian Herrero
2020-03-16 12:54:00 +01:00
committed by Javi Martín
parent fe05bfe9ea
commit d2871d7770
10 changed files with 134 additions and 12 deletions

View File

@@ -20,7 +20,7 @@
</td>
<td>
<% if phase.starts_at.present? || phase.ends_at.present? %>
<%= start_date(phase) %> - <%= end_date(phase) %>
<%= dates(phase) %>
<% else %>
<em><%= t("admin.budgets.edit.blank_dates") %></em>
<% end %>

View File

@@ -11,15 +11,7 @@ class Admin::BudgetPhases::PhasesComponent < ApplicationComponent
budget.phases.order(:id)
end
def start_date(phase)
formatted_date(phase.starts_at) if phase.starts_at.present?
end
def end_date(phase)
formatted_date(phase.ends_at - 1.second) if phase.ends_at.present?
end
def formatted_date(time)
time_tag(time, format: :datetime)
def dates(phase)
Admin::Budgets::DurationComponent.new(phase).dates
end
end

View File

@@ -0,0 +1,29 @@
class Admin::Budgets::DurationComponent < ApplicationComponent
attr_reader :durable
def initialize(durable)
@durable = durable
end
def dates
safe_join([formatted_start_date, "-", formatted_end_date], " ")
end
def duration
distance_of_time_in_words(durable.starts_at, durable.ends_at)
end
private
def formatted_start_date
formatted_date(durable.starts_at) if durable.starts_at.present?
end
def formatted_end_date
formatted_date(durable.ends_at - 1.second) if durable.ends_at.present?
end
def formatted_date(time)
time_tag(time, format: :datetime)
end
end

View File

@@ -13,6 +13,7 @@
<tr>
<th><%= t("admin.budgets.index.table_name") %></th>
<th><%= t("admin.budgets.index.table_phase") %></th>
<th><%= t("admin.budgets.index.table_duration") %></th>
<th><%= t("admin.actions.actions") %></th>
</tr>
</thead>
@@ -27,8 +28,13 @@
<% end %>
<strong><%= budget.name %></strong>
</td>
<td class="small">
<td>
<%= budget.current_phase.name %>
<small><%= phase_progress_text(budget) %></small>
</td>
<td>
<%= dates(budget) %>
<%= duration(budget) %>
</td>
<td>
<%= render Admin::Budgets::TableActionsComponent.new(budget) %>

View File

@@ -9,4 +9,24 @@ class Admin::Budgets::IndexComponent < ApplicationComponent
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)
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
end