Merge pull request #2592 from consul/investment_valuator_groups_csv_table

Add valuator groups  assigned to investments to admin tables & csv export
This commit is contained in:
Alberto Calderón Queimadelos
2018-04-11 22:52:13 +02:00
committed by GitHub
8 changed files with 116 additions and 86 deletions

View File

@@ -20,7 +20,7 @@ class Admin::BudgetInvestmentsController < Admin::BaseController
format.html
format.js
format.csv do
send_data Budget::Investment.to_csv(@investments, headers: true),
send_data Budget::Investment::Exporter.new(@investments).to_csv,
filename: 'budget_investments.csv'
end
end

View File

@@ -13,11 +13,6 @@ module ValuationHelper
ValuatorGroup.order("name ASC").collect { |g| [ g.name, "group_#{g.id}"] }
end
def assigned_valuators(investment)
[investment.valuator_groups.collect(&:name) +
investment.valuators.collect(&:description_or_name)].flatten.join(', ')
end
def assigned_valuators_info(valuators)
case valuators.size
when 0

View File

@@ -1,5 +1,3 @@
require 'csv'
class Budget
class Investment < ActiveRecord::Base
SORTING_OPTIONS = %w(id title supports).freeze
@@ -320,44 +318,12 @@ class Budget
investments
end
def self.to_csv(investments, options = {})
attrs = [I18n.t("admin.budget_investments.index.table_id"),
I18n.t("admin.budget_investments.index.table_title"),
I18n.t("admin.budget_investments.index.table_supports"),
I18n.t("admin.budget_investments.index.table_admin"),
I18n.t("admin.budget_investments.index.table_valuator"),
I18n.t("admin.budget_investments.index.table_geozone"),
I18n.t("admin.budget_investments.index.table_feasibility"),
I18n.t("admin.budget_investments.index.table_valuation_finished"),
I18n.t("admin.budget_investments.index.table_selection")]
csv_string = CSV.generate(options) do |csv|
csv << attrs
investments.each do |investment|
id = investment.id.to_s
title = investment.title
total_votes = investment.total_votes.to_s
admin = if investment.administrator.present?
investment.administrator.name
else
I18n.t("admin.budget_investments.index.no_admin_assigned")
end
vals = if investment.valuators.empty?
I18n.t("admin.budget_investments.index.no_valuators_assigned")
else
investment.valuators.collect(&:description_or_name).join(', ')
end
heading_name = investment.heading.name
price_string = "admin.budget_investments.index.feasibility"\
".#{investment.feasibility}"
price = I18n.t(price_string, price: investment.formatted_price)
valuation_finished = investment.valuation_finished? ?
I18n.t('shared.yes') :
I18n.t('shared.no')
csv << [id, title, total_votes, admin, vals, heading_name, price,
valuation_finished]
end
end
csv_string
def assigned_valuators
self.valuators.collect(&:description_or_name).compact.join(', ').presence
end
def assigned_valuation_groups
self.valuator_groups.collect(&:name).compact.join(', ').presence
end
private

View File

@@ -0,0 +1,59 @@
class Budget::Investment::Exporter
require 'csv'
def initialize(investments)
@investments = investments
end
def to_csv
CSV.generate(headers: true) do |csv|
csv << headers
@investments.each { |investment| csv << csv_values(investment) }
end
end
private
def headers
[
I18n.t("admin.budget_investments.index.table_id"),
I18n.t("admin.budget_investments.index.table_title"),
I18n.t("admin.budget_investments.index.table_supports"),
I18n.t("admin.budget_investments.index.table_admin"),
I18n.t("admin.budget_investments.index.table_valuator"),
I18n.t("admin.budget_investments.index.table_valuation_group"),
I18n.t("admin.budget_investments.index.table_geozone"),
I18n.t("admin.budget_investments.index.table_feasibility"),
I18n.t("admin.budget_investments.index.table_valuation_finished"),
I18n.t("admin.budget_investments.index.table_selection")
]
end
def csv_values(investment)
[
investment.id.to_s,
investment.title,
investment.total_votes.to_s,
admin(investment),
investment.assigned_valuators || '-',
investment.assigned_valuation_groups || '-',
investment.heading.name,
price(investment),
investment.valuation_finished? ? I18n.t('shared.yes') : I18n.t('shared.no'),
investment.selected? ? I18n.t('shared.yes') : I18n.t('shared.no')
]
end
def admin(investment)
if investment.administrator.present?
investment.administrator.name
else
I18n.t("admin.budget_investments.index.no_admin_assigned")
end
end
def price(investment)
price_string = "admin.budget_investments.index.feasibility.#{investment.feasibility}"
I18n.t(price_string, price: investment.formatted_price)
end
end

