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

@@ -21,4 +21,10 @@
text-transform: uppercase; text-transform: uppercase;
} }
} }
td time:last-of-type::after,
td small::before {
content: "";
display: block;
}
} }

View File

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

View File

@@ -11,15 +11,7 @@ class Admin::BudgetPhases::PhasesComponent < ApplicationComponent
budget.phases.order(:id) budget.phases.order(:id)
end end
def start_date(phase) def dates(phase)
formatted_date(phase.starts_at) if phase.starts_at.present? Admin::Budgets::DurationComponent.new(phase).dates
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)
end end
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> <tr>
<th><%= t("admin.budgets.index.table_name") %></th> <th><%= t("admin.budgets.index.table_name") %></th>
<th><%= t("admin.budgets.index.table_phase") %></th> <th><%= t("admin.budgets.index.table_phase") %></th>
<th><%= t("admin.budgets.index.table_duration") %></th>
<th><%= t("admin.actions.actions") %></th> <th><%= t("admin.actions.actions") %></th>
</tr> </tr>
</thead> </thead>
@@ -27,8 +28,13 @@
<% end %> <% end %>
<strong><%= budget.name %></strong> <strong><%= budget.name %></strong>
</td> </td>
<td class="small"> <td>
<%= budget.current_phase.name %> <%= budget.current_phase.name %>
<small><%= phase_progress_text(budget) %></small>
</td>
<td>
<%= dates(budget) %>
<%= duration(budget) %>
</td> </td>
<td> <td>
<%= render Admin::Budgets::TableActionsComponent.new(budget) %> <%= render Admin::Budgets::TableActionsComponent.new(budget) %>

View File

@@ -9,4 +9,24 @@ class Admin::Budgets::IndexComponent < ApplicationComponent
def title def title
t("admin.budgets.index.title") t("admin.budgets.index.title")
end 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 end

View File

@@ -71,6 +71,14 @@ class Budget < ApplicationRecord
phases.published.order(:id) phases.published.order(:id)
end end
def starts_at
phases.published.first.starts_at
end
def ends_at
phases.published.last.ends_at
end
def description def description
description_for_phase(phase) description_for_phase(phase)
end end

View File

@@ -76,8 +76,10 @@ en:
help: "Participatory budgets allow citizens to propose and decide directly how to spend part of the budget, with monitoring and rigorous evaluation of proposals by the institution." help: "Participatory budgets allow citizens to propose and decide directly how to spend part of the budget, with monitoring and rigorous evaluation of proposals by the institution."
budget_investments: Manage projects budget_investments: Manage projects
table_completed: Completed table_completed: Completed
table_duration: "Duration"
table_name: Name table_name: Name
table_phase: Phase table_phase: Phase
table_phase_progress: "(%{current_phase_number}/%{total_phases})"
edit_groups: Edit headings groups edit_groups: Edit headings groups
edit_budget: Edit budget edit_budget: Edit budget
admin_ballots: Admin ballots admin_ballots: Admin ballots

View File

@@ -76,8 +76,10 @@ es:
help: "Los presupuestos participativos permiten que los ciudadanos propongan y decidan de manera directa cómo gastar parte del presupuesto, con un seguimiento y evaluación riguroso de las propuestas por parte de la institución." help: "Los presupuestos participativos permiten que los ciudadanos propongan y decidan de manera directa cómo gastar parte del presupuesto, con un seguimiento y evaluación riguroso de las propuestas por parte de la institución."
budget_investments: Gestionar proyectos de gasto budget_investments: Gestionar proyectos de gasto
table_completed: Completado table_completed: Completado
table_duration: "Duración"
table_name: Nombre table_name: Nombre
table_phase: Fase table_phase: Fase
table_phase_progress: "(%{current_phase_number}/%{total_phases})"
edit_groups: Editar grupos de partidas edit_groups: Editar grupos de partidas
edit_budget: Editar presupuesto edit_budget: Editar presupuesto
admin_ballots: Gestionar urnas admin_ballots: Gestionar urnas

View File

@@ -0,0 +1,57 @@
require "rails_helper"
describe Admin::Budgets::DurationComponent, type: :component do
describe "#dates" do
it "shows both dates when both are defined" do
durable = double(
starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0),
ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00)
)
dates = Admin::Budgets::DurationComponent.new(durable).dates
render dates
expect(page.text).to eq "2015-08-01 12:00:00 - 2016-09-30 16:29:59"
expect(dates).to be_html_safe
end
it "shows the start date 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).dates
expect(page.text).to eq "2015-08-01 12:00:00 - "
end
it "shows the end date 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).dates
expect(page.text).to eq "- 2016-09-30 16:29:59"
end
end
describe "#duration" do
it "describes the total duration in human language" do
durable = double(
starts_at: Time.zone.local(2015, 8, 1, 12, 0, 0),
ends_at: Time.zone.local(2016, 9, 30, 16, 30, 00)
)
render Admin::Budgets::DurationComponent.new(durable).duration
expect(page.text).to eq "about 1 year"
end
end
attr_reader :content
def render(content)
@content = content
end
def page
Capybara::Node::Simple.new(content)
end
end