View File

@@ -24,6 +24,7 @@
<th><%= t("admin.budget_investments.index.table_supports") %></th>
<th><%= t("admin.budget_investments.index.table_admin") %></th>
<th><%= t("admin.budget_investments.index.table_valuator") %></th>
<th><%= t("admin.budget_investments.index.table_valuation_group") %></th>
<th><%= t("admin.budget_investments.index.table_geozone") %></th>
<th><%= t("admin.budget_investments.index.table_feasibility") %></th>
<th class="text-center"><%= t("admin.budget_investments.index.table_valuation_finished") %></th>
@@ -60,11 +61,12 @@
<% end %>
</td>
<td class="small">
<% if investment.valuators.size == 0 && investment.valuator_groups.size == 0 %>
<%= t("admin.budget_investments.index.no_valuators_assigned") %>
<% else %>
<%= assigned_valuators(investment) %>
<% end %>
<% no_valuators_assigned = t("admin.budget_investments.index.no_valuators_assigned") %>
<%= investment.assigned_valuators || no_valuators_assigned %>
</td>
<td class="small">
<% no_valuation_groups = t("admin.budget_investments.index.no_valuation_groups") %>
<%= investment.assigned_valuation_groups || no_valuation_groups %>
</td>
<td class="small">
<%= investment.heading.name %>

View File

@@ -171,6 +171,7 @@ en:
assigned_admin: Assigned administrator
no_admin_assigned: No admin assigned
no_valuators_assigned: No valuators assigned
no_valuation_groups: No valuation groups assigned
feasibility:
feasible: "Feasible (%{price})"
unfeasible: "Unfeasible"
@@ -182,6 +183,7 @@ en:
table_supports: "Supports"
table_admin: "Administrator"
table_valuator: "Valuator"
table_valuation_group: "Valuation Group"
table_geozone: "Scope of operation"
table_feasibility: "Feasibility"
table_valuation_finished: "Val. Fin."

View File

@@ -171,6 +171,7 @@ es:
assigned_admin: Administrador asignado
no_admin_assigned: Sin admin asignado
no_valuators_assigned: Sin evaluador
no_valuation_groups: Sin grupos evaluadores
feasibility:
feasible: "Viable (%{price})"
unfeasible: "Inviable"
@@ -182,6 +183,7 @@ es:
table_supports: "Apoyos"
table_admin: "Administrador"
table_valuator: "Evaluador"
table_valuation_group: "Grupos evaluadores"
table_geozone: "Ámbito de actuación"
table_feasibility: "Viabilidad"
table_valuation_finished: "Ev. Fin."

View File

@@ -64,22 +64,27 @@ feature 'Admin budget investments' do
admin = create(:administrator, user: create(:user, username: 'Gema'))
budget_investment1.valuators << valuator1
budget_investment2.valuator_ids = [valuator1.id, valuator2.id]
budget_investment3.update(administrator_id: admin.id)
budget_investment2.valuators << valuator1
budget_investment2.valuators << valuator2
visit admin_budget_budget_investments_path(budget_id: budget.id)
within("#budget_investment_#{budget_investment1.id}") do
expect(page).to have_content("No admin assigned")
expect(page).to have_content("Valuator Olga")
expect(page).to have_content("No valuation groups assigned")
end
within("#budget_investment_#{budget_investment2.id}") do
expect(page).to have_content("No admin assigned")
expect(page).to have_content("Valuator Olga")
expect(page).to have_content("Valuator Miriam")
expect(page).to have_content("No valuation groups assigned")
end
budget_investment3.update(administrator_id: admin.id)
visit admin_budget_budget_investments_path(budget_id: budget.id)
within("#budget_investment_#{budget_investment3.id}") do
expect(page).to have_content("Gema")
expect(page).to have_content("No valuators assigned")
@@ -109,7 +114,7 @@ feature 'Admin budget investments' do
end
within("#budget_investment_#{budget_investment3.id}") do
expect(page).to have_content("No valuators assigned")
expect(page).to have_content("No valuation groups assigned")
end
end
@@ -949,14 +954,25 @@ feature 'Admin budget investments' do
context "Selecting csv" do
scenario "Downloading CSV file" do
investment = create(:budget_investment, :feasible, budget: budget,
price: 100)
valuator = create(:valuator, user: create(:user, username: 'Rachel',
email: 'rachel@val.org'))
investment.valuators.push(valuator)
admin = create(:administrator, user: create(:user, username: 'Gema'))
investment.update(administrator_id: admin.id)
admin = create(:administrator, user: create(:user, username: 'Admin'))
valuator = create(:valuator, user: create(:user, username: 'Valuator'))
valuator_group = create(:valuator_group, name: "Valuator Group")
budget_group = create(:budget_group, name: "Budget Group", budget: budget)
first_budget_heading = create(:budget_heading, group: budget_group, name: "Budget Heading")
second_budget_heading = create(:budget_heading, group: budget_group, name: "Other Heading")
first_investment = create(:budget_investment, :feasible, :selected, title: "Le Investment",
budget: budget, group: budget_group,
heading: first_budget_heading,
cached_votes_up: 88, price: 99,
valuators: [],
valuator_groups: [valuator_group],
administrator: admin)
second_investment = create(:budget_investment, :unfeasible, title: "Alt Investment",
budget: budget, group: budget_group,
heading: second_budget_heading,
cached_votes_up: 66, price: 88,
valuators: [valuator],
valuator_groups: [])
visit admin_budget_budget_investments_path(budget)
@@ -966,39 +982,27 @@ feature 'Admin budget investments' do
expect(header).to match(/^attachment/)
expect(header).to match(/filename="budget_investments.csv"$/)
valuators = investment.valuators.collect(&:description_or_name).join(', ')
feasibility_string = "admin.budget_investments.index"\
".feasibility.#{investment.feasibility}"
price = I18n.t(feasibility_string, price: investment.formatted_price)
expect(page).to have_content investment.title
expect(page).to have_content investment.total_votes.to_s
expect(page).to have_content investment.id.to_s
expect(page).to have_content investment.heading.name
expect(page).to have_content investment.administrator.name
expect(page).to have_content valuators
expect(page).to have_content price
expect(page).to have_content I18n.t('shared.no')
csv_contents = "ID,Title,Supports,Administrator,Valuator,Valuation Group,Scope of operation,"\
"Feasibility,Val. Fin.,Selected\n#{first_investment.id},Le Investment,88,"\
"Admin,-,Valuator Group,Budget Heading,Feasible (€99),Yes,Yes\n"\
"#{second_investment.id},Alt Investment,66,No admin assigned,Valuator,-,"\
"Other Heading,Unfeasible,No,No\n"
expect(page.body).to eq(csv_contents)
end
scenario "Downloading CSV file with applied filter" do
investment1 = create(:budget_investment, :unfeasible, budget: budget,
title: 'compatible')
investment2 = create(:budget_investment, :finished, budget: budget,
title: 'finished')
unfeasible_investment = create(:budget_investment, :unfeasible, budget: budget,
title: 'Unfeasible one')
finished_investment = create(:budget_investment, :finished, budget: budget,
title: 'Finished Investment')
visit admin_budget_budget_investments_path(budget)
within('#filter-subnav') { click_link 'Valuation finished' }
click_link "Download current selection"
header = page.response_headers['Content-Disposition']
expect(header).to match(/^attachment/)
expect(header).to match(/filename="budget_investments.csv"$/)
expect(page).to have_content investment2.title
expect(page).not_to have_content investment1.title
expect(page).to have_content('Finished Investment')
expect(page).not_to have_content('Unfeasible one')
end
